标签:hibernate5-进介添加工具类 对获取session的方法封装
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; } }
<?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>
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(); } }
<?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>
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的方法封装
标签:hibernate5-进介添加工具类 对获取session的方法封装
原文地址:http://suyanzhu.blog.51cto.com/8050189/1911137