一、hibernate基本开发步骤
1.引入包,官网下载的包,目录\lib\required下的jar都加进去,这里使用的是 hibernate-release-5.2.1.Final
2.编写配置文件hibernate.cfg.xml,可用hibernate Tools自动生成
<?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"> <hibernate-configuration>
<session-factory> <!-- 连接地址 -->
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:TEST</property>
<!-- 连接驱动 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 数据库登录名 -->
<property name="hibernate.connection.username">system</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">XXXXXXXX</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</property>
<!-- 是否打印sql -->
<property name="show_sql">true</property>
<!-- 是否格式化sql -->
<property name="format_sql">true</property> <!-- 添加对应实体类的映射文件 -->
<mapping resource="com/lzq/model/Tb_User.hbm.xml" /> </session-factory>
</hibernate-configuration>
3.编写实体类,需要遵循javabeans的实体规范
package com.lzq.model; import java.util.Date; /**
* javabeans的实体规范
* 1.公有的类
* 2.提供公有的不带参数的默认构造方法
* 3.属性私有
* 4.属性setter/getter封装
*/
public class Tb_User { private String id;
private String name;
private int sex;
private Date birthday; 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 int getSex() {
return sex;
} public void setSex(int sex) {
this.sex = sex;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public Tb_User(String id, String name, int sex, Date birthday) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
} public Tb_User() {
}; @Override
public String toString() {
return "name:" + this.name + ";sex:" + this.sex + ";birthday:" + this.birthday;
}
}
4.编写对应实体类的映射文件并添加到配置文件中
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-7-16 11:24:17 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.lzq.model.Tb_User" table="TB_USER">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="sex" type="integer">
<column name="SEX" />
</property>
<property name="birthday" type="date">
<column name="BIRTHDAY" />
</property>
</class>
</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();
测试代码:
package Test; import java.util.Date;
import java.util.UUID; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.lzq.model.Tb_User; public class UserTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void init() {
// 创建配置对象
Configuration config = new Configuration();
// 创建会话工厂对象
sessionFactory = config.configure().buildSessionFactory();
// 会话对象
session = sessionFactory.openSession();
// 开启事务
transaction = session.beginTransaction(); } @After
public void destory() {
transaction.commit();// 提交事务
session.close();// 关闭会话
sessionFactory.close();// 关闭会话工厂
} @Test
public void testGetUser(){
Tb_User u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de");
System.out.println(u);
} /**
* load 方法执行后返回的是一个代理对象,只有再使用到非主键的属性时才会发送sql语句; load 查询出来为空会有异常 org.hibernate.ObjectNotFoundException
* */
@Test
public void testLoadUser(){
Tb_User u = session.load(Tb_User.class, "e662fa4f-ebf2-458f-a166-0ce9bb5c3cb6");
System.out.println(u);
} /**
* get 方法执行后 立即查询数据库,返回实体类的对象类型
* */
@Test
public void testUpdateUser() {
// 生成一个用户对象
Tb_User u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de");
u.setName("TestObj111");
session.update(u);
System.out.println(u);
} @Test
public void testDeleteUser() {
// 生成一个用户对象
Tb_User u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de");
session.delete(u);
u = session.get(Tb_User.class, "057138ac-c842-4aba-8662-0ba9a5b343de");
System.out.println(u);
} @Test
public void testSaveUser() {
// 生成一个用户对象
Tb_User u = new Tb_User(getUUID(), "沈七", 1, new Date());
session.save(u);
} private String getUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
}
最后附上完整的Demo 供参考:http://pan.baidu.com/s/1nuZA2LV