废话不聊,直接上步骤,然后上说明,最后上源码。
1、到官网下载hibernate-release-5.2.0.Final包。
2、用myeclipse新建web项目
3、写代码
步骤已说完。详解每一步
1、从官网下载jar以及源码的教程http://blog.csdn.net/leisure_life/article/details/60748288;ps:本人准备自己写的,可是这篇文章描述的太详细了
2、用myeclipse新建web项目。这个就不说了吧。
a)第一步下载好之后,把hibernate-release-5.2.0.Final\lib\required 文件夹下的jar放入项目的lib中。
b)本示例是用junit测试的,另外需要下载com.springsource.org.junit-4.7.0.jar
3、写代码,最刺激的,写代码。先写实体,然后写hibernate.cfg.xml,最后写测试用例
a)Student.java//学生实体类
@Entity(name="Student2161")
public class Student {
@Id //主键
@GeneratedValue //主键生成策略
private Integer sid;
private String name;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
b)hibernate.cfg.xml<?xml version='1.0' encoding='utf-8'?>
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
root
123456
5
50
23
org.hibernate.dialect.MySQL5Dialect
thread
true
update
c)HibernateUtil.java,此类是为了减少test的代码,因为鄙人准备搭建框架,所以沿用了多类少代码的节奏import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.*;
/**
* Description:
* @author VipMao
* @version 1.0
*/
/**
* 该工具类提供了一个属性:SessionFactory sessionFactory 并创建了sessionFactory 将它设置成static
* 这样其他程序就可以直接通过此工具类引用 提供了二个方法: 1:通过线程创建Session-->currentSession()
* 2:关闭Session-->closeSession() 需要在主类中手动关闭sessionFactory
*/
public class HibernateUtil {
public static final SessionFactory sessionFactory;
// 创建sessionFactory
static {
try {
//读取hibernate.cfg.xml文件
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("cfg/hibernate.cfg.xml").build();
//2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
System.out.println("ddd");
} catch (Throwable ex) {
System.out.println("ddd_________________");
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
// ThreadLocal可以隔离多个线程的数据共享,因此不再需要对线程同步
public static final ThreadLocal session = new ThreadLocal();
// 创建Session
public static Session currentSession() throws HibernateException {
// 通过线程对象.get()方法安全创建Session
Session s = session.get();
// 如果该线程还没有Session,则创建一个新的Session
if (s == null) {
s = sessionFactory.openSession();
// 将获得的Session变量存储在ThreadLocal变量session里
session.set(s);
}
return s;
}
// 关闭Session
public static void closeSession() throws HibernateException {
Session s = session.get();
if (s != null)
s.close();
session.set(null);
}
}
d)MyTest.javaimport org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sl.hibernate.entity.StuCard;
import com.sl.hibernate.entity.Student;
import com.sl.hibernate.util.HibernateUtil;
public class MyTest {
Session session;
Transaction tx;
// 单元测试前走
@Before
public void Before() {
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
}
// 单元测试后走
@After
public void After() {
tx.commit();
HibernateUtil.closeSession();
}
@Test
public void TestOne() {
Student student = new Student();
student.setName("good");
System.out.println("3333333333");
session.save(student);
}
}
此时就可以运行了。我捣饬了一下午,遇到很多错。
注:在搭建过程遇到很多错误,这些才是最宝贵的
1、找包,有些时候我们想急功近利,一个包一个包的搜。这样反而给自己造成更多的麻烦,所以建议到官网下载包
2、hibernate-release-5.2.0.Final\lib\required\hibernate-core-5.2.0.Final.jar 是下载的,可是运行的时候居然冲突,解决方案用hibernate-core-5.0.1.Final.jar 更换
3、项目运行的时候hibernate会自己生成一个tablehibernate_sequence;需要在这个table 塞入一个数据。后期有时间我来研究下,为什么会这样
4、 在HibernateUtil 中,创建sessionFactory 不能用Configuration,百度了下,据说hibernate5已经不玩这个了,切记切记