日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate的映射关系配置及对会话工厂的初始化。以及struts2写实例查询

發(fā)布時間:2024/4/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate的映射关系配置及对会话工厂的初始化。以及struts2写实例查询 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.首先獲取hibernate的jar導入,不寫。

2.hibernate關鍵配置映射文件有兩個,關鍵工具一個

分別是:
核心配置 hibernate.cfg.xml
持久化類對象與數(shù)據(jù)庫映射配置*.hbm.xml
hibernate會話工廠初始化,會話管理工具

核心配置文件hibernate.cfg.xml `配置,在src目錄

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration><session-factory><!-- 數(shù)據(jù)庫驅(qū)動 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- 數(shù)據(jù)庫url --><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/studentinfo</property><!-- 數(shù)據(jù)庫連接用戶名 --><property name="hibernate.connection.username">root</property><!-- 數(shù)據(jù)庫連接密碼 --><property name="hibernate.connection.password">123456</property><!-- 數(shù)據(jù)庫方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 把hibernate執(zhí)行sql語句打印到控制臺 開發(fā)完成就false,我一直奇怪sql哪來的,怪我,日志則不一定--><property name="hibernate.show_sql">true</property><!-- 把生成的sql格式化一下,方便閱讀 --><property name="hibernate.format_sql">true</property><!-- *.hbm.xml映射文件 --><mapping resource="model/stuinfo/StudentInfo.hbm.xml" /></session-factory> </hibernate-configuration>

hibernate會話工廠初始化,會話管理工具類編寫:

package com.hibernateInit;import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;public class HbInit {/*** @author joker hibernate 的初始化工具*/private static SessionFactory factory = null;// 會話工廠private static final ThreadLocal<Session> sessionl = new ThreadLocal<Session>();//會話集合public static ThreadLocal<Session> getSessionL() {return sessionl;}// 靜態(tài)代碼塊static {try {// 加載hibernateConfiguration configuration = new Configuration().configure();// Configurationfactory = configuration.buildSessionFactory();} catch (HibernateException e) {// TODO 自動生成的 catch 塊}}// 獲取Sessionpublic static Session getSession() throws Exception {Session session =(Session)sessionl.get();if (session == null || !session.isOpen()) {if(factory==null) {//只建立一次,不空繼續(xù)rebuildSessionFactory();}//此處補上session=(factory==null)?null:factory.openSession();sessionl.set(session);}return session;}// 重新加載hibernatepublic static void rebuildSessionFactory() {try {// 加載hibernateConfiguration configuration = new Configuration().configure();factory = configuration.buildSessionFactory();} catch (HibernateException e) {// TODO 自動生成的 catch 塊}}// 獲取SessionFactorypublic static SessionFactory getFactory() {return factory;}// 關閉Sessionpublic static void closeSession() throws Exception {Session session = (Session) sessionl.get();sessionl.set(null);if (session != null) {session.close();}}}

現(xiàn)在編寫實例

首先是學生信息持久化類:

package model.stuinfo;public class StudentInfo {/*** @author joker * @param 學生信息庫持久化類*/private Integer sno;public Integer getSno() {return sno;}public void setSno(Integer sno) {this.sno = sno;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getSsex() {return ssex;}public void setSsex(String ssex) {this.ssex = ssex;}private String sname;private String ssex;private String scomputer_g;public String getScomputer_g() {return scomputer_g;}public void setScomputer_g(String scomputer_g) {this.scomputer_g = scomputer_g;}// 持久化的無參構造public StudentInfo() {}}

緊接著配置StudentInfo.hbm.xml持久化類的映射文件。{type為持久化類的類型,column為數(shù)據(jù)庫字段名}
首先在核心配置文件的持久化類源配置。
然后

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping ><!-- 學生信息庫studentinfo --><class name="model.stuinfo.StudentInfo" table="info"><id name="sno" column="sno" type="java.lang.Integer" ><generator class="native"></generator></id><property name="sname" not-null="true" type="java.lang.String"><column name="sname"></column></property><property name="ssex" not-null="true" type="java.lang.String"><column name="ssex"></column></property><property name="scomputer_g" not-null="true" type="java.lang.String"><column name="scomputer_g"></column></property></class></hibernate-mapping>

然后是調(diào)用查詢類獲取動態(tài)的要查詢的ID,用到了hibernate工具類下降此類調(diào)用selectInfo()可將結(jié)果對象返回到controller,struts2層

package hb.model.op;import org.hibernate.Session;import com.hibernateInit.HbInit;import model.stuinfo.StudentInfo;public class SelectStuInfo {/*** @author joker* * @param hb_select_studentinfo* */private String ID;// 持久化對象IDpublic SelectStuInfo(String ID) {this.ID = ID;}// 返回持久化對象public StudentInfo selectInfo() {// TODO 自動生成的方法存根Session session = null;// 持久化對象StudentInfo studentInfo = null;try {session = HbInit.getSession();// get方法(持久類,持久化對象都有唯一標識),立即查詢;load延遲裝載//此處get合理,studentInfo = session.get(StudentInfo.class, new Integer(ID));HbInit.closeSession();} catch (Exception e) {// TODO 自動生成的 catch 塊}// 返回查詢結(jié)果return studentInfo;}}

總結(jié)

以上是生活随笔為你收集整理的hibernate的映射关系配置及对会话工厂的初始化。以及struts2写实例查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。