Hibernate5-進階添加工具類,對擷取Session的方法封裝

來源:互聯網
上載者:User

標籤:hibernate5-進介添加工具類   對擷取session的方法封裝   

1.建立項目,項目名稱hibernatedemo3,目錄結構

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/8F/4B/wKiom1jaH_axbUh9AAApFRwwxjI510.png-wh_500x0-wm_3-wmp_4-s_4016444450.png" title="QQ20170328163309.png" alt="wKiom1jaH_axbUh9AAApFRwwxjI510.png-wh_50" />


2.在項目中建立lib目錄存放jar檔案,目錄結構

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M02/8F/4A/wKioL1jaICaQOjEsAAB3fIV6qQQ519.png-wh_500x0-wm_3-wmp_4-s_3779231332.png" title="QQ20170328163355.png" alt="wKioL1jaICaQOjEsAAB3fIV6qQQ519.png-wh_50" />


3.在src目錄中建立實體Bean Forum,包名(com.mycompany.demo.bean),

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8F/4B/wKiom1jaIEXBXQskAABB8BLGc5E355.png-wh_500x0-wm_3-wmp_4-s_1451098116.png" title="QQ20170328163429.png" alt="wKiom1jaIEXBXQskAABB8BLGc5E355.png-wh_50" />


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;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + fid;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic 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),

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/8F/4B/wKiom1jaIIChOm-1AABDfaIETsE346.png-wh_500x0-wm_3-wmp_4-s_3287520910.png" title="QQ20170328163529.png" alt="wKiom1jaIIChOm-1AABDfaIETsE346.png-wh_50" />


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),

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8F/4A/wKioL1jaIOXwkXzMAAA-QR6FLRo980.png-wh_500x0-wm_3-wmp_4-s_3199441275.png" title="QQ20170328163708.png" alt="wKioL1jaIOXwkXzMAAA-QR6FLRo980.png-wh_50" />


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,

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/8F/4B/wKiom1jaIWKBImUjAAA5uocRFi8832.png-wh_500x0-wm_3-wmp_4-s_1635682550.png" title="QQ20170328163914.png" alt="wKiom1jaIWKBImUjAAA5uocRFi8832.png-wh_50" />


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),目錄結構

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/8F/4B/wKiom1jaIjrw6g_vAAA2Ts272I4038.png-wh_500x0-wm_3-wmp_4-s_3491753065.png" title="QQ20170328164250.png" alt="wKiom1jaIjrw6g_vAAA2Ts272I4038.png-wh_50" />


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 {@Testpublic void testAdd(){//1.擷取SessionSession 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的方法封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.