1. Hibernate原理:
Hibernate技术本质上是一个提供数据库服务的中间件。Hibernate的工作原理,他是利用数据库以及其他一些配置文件如:hibernate.cfg.xml,
xxx.hbm.xml等来为应用程序提供数据持久化服务的。
- Configuration来读取hibernate.cfg.xml文件
- 使用Configuration对象创建SessionFactory
- 用SessionFactory来打开一个Session
- 创建事务Transaction
- begin事务
- 使用Session进行数据库操作
- 关闭或回滚事务
- 关闭Session
2. 配置Hibernate 声明:我使用的是JDK1.8,Hibernate3.6.0
3.1 导入相关Jar包:Hibernate模式不只是自己内部实现,同样也导入了许多外部的jar包,下面是Hibernate3.6.0框架的基本jar包
3.先创建一个Hello实体
/**
* @author 作者 Your-Name:
* @version 创建时间:2019年5月22日 下午2:49:26
* 类说明
*/
package test.entity;
/**
* @author Administrator
*
*/
public class Hello {
private int hid;
private String hname;
public int getHid() {
return hid;
}
public void setHid(int hid) {
this.hid = hid;
}
public String getHname() {
return hname;
}
public void setHname(String hname) {
this.hname = hname;
}
@Override
public String toString() {
return "Hello [hid=" + hid + ", hname=" + hname + "]";
}
}
4、配置Hello实体在数据库表的映射Hello.hbm.xml
<?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="test.entity">
<class name="Hello" table="hello">
<id name="hid">
<generator class="native"></generator>
</id>
<property name="hname" column="hname"></property>
</class>
</hibernate-mapping>
5、简单配置hibernate.cfg.xml配置文件,这个文件名可以随便修改(xxx.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">
<!--
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<!--
hibernate.show_sql true
hibernate.format_sql true
-->
<!--
## auto schema export
#hibernate.hbm2ddl.auto create-drop 加载hibernate时创建,退出是删除表结构
#hibernate.hbm2ddl.auto create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
#hibernate.hbm2ddl.auto update 加载hibernate自动更新数据库结构
#hibernate.hbm2ddl.auto validate 加载hibernate时,验证创建数据库表结构
-->
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///shop</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="test/entity/Hello.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、以上Hibernate简单配置已经基本完成,可以添加一个测试类来测试是否成功
/**
* @author 作者 Your-Name:
* @version 创建时间:2019年5月7日 下午1:41:41
* 类说明
*/
package test.action;
import java.util.UUID;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import test.entity.Hello;
/**
* @author Administrator
*
*/
public class Text {
private static SessionFactory sf = new Configuration().configure().buildSessionFactory();
@Test
public void add(){
Session session = null;
Transaction tran = null;
try{
session = sf.openSession();
tran = session.beginTransaction();
Hello h = new Hello();
h.setHname("你好");
session.save(h);
tran.commit();//事物的提交
}catch(Exception e){
tran.rollback();//事物的回滚
throw(e);
}finally{
session.close();
}
}
@Test
public void getHello(){
Session session = null;
Transaction tran = null;
try{
session = sf.openSession();
tran = session.beginTransaction();
/*这里指明你要获得哪个类型,Hibernate会根据类名查询映射配置文件到数据库查询哪张表,根据指定
* id查询实体,通过反射机制创建实体对象
*/
Hello hello = session.get(Hello.class,2);
tran.commit();
}catch(Exception e){
tran.rollback();
throw(e);
}finally{
session.close();
}
}
@Test
public void update(){
Session session = null;
Transaction tran = null;
try{
session = sf.openSession();
tran = session.beginTransaction();
Hello h = new Hello();
h.setHid(1);
h.setHname("chenzuolin");
session.update(h);
tran.commit();
}catch(Exception e){
tran.rollback();
throw(e);
}finally{
session.close();
}
}
}