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

1.首先获取hibernate的jar导入,不写。

2.hibernate关键配置映射文件有两个,关键工具一个

分别是:
核心配置 hibernate.cfg.xml
持久化类对象与数据库映射配置*.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>
		<!-- 数据库驱动 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 数据库url -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/studentinfo</property>
		<!-- 数据库连接用户名 -->
		<property name="hibernate.connection.username">root</property>
		<!-- 数据库连接密码 -->
		<property name="hibernate.connection.password">123456</property>
		<!-- 数据库方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 把hibernate执行sql语句打印到控制台 开发完成就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;
	}
	// 静态代码块
	static {
		try {// 加载hibernate
			Configuration configuration = new Configuration().configure();// Configuration
			factory = configuration.buildSessionFactory();
		} catch (HibernateException e) {
			// TODO 自动生成的 catch 块
		}
	}

	// 获取Session
	public static Session getSession() throws Exception {
		Session session =(Session)sessionl.get();
		if (session == null || !session.isOpen()) {
			if(factory==null) {
				//只建立一次,不空继续
				rebuildSessionFactory();
			}
			//此处补上
			session=(factory==null)?null:factory.openSession();
			sessionl.set(session);
		}
		return session;
	}

	// 重新加载hibernate
	public static void rebuildSessionFactory() {

		try {// 加载hibernate
			Configuration configuration = new Configuration().configure();
			factory = configuration.buildSessionFactory();
		} catch (HibernateException e) {
			// TODO 自动生成的 catch 块
		}
	}

	// 获取SessionFactory
	public static SessionFactory getFactory() {
		return factory;
	}

	// 关闭Session
	public static void closeSession() throws Exception {
		Session session = (Session) sessionl.get();
		sessionl.set(null);
		if (session != null) {
			session.close();
		}
	}

}

现在编写实例

首先是学生信息持久化类:

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为数据库字段名}
首先在核心配置文件的持久化类源配置。
然后

<?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>

然后是调用查询类获取动态的要查询的ID,用到了hibernate工具类下降此类调用selectInfo()可将结果对象返回到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;

	// 持久化对象ID
	public 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 块
			
		}
		// 返回查询结果
		return studentInfo;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值