hibernate的映射关系配置及对会话工厂的初始化。以及struts2写实例查询
生活随笔
收集整理的這篇文章主要介紹了
hibernate的映射关系配置及对会话工厂的初始化。以及struts2写实例查询
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.首先獲取hibernate的jar導(dǎo)入,不寫。
2.hibernate關(guān)鍵配置映射文件有兩個,關(guān)鍵工具一個
分別是:
核心配置 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();}//此處補(bǔ)上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;}// 關(guān)閉Sessionpublic static void closeSession() throws Exception {Session session = (Session) sessionl.get();sessionl.set(null);if (session != null) {session.close();}}}現(xiàn)在編寫實例
首先是學(xué)生信息持久化類:
package model.stuinfo;public class StudentInfo {/*** @author joker * @param 學(xué)生信息庫持久化類*/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;}// 持久化的無參構(gòu)造public StudentInfo() {}}緊接著配置StudentInfo.hbm.xml持久化類的映射文件。{type為持久化類的類型,column為數(shù)據(jù)庫字段名}
首先在核心配置文件的持久化類源配置。
然后
然后是調(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方法(持久類,持久化對象都有唯一標(biāo)識),立即查詢;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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaweb利用servlet与str
- 下一篇: 总结xml配置spring-aop声明式