OpenSessionInView模式
首先搭建建构

引入jar包

创建实体类 Emp.java
public class Emp {
private Integer empId;//员工ID
private String empname; //员工姓名
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
}
配置大配置
<hibernate-configuration>
<session-factory>
<!-- 1.连接数据库的语句 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">0123</property> <!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.show_sql">true</property> <!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property> <!-- hbm2ddl -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 支持getCurrentSession的 属性配置 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 关联小配置 --> <!-- <mapping resource="cn/happy/entity/Project.hbm.xml"/> -->
<mapping resource="entity/Emp.hbm.xml"/> </session-factory> </hibernate-configuration>
配置 小配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="Emp" table="Emp2">
<id name="empId" column="EMPID">
<generator class="native"></generator>
</id>
<property name="empname" type="string" column="empname"></property> <!-- 多对多 -->
<!-- <set name="pros" table="ProEmp">
<key column="nid"></key>
<many-to-many class="Project" column="pid"></many-to-many>
</set>-->
</class> </hibernate-mapping>
创建HibernateUtil工具类
public class HibernateUtil {
private static final ThreadLocal sessionTL = new ThreadLocal();
private static Configuration configuration;
//
private static final SessionFactory sessionFactory;
static{
try {
configuration=new Configuration().configure();
sessionFactory=configuration.buildSessionFactory();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session getSession() {
Session session = (Session)sessionTL.get();
if(session==null)
{
session = sessionFactory.openSession();
sessionTL.set(session);
}
return session;
}
public static void closeSession()
{
Session session = (Session)sessionTL.get();
sessionTL.set(null);
session.close();
}
}
搭建Dao
package dao;
import java.io.Serializable;
import util.HibernateUtil;
public class Mydao {
public Object get(Class clazz,Serializable id){
System.out.println("dao\t"+HibernateUtil.getSession());
Object result= HibernateUtil.getSession().load(clazz, id);
return result;
}
}
biz层
public class Hibernatebiz {
Mydao dao=new Mydao();
public Object get(Class clazz,Serializable id){
// Transaction tx = HibernateUtil.getSession().beginTransaction();
Object obj= dao.get(clazz, id);
System.out.println("==============================================");
// tx.commit();
// HibernateUtil.closeSession();
return obj;
}
}
filter类
public class MyFilter implements Filter{
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Session session;
Transaction tx=null;
try {
session=HibernateUtil.getSession();
tx=session.beginTransaction();
chain.doFilter(request, response);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
finally{
HibernateUtil.closeSession();
}
}
最后编写测试类
public class H_01Test {
@Test
public void addTest(){
Hibernatebiz biz=new Hibernatebiz();
Object object = biz.get(Emp.class,1);
Emp emp=(Emp)object;
System.out.println(emp.getEmpname());
}
}
结果:


OpenSessionInView模式的更多相关文章
- [转]OpenSessionInView模式
OpenSessionInView模式解决的问题: * hibernate事物边界问题 * 因session关闭导致hibernate延迟加载例外的问题 事物边界: 一个事物的完成应该 ...
- SSH第一篇【整合SSH步骤、OpenSessionInView】
前言 到目前为止,Struts2.Hibernate.Spring框架都过了一遍了.也写过了Spring怎么与Struts2整合,Spring与Hibernate整合-本博文主要讲解SSH的整合 整合 ...
- 【转】Hibernate 常见异常
转载地址:http://smartan.iteye.com/blog/1542137 Hibernate 常见异常net.sf.hibernate.MappingException 当出 ...
- Struts2,Spring, Hibernate三大框架SSH的整合步骤
整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...
- hibernate中load和get方法的区别
1.读取时机不同(当lazy=true的时候) load是采用延迟机制(load语句不读库,等使用非主键时才去读库),而get不采用延 迟机制(get语句时马上读库): 2.搜索不到数据时的情 ...
- struts2支持的结果类型
在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这 些result-ty ...
- Spring 管理数据源
Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...
- Hibernate 常见异常
Hibernate 常见异常net.sf.hibernate.MappingException 当出现net.sf.hibernate.MappingException: Error r ...
- OpenSessionInViewFilter 的配置及替代方案(转)
鸣谢:http://justsee.iteye.com/blog/1174999,http://blog.csdn.net/sunsea08/article/details/4545186 Sprin ...
随机推荐
- TensorFlow知识总结
学习资料: 英文官方网站 Tensorflow 将要写的博客目录: 1.使用Spring AOP对异常进行统一处理 2.动态代理模式理解 aop中的动态代理模式 3.工厂模式三种的理解.logger ...
- Cats(4)- 叠加Free程序运算结果,Stacking monadic result types
在前面的几篇关于Free编程的讨论示范中我们均使用了基础类型的运算结果.但在实际应用中因为需要考虑运算中出现异常的情况,常常会需要到更高阶复杂的运算结果类型如Option.Xor等.因为Monad无法 ...
- css的三种样式
1.行间样式 顾名思义就是直接写在div对里面的样式 2.内部样式 把样式写在一对<style></style>标签对中 这个标签对是放在html页面里面的 3.外联样 ...
- 妈妈再也不用担心我找不到spring源码了!
获取spring源码: http://repo.springsource.org/libs-release-local/ http://repo.springsource.org/libs-relea ...
- ipa如何通过网络进行安装
苹果手机端应用,如果发布的到Appstore上,往往比较复杂,周期也比较长,Over-the-Air是Apple在 iOS4 中新加的一项技术,目的是让开发者能够脱离Appstore,实现从自己的服务 ...
- easyui弹出层在最顶层显示跳出iframe框架通用javascript代码
有时候我们用easyui在后台框架中弹框的时候,总是显示在框架页面里面而不是整个系统框架的上面,看着有些不太乐意. dialog = function (opts) { var query = par ...
- JavaScript中Array类型方法总结
Array类型是ECMAScript中最常用的类型之一,ECMAScript中的数组与其他多数语言中的数组有着相当大的区别.ECMAScript数组的每一项可以保存任何类型的数据.这里总结了数组类型的 ...
- iOS GCD的 一次性执行、定时器、迭代、队列组
- HotApp小程序统计之自定义事件统计
什么是自定义事件统计 官网:https://weixin.hotapp.cn/document 自定事件,就是自定统计任意事件的执行,灵活度最高. 用上图的云笔记说明想知道如下信息 (1)多少 ...
- Android 自定义线程池的实战
前言:在上一篇文章中我们讲到了AsyncTask的基本使用.AsyncTask的封装.AsyncTask 的串行/并行线程队列.自定义线程池.线程池的快速创建方式. 对线程池不了解的同学可以先看 An ...