好久没接触hibernate了,今天做毕设忽然要连接数据库,想用hibernate来做持久化。就开始了搭建:
搭建环境:eclipse-luna-4.4.2 + jdk7
本人小菜一个,一开始在我的eclipse是没有hibernate的插件的,在百度上说在这个位置下载jboss插件:Eclipse, Help-->Eclipse Marketplace--》JBoss-Tools,但一打开就出现了死机。所以不推荐大家用这样的方式。
然后在博客(http://blog.sina.com.cn/s/blog_aaa756740101fub7.html)上看到要到这个位置上去下载:http://jaist.dl.sourceforge.net/sourceforge/jboss/HibernateTools-3.2.4.Beta1-R200810311334.zip
,个人电脑windows10连的wifi,启动内部的ie发现根本下载不动这个14M的东西,后换成firefox。直接秒传
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmL5Znbl9CX0xWdhZWZk9CX09Wbl9lcvRXakVGa49CXy9GdpRWZoh3LcRXZu5ibkN3Yuc2bsJmLjlGdhR3cvw1LcpDc0RHaiojIsJye.gif)
。
接下来进入正题:
1.将刚下载的插件解压:出现两个文件夹:plugins和features。将这两个文件夹直接放在eclipse的安装位置。
2.下载hibernate的jar包。我这里用到的版本是:hibernate-distribution-3.6.10.Final。解压后放在将以下jar放在 webinfo-lib下,add-to-buildpath:
解压后主目录下 hibernate3.jar
解压后lib\required\* 目录下antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar javassist-3.12.0.GA.jar jta-1.1.jar slf4j-api-1.6.1.jar
最后还有lib\jpa 下的 hibernate-jpa-2.0-api-1.0.1.Final.jar 本人这里就因为没有加这个jar,报了 Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
3.添加mysql驱动jar:本人这里用到的是:mysql-connector-java-5.1.34.jar 到 webinfo-lib下,add-to-buildpath
4.环境基本get,接下来放大招了:
点击项目——》右键——》new ——》other——》输入hibernate——》选择
——》next——》填写mysql驱动配置。finished。生成hibernate.cfg.xml
点击项目——》右键——》new ——》other——》输入hibernate——》选择
——》next ——》finished。生成User.hbm.xml。(hbm.xml前面的名字是参照你的bean类的)
环境基本搭成。
5.接下来配置xml文件:
以我的bean类User为例:
User.java
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String password;
private int sex;
private int age;
Constructor;
getter and setter;
toString;
}
所以相对应的在hibernate.cfg.xml里的session-factory里面添加:
<mapping resource="com/cyan/entity/User.hbm.xml"></mapping>
这里注意:resource里的路径是“/”而不是“.”
User.hbm.xml则为:
<hibernate-mapping package="com.cyan.entity">
<class name="User" table="user">
<id name="name" column="name"></id>
<property name="password" column="password"></property>
<property name="sex" column="sex"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
这里必须设置id,否则会报错。
6.写一个HibernateSessionFactory:(这是鄙人抄教科书的)
public class HibernateSessionFactory {
private static String CONGIG_FILE_LOCATION = "hibernate.cfg.xml";
private static final ThreadLocal<Session> THREAD_LOCAL = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static SessionFactory sessionFactory;
static{
configuration.configure(CONGIG_FILE_LOCATION);
sessionFactory = configuration.buildSessionFactory();
}
private HibernateSessionFactory(){
}
public static Session getSession(){
Session session = THREAD_LOCAL.get();
if(session == null || session.isOpen()){
if(sessionFactory == null){
rebuildSessionFactory();
}
}
session = (sessionFactory != null)?sessionFactory.openSession():null;
return session;
}
public static void rebuildSessionFactory(){
try{
configuration.configure(CONGIG_FILE_LOCATION);
sessionFactory = configuration.buildSessionFactory();
}catch(Exception e){
System.out.println("SessionFactory build error");
e.printStackTrace();
}
}
public static void main(String[] args) {
getSession();
}
public static void closeSession(){
Session session = THREAD_LOCAL.get();
THREAD_LOCAL.set(null);
if(session != null){
session.close();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static void setConfigFile(String CONGIG_FILE_LOCATION){
HibernateSessionFactory.CONGIG_FILE_LOCATION = CONGIG_FILE_LOCATION;
sessionFactory = null;
}
public static Configuration getConfiguration(){
return configuration;
}
}
7.测试:
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
User user = new User("xiaoqing","xiaoqing",1,13);
session.save(user);
tx.commit();
System.out.println("get");
HibernateSessionFactory.closeSession();
}
}
8.get.
由于今天过晚,只简单记录下自己的操纵步骤。后续再完善。