Hibernate5-进阶添加工具类,对获取Session的方法封装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate5-进阶添加工具类,对获取Session的方法封装相关的知识,希望对你有一定的参考价值。
1.创建项目,项目名称hibernatedemo3,目录结构如图所示
2.在项目中创建lib目录存储jar文件,目录结构如图所示
3.在src目录中创建实体Bean Forum,包名(com.mycompany.demo.bean),如图所示
4.实体Bean Forum的内容如下
package com.mycompany.demo.bean; public class Forum { private int fid; private String name; public Forum() { super(); } public Forum(String name) { super(); this.name = name; } public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + fid; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Forum other = (Forum) obj; if (fid != other.fid) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
5.在src目录中创建实体Bean Forum的映射文件Forum.hbm.xml,包名(com.mycompany.demo.bean),如图所示
6.映射文件Forum.hbm.xml的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.mycompany.demo.bean.Forum" table="hnsq_forum"> <meta attribute="class-description"> This class contains the forum detail. </meta> <id name="fid" type="int" column="fid"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> </class> </hibernate-mapping>
7.在src目录中创建工具类 HbnUtil,包名(com.mycompany.demo.util),如图所示
8.工具类 HbnUtil的内容如下
package com.mycompany.demo.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HbnUtil { private static SessionFactory sessionFactory; public static Session getSession(){ if(sessionFactory == null || sessionFactory.isClosed()){ sessionFactory = new Configuration().configure().buildSessionFactory(); } return sessionFactory.getCurrentSession(); } }
9.在src目录中创建Hibernate的配置文件hibernate.cfg.xml,如图所示
10.Hibernate的配置文件hibernate.cfg.xml的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.mysqlDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Assume test is the database name --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/b_shequ_two</property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"></property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 事务环境一个线程对一个事务 --> <property name="hibernate.current_session_context_class">thread</property> <!-- List of XML mapping files --> <mapping resource="com/mycompany/demo/bean/Forum.hbm.xml"/> </session-factory> </hibernate-configuration>
11.在项目中创建test目录存储测试文件,文件名称ManageForum,包名(com.mycompany.demo.bean),目录结构如图所示
12.ManageForum测试类的内容如下
package com.mycompany.demo.bean; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.mycompany.demo.util.HbnUtil; public class ManageForum { @Test public void testAdd(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = new Forum("hbnutil"); session.save(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } e.printStackTrace(); } } }
本文出自 “素颜” 博客,谢绝转载!
以上是关于Hibernate5-进阶添加工具类,对获取Session的方法封装的主要内容,如果未能解决你的问题,请参考以下文章
Java进阶 - 数据结构ListSetCollections工具类
Spring 4 + Hibernate 5 = org.springframework.orm.jpa.EntityManagerHolder 不能转换为 org.springframework.o