时间:2021-07-01 10:21:17 帮助过:2人阅读
以下是我的hibernate.cfg.xml的配置情况:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- old: http://hibernate.sourceforge.net/hibernate-configuration-3.6.dtd --> <!-- new: http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd --> <!-- : http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd --> <hibernate-configuration> <session-factory> <!-- 显示sql语句 --> <property name="show_sql">true</property> <property name="myeclipse.connection.profile">bookshop</property> <!-- <property name="connection.url"> jdbc:mysql://localhost:3306/bookshop jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8 </property> --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.username">root</property> <property name="connection.password">xxxxxxxxcc</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property> <!-- 将实体类映射到数据库 --> <mapping class="com.entity.Students"/> </session-factory> </hibernate-configuration>
之后生成数据库表结构:
@Test public void testShemaExport(){ //创建Hibernate配置对象 Configuration configuration = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); //创建sessionFactory SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); //生成SchemaExport对象 SchemaExport export = new SchemaExport(configuration); //调用schemaExport的create生成数据库表结构 export.create(true, true); }
将数据插入数据库:
@Test public void addStudents() { //创建Hibernate配置对象 Configuration configuration = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); //创建sessionFactory SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); //创建会话 Session session = sessionFactory.getCurrentSession(); //创建事务 Transaction tx = session.beginTransaction(); //创建一个地址对象 Address add = new Address("700005","武当山","1388732789"); //创建一个学生对象 Students s = new Students("S00000001","张三丰","男", new Date(),"太极拳",add); //保存session session.save(s);//在没有执行commit之前数据库里面是没有保存数据的 tx.commit(); }
log:
2016-4-16 14:46:47 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 2016-4-16 14:46:47 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.21.Final} 2016-4-16 14:46:47 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 2016-4-16 14:46:47 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 2016-4-16 14:46:47 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 2016-4-16 14:46:47 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 2016-4-16 14:46:47 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 2016-4-16 14:46:47 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) 2016-4-16 14:46:48 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 2016-4-16 14:46:48 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 2016-4-16 14:46:48 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8] 2016-4-16 14:46:48 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} 2016-4-16 14:46:48 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 2016-4-16 14:46:48 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 2016-4-16 14:46:48 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 2016-4-16 14:46:48 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000102: Fetching database metadata 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000396: Updating schema 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: t_students 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: t_students 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: t_students 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute ERROR: HHH000388: Unsuccessful: create table bookshop.t_students (sid varchar(20) not null, address varchar(255), phone varchar(255), postCode varchar(255), birthday datetime, gender varchar(255), major varchar(255), sname varchar(255), primary key (sid)) 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute ERROR: Table ‘t_students‘ already exists 2016-4-16 14:46:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: insert into bookshop.t_students (address, phone, postCode, birthday, gender, major, sname, sid) values (?, ?, ?, ?, ?, ?, ?, ?)
数据库显示正常。中文乱码问题彻底小时。
Java在mysql插入数据的时候的乱码问题解决
标签: