1.Hibernate包简介
hibernate3.jar: Hibernate的核心库,没有什么可说的,必须使用的jar包
dom4j.jar: dom4j是一个Java的XML API,类似于jdom,用于读写XMl文件
commons-collections.jar:
Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大。必须使用的jar包
antlr-2.7.6.jar:语言转换工,Hibernate利用它实现
HQL 到 SQL的转换
slf4j-api-1.5.8.jar:sfl4j,新版hibernate已经用log4j了
slf4j-log4j12-1.5.8.jar:slf4j到log4j的桥梁
javassist-3.9.0.GA.jar:代理
jta-1.1.jar:事务管理
log4j-1.2.17.jar:log4j,日志打印
2.搭建实例
环境:tomcat+myeclipese+mysql+hierbate3
1.引入相关包
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.17.jar
mysql-connector-java-5.1.17-bin.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.17.jar
mysql-connector-java-5.1.17-bin.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
2.创建hibernate.cfg.xml配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 方言 mysql -->
<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/hibernate?useUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 打印执行的sql -->
<property name="show_sql">true</property>
<!-- 映射的实体 -->
<mapping resource="com/shm/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.创建实体类User.java与User.hbm.xml
package com.shm.entity;
public class User {
private int id;
private String userName;
private String password;
private String trueName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTrueName() {
return trueName;
}
public void setTrueName(String trueName) {
this.trueName = trueName;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.shm.entity.User" table="user" >
<id name="id" type="int" column="id">
<generator class="increment"></generator>
</id>
<property name="userName" type="java.lang.String" column="user_name"
length="100">
</property>
<property name="password" type="java.lang.String" column="password"
length="100">
</property>
<property name="trueName" type="java.lang.String" length="100" column="true_name">
</property>
</class>
</hibernate-mapping>
4.写用例测试HibernateDemo
public class HibernateDemo {
@Test
public void testAdd(){
User user = new User();
user.setUserName("root");
user.setPassword("孙");
user.setTrueName("孙");
//Hibernate常用的四个API Configuration SessionFactory Session Query
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 创建SessionFactory(创建连接池)
SessionFactory sessionFactory = cfg.buildSessionFactory();
// 创建Session
Session session = sessionFactory.openSession();
// 创建及开启事务对象
Transaction tran = session.beginTransaction();
try {
session.save(user);
tran.commit();
} catch (HibernateException e) {
tran.rollback();
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
@Test
public void testDel(){
//Hibernate常用的四个API Configuration SessionFactory Session Query
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 创建SessionFactory(创建连接池)
SessionFactory sessionFactory = cfg.buildSessionFactory();
// 创建Session
Session session = sessionFactory.openSession();
// 创建及开启事务对象
Transaction tran = session.beginTransaction();
try {
User user = (User)session.get(User.class, 2);
session.delete(user);
tran.commit();
} catch (HibernateException e) {
tran.rollback();
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
@Test
public void testQuery(){
//Hibernate常用的四个API Configuration SessionFactory Session Query
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 创建SessionFactory(创建连接池)
SessionFactory sessionFactory = cfg.buildSessionFactory();
// 创建Session
Session session = sessionFactory.openSession();
// 创建及开启事务对象*/
Transaction tran = session.beginTransaction();
try {
Query query = session.createQuery("from User");
List<User> us = query.list();
for(User u : us){
System.out.println(u.getUserName());
}
tran.commit();
} catch (HibernateException e) {
tran.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession(session);
}
}
}
5.封装Hibernate的API:Configruation与SessionFactory
/**
* 单例封装
*
* @author yzx
*
*/
public class HibernateUtil {
private static Configuration configuration = null;
private static SessionFactory sessionFactory = null;
static{
if(configuration == null){
configuration = new Configuration().configure();
}
if(sessionFactory == null){
sessionFactory = configuration.buildSessionFactory();
}
}
public static Session getSession(){
return sessionFactory.openSession();
}
public static void closeSession(Session session){
if(session != null){
session.close();
}
}
}