Hibernate 单框架搭建 测试Demo
1: 创建java/web project项目
2: 导入jar包 hibernate-release-4.2.19.Final\lib\required 下所有的 jar包,还需要导入一个jar包 数据库 的 驱动包(官网直接下载)
3:编写hibernate的核心配置文件
Hibernate.cfg.xml 文件
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hib</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 配置数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否显示sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化语句 -->
<property name="format_sql">true</property>
<!-- 自动创建表的类型 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 加载二级缓存 -->
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>
<!-- 实体类的映射文件 -->
<mapping resource="top/vkiss/hibernate/domain/Person.hbm.xml"/>
<mapping resource="top/vkiss/hibernate/domain/Customer.hbm.xml"/>
<mapping resource="top/vkiss/hibernate/domain/Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4定义实体类(entity,pojo,domain,perudre)
public class Person {
private Integer pid;
private String pername;
private String age;
private String sex;
<!-- 提供 getter / settter 方法 -->
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPername() {
return pername;
}
public void setPername(String pername) {
this.pername = pername;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
5 编写实体类的配置文件(实体类名.hbm.xml) --对象关系映射文件
作用:描述类和数据库中表之间的关系
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="top.vkiss.hibernate.domain">
<class name="Person" table="t_Person">
<!-- 主键配置 -->
<id name="pid" column="pid" type="integer">
<generator class="native"></generator>
</id>
<!-- 普通属性 -->
<property name="pername" column="pername" type="string" length="20"></property>
<property name="age" column="age" type="string" length="3"></property>
<property name="sex" column="sex" type="string" length="4"></property>
</class>
</hibernate-mapping>
6 编写hibernate代码完成增删改查的测试
1. 自定义封装 hibernate 读取配置文件的Utils 方法
package top.vkiss.hibernate.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtils {
private static SessionFactory sf;
private static Session session;
private HibernateUtils(){}
static{
Configuration cof=new Configuration();
// 配置文件
cof.configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(cof.getProperties()).buildServiceRegistry();
//获取到 session工厂对象
sf = cof.buildSessionFactory(serviceRegistry);
}
public static Session opeSession() {
// 如果没有, 或者已经关闭 - 根据工厂对象 获取一个 session对象
if (session==null || !session.isOpen()) {
session=sf.openSession();
}
return session;
}
<!-- 关闭Session -->
public static void closeSession() {
if (session!=null && session.isOpen()) {
session.close();
}
}
<!-- 关闭SessionFactory -->
public static void closeSessionFactory() {
if (sf!=null && !sf.isClosed()) {
sf.close();
}
}
}
编写测试文件
package top.vkiss.hibernate.test;
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 top.vkiss.hibernate.HibernateUtils.HibernateUtils;
import top.vkiss.hibernate.domain.Person;
public class TestHibernate {
public static void main(String[] args) {
Session session = HibernateUtils.opeSession();
//开启事务
Transaction tr= session.beginTransaction();
//进行数据库 操作 -- 增删改查
/* Person person = new Person();
person.setPid(1);
person.setAge("11");
person.setPername("周浩浩");
person.setSex("男");*/
//save方法 就是保存一条记录
//session.save(person);
//修改一条记录
//session.update(person);
//删除一条记录
//session.delete(person);
Person person= (Person) session.get(Person.class,2);
System.out.println(person);
<!-- 必须提交事务 -->
tr.commit();//事务提交
}
}