标签:
一、hibernate基本开发步骤
1.引入包,官网下载的包,目录\lib\required下的jar都加进去,这里使用的是 hibernate-release-5.2.1.Final
2.编写配置文件hibernate.cfg.xml,可用hibernate Tools自动生成
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 9 <!-- 连接地址 --> 10 <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:TEST</property> 11 <!-- 连接驱动 --> 12 <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 13 <!-- 数据库登录名 --> 14 <property name="hibernate.connection.username">system</property> 15 <!-- 数据库密码 --> 16 <property name="hibernate.connection.password">XXXXXXXX</property> 17 <!-- 数据库方言 --> 18 <property name="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</property> 19 <!-- 是否打印sql --> 20 <property name="show_sql">true</property> 21 <!-- 是否格式化sql --> 22 <property name="format_sql">true</property> 23 24 25 <!-- 添加对应实体类的映射文件 --> 26 <mapping resource="com/lzq/model/Tb_User.hbm.xml" /> 27 28 </session-factory> 29 </hibernate-configuration>
3.编写实体类,需要遵循javabeans的实体规范
1 package com.lzq.model; 2 3 import java.util.Date; 4 5 /** 6 * javabeans的实体规范 7 * 1.公有的类 8 * 2.提供公有的不带参数的默认构造方法 9 * 3.属性私有 10 * 4.属性setter/getter封装 11 */ 12 public class Tb_User { 13 14 private String id; 15 private String name; 16 private int sex; 17 private Date birthday; 18 19 public String getId() { 20 return id; 21 } 22 23 public void setId(String id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public int getSex() { 36 return sex; 37 } 38 39 public void setSex(int sex) { 40 this.sex = sex; 41 } 42 43 public Date getBirthday() { 44 return birthday; 45 } 46 47 public void setBirthday(Date birthday) { 48 this.birthday = birthday; 49 } 50 51 public Tb_User(String id, String name, int sex, Date birthday) { 52 super(); 53 this.id = id; 54 this.name = name; 55 this.sex = sex; 56 this.birthday = birthday; 57 } 58 59 public Tb_User() { 60 }; 61 62 @Override 63 public String toString() { 64 return "name:" + this.name + ";sex:" + this.sex + ";birthday:" + this.birthday; 65 } 66 }
4.编写对应实体类的映射文件并添加到配置文件中
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2016-7-16 11:24:17 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="com.lzq.model.Tb_User" table="TB_USER"> 7 <id name="id" type="java.lang.String"> 8 <column name="ID" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="NAME" /> 13 </property> 14 <property name="sex" type="integer"> 15 <column name="SEX" /> 16 </property> 17 <property name="birthday" type="date"> 18 <column name="BIRTHDAY" /> 19 </property> 20 </class> 21 </hibernate-mapping>
5.调用hibernate api,使用的Junit测试
二、hibernate接口调用步骤
1.创建配置对象 Configuration ,用他读取了hibernate.cfg.xml,代表了应用程序到数据库的配置信息
Configuration config = new Configuration();
2.创建会话工厂SessionFactory,他是生成session的工厂
sessionFactory = config.configure().buildSessionFactory();
3.获取会话对象
session = sessionFactory.openSession();//一个session就是一个数据库连接
session = sessionFactory.getCurrentSession();//使用了单例模式,每次创建相同的对象
4.开启事务
transaction = session.beginTransaction();
5.调用get , load, delete, save, update增删改查
6.提交事务,更新到数据库
transaction.commit();
7.如果openSession,用完后需要显示的关闭
session.close();
如果是getCurrentSession,再提交事务后,会自动关闭
8.关闭Session工厂
sessionFactory.close();
测试代码:
1 package Test; 2 3 import java.util.Date; 4 import java.util.UUID; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 11 import org.junit.After; 12 import org.junit.Before; 13 import org.junit.Test; 14 15 import com.lzq.model.Tb_User; 16 17 public class UserTest { 18 19 private SessionFactory sessionFactory; 20 private Session session; 21 private Transaction transaction; 22 23 @Before 24 public void init() { 25 // 创建配置对象 26 Configuration config = new Configuration(); 27 // 创建会话工厂对象 28 sessionFactory = config.configure().buildSessionFactory(); 29 // 会话对象 30 session = sessionFactory.openSession(); 31 // 开启事务 32 transaction = session.beginTransaction(); 33 34 35 } 36 37 @After 38 public void destory() { 39 transaction.commit();// 提交事务 40 session.close();// 关闭会话 41 sessionFactory.close();// 关闭会话工厂 42 } 43 44 45 @Test 46 public void testGetUser(){ 47 Tb_User u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de"); 48 System.out.println(u); 49 } 50 51 /** 52 * load 方法执行后返回的是一个代理对象,只有再使用到非主键的属性时才会发送sql语句; load 查询出来为空会有异常 org.hibernate.ObjectNotFoundException 53 * */ 54 @Test 55 public void testLoadUser(){ 56 Tb_User u = session.load(Tb_User.class, "e662fa4f-ebf2-458f-a166-0ce9bb5c3cb6"); 57 System.out.println(u); 58 } 59 60 /** 61 * get 方法执行后 立即查询数据库,返回实体类的对象类型 62 * */ 63 @Test 64 public void testUpdateUser() { 65 // 生成一个用户对象 66 Tb_User u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de"); 67 u.setName("TestObj111"); 68 session.update(u); 69 System.out.println(u); 70 } 71 72 73 @Test 74 public void testDeleteUser() { 75 // 生成一个用户对象 76 Tb_User u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de"); 77 session.delete(u); 78 u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de"); 79 System.out.println(u); 80 } 81 82 @Test 83 public void testSaveUser() { 84 // 生成一个用户对象 85 Tb_User u = new Tb_User(getUUID(), "沈七", 1, new Date()); 86 session.save(u); 87 } 88 89 private String getUUID() { 90 UUID uuid = UUID.randomUUID(); 91 return uuid.toString(); 92 } 93 }
最后附上完整的Demo 供参考:http://pan.baidu.com/s/1nuZA2LV
标签:
原文地址:http://www.cnblogs.com/lzq1065763582/p/5676782.html