Hibernate简介
hibernate是一个开源的ORM(对象关系映射)框架,也是一个持久层的框架,它对JDBC做了非常轻量级的封装。
Hibernate核心(六大接口+配置文件)
Configuration:负责配置并启动Hibernate
SessionFactory:负责初始化Hibernate
Session:负责持久化对象的CRUD操作
Transaction:负责事务
Query和Criteria:负责执行各种数据库查询
Hibernate工作原理
- 通过Configuration config = new Configuration().configure();//读取并解析hibernate.cfg.xml配置文件
- 由hibernate.cfg.xml中的<mapping resource = "com/xx/xx.hbm.xml" />//读取并解析映射信息
- 通过SessionFactory sf = config.buildSessionFactory();//创建SessionFactory会话工厂
- Session session = sf.openSession();//打开Session
- Transaction tx = session.beginTransaction();//创建并启动事务
- persistent operate CRUD//数据持久化操作
- tx.commit();//提交事务
- 关闭Session
- 关闭SesstionFactory
开发步骤:
- 创建Hibernate的配置文件
- 创建持久化类
- 创建对象-关系映射文件
- 通过Hibernate API编写访问数据库的代码
Hibernate开发实例
- 使用版本:Hibernate 4.2.4 + Mysql
- 导入Hibernate必须的jar包(Hibernate-release-4.2.4.Final\lib\required)
- 导入Mysql的jdbc驱动(mysql-connector-java-5.1.7-bin.jar)
- 导入Junit4的jar包(Junit单元测试工具)//测试用
- 创建Hibernate的配置文件 hibernate.cfg.xml
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/students</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/students</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
- 创建持久化类
import java.util.Date;
//学生类
public class Students {
private int sid;// 学号
private String sname;// 姓名
private String gender;// 性别
private Date birthday;// 出生日期
private String address;// 地址
public Students() {
}
public Students(int sid, String sname, String gender, Date birthday,
String address) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
- 创建对象-关系映射文件 Students.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENEDER" />
</property>
<property name="birthday" type="java.util.Data">
<column name="BIRTHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
- 将Students.hbm.xml加入hibernate.cfg.xml
<mapping resource="Students.hbm.xml" />
- 创建数据库students 刷新表就会自动产生students表
- @Test:测试方法
- @Before:初始化方法
- @After:释放资源
- 通过Hibernate API编写访问数据库的代码
- Configuration config = new Configuration().configure();//创建配置对象
- ServiceRegistry seviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();//创建服务注册对象
- sessionFactory = config.buildSessionFactory(serviceRegistry);//创建会话工厂对象
- session = sessionFactory.openSession();//打开对话
package Test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.Students;
//测试类
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transation;
@Before
public void init() {
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transation = session.beginTransaction();
}
@After
public void destory() {
transation.commit();//提交事务
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
@Test
public void testSaveStudents() {
//生成学生对象
Students s = new Students(1, "小明", "男", new Date(), "中国");
session.save(s);//保存对象进入数据库
}
}