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 ...
随机推荐
- 连接输出 如果存在在php中多次echo输出js的时候
- Lind.DDD.Manager里的3,7,15,31,63,127,255,511,1023,2047
回到目录 进制 我是一个程序猿,我喜欢简单的数字,十进制如何,数字太多,有10种数字组成,但由于它广为人知,所有使用最为广泛,人们的惯性思维培养了十进制,并说它是最容易被计算的数字,事实上,在计算机里 ...
- JMeter专题系列(二)录制脚本
环境 Badboy version 2.1.1 JDK: 1.7.0_67 Apache JMeter-2.11 ----------------------------------------- ...
- MySQL中CURRENT_TIMESTAMP(转)
1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp() 代码如下 mysql> select current_timestamp ...
- [翻译]用 Puppet 搭建易管理的服务器基础架构(3)
我通过伯乐在线翻译了一个Puppet简明教程,一共分为四部分,这是第三部分. 本文由 伯乐在线 - Wing 翻译,黄利民 校稿.未经许可,禁止转载!英文出处:Manuel Kiessling.欢迎加 ...
- 工行ATM转账——事务操作
今儿去工行ATM给已朋友转账,遇到这么个情况: 选择对外转账后输入转入账号(输入两次),接着提示输入转入金额(输入一次金额),按确定,系统提示交易中,3秒左右,提示“输入账号无效”,系统自动中断了操作 ...
- FIS3的简单使用
序言: 最近在收集前端的优化工具,随便一搜,厉害了word哥,有grunt.gulp.FIS3.webpack等等,简直就是眼花缭乱!前辈们对于他们的评价各有千秋,于是乎就想每个都来用一遍(之前已经倒 ...
- 轻松掌握:JavaScript策略模式
策略模式 定义:定义一系列的算法,把它们一个个封装成函数,也可把它们作为属性统一封装进一个对象,然后再定义一个方法,该方法可根据参数自动选择执行对应的算法. 一般用于在实现一个功能时,有很多个方案可选 ...
- IIS10中使用OpenSSL来创建CA并且签发SSL证书
参考: http://www.cnblogs.com/lierle/p/5140187.html http://alvinhu.com/blog/2013/06/12/creating-a-certi ...
- Sanboxie 5.14安装图解
Sanboxie, 即沙盘,引用官方解释:电脑就像一张纸,程序的运行与改动,就像将字写在纸上.而Sandboxie就相当于在纸上放了块玻璃,程序的运行与改动就像写在了那块玻璃上,除去玻璃,纸上还是一点 ...