今天开始学习hibernate,碰到的几个问题在这里记录一下。
1>导入hibernate5.2.10的hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required 文件夹下的jar包,及mysql数据库驱动。
2>配置hibernate.cfg.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=UTF8</property> 10 <property name="connection.username">root</property> 11 <property name="connection.password"></property> 12 <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch 13 Size越小,读数据库的次数越多,速度越慢 --> 14 <property name="jdbc.fetch_size">50 </property> 15 <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大 --> 16 <property name="jdbc.batch_size">23 </property> 17 <!-- SQL 方言--> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 <!-- Enable Hibernate's automatic session context management --> 20 <property name="current_session_context_class">thread</property> 21 <!-- 在控制台输出sql语句 --> 22 <property name="show_sql">true</property> 23 <!-- 在启动时根据配置更新数据库 --> 24 <property name="hbm2ddl.auto">update</property> 25 <mapping resource="com/cnblogs/hibernate_first/User.hbm.xml" /><!-- 注册我们的实体映射类 --> 26 </session-factory> 27 </hibernate-configuration>
以上配置需要注意几个问题:
a建立数据库的时候一定要指定数据库编码,如:create database hibernate_first DEFAULT CHARACTER SET UTF8及数据库url:
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=UTF8</property> 否则可能产生持久化的时候出现的乱码情况
如:
b <mapping resource="com/cnblogs/hibernate_first/User.hbm.xml" /><!-- 注册我们的实体映射类 --> 注册实体映射类注意带包名,其中“.”用“/”代替。
c如果报错前言中不允许有内容,可能是在xml文件的开头有不必要的空格,记得检查。
备注:因为是刚刚学习hibernate 配置中的很多property 可以查阅相关资料,以上配置没有过多列出。
3>建立实体类及配置映射文件,因为是刚刚学习,所以配置的很简单,后续进一步学习将进行整改。
package com.cnblogs.hibernate_first; import java.util.Date; public class User { private String id; private String name; private String password; private Date createDate; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="User" table="user"> <id name="id" column="id"> <generator class="uuid"></generator> </id> <property name="name" column="name" /> <property name="password" column="password" /> <property name="createDate" column="createDate" /> </class> </hibernate-mapping>
以上配置需要注意以下问题:
a <hibernate-mapping package="com.cnblogs.hibernate_first"> 记得指定包名。要不报错找不到映射文件。
4>测试
1 public class Test1 { 2 public static void main(String[] args) { 3 //读取配置文件 4 Configuration cfg = new Configuration().configure(); 5 //建立SessionFactory 6 SessionFactory factory = cfg.buildSessionFactory(); 7 Session session = null; 8 Transaction transation = null; 9 try { 10 //打开Session 11 session = factory.openSession(); 12 //开启事务 13 transation = session.beginTransaction(); 14 User user = new User(); 15 user.setName("王五"); 16 user.setPassword("123"); 17 user.setCreateDate(new Date()); 18 session.save(user); 19 //提交事务 20 transation.commit(); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 //回滚 24 transation.rollback(); 25 }finally{ 26 //关闭Session 27 if(session != null){ 28 if(session.isOpen()){ 29 session.close(); 30 } 31 } 32 } 33 } 34 }
提交结果如下图: