首先搭建建构

引入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模式的更多相关文章

  1. [转]OpenSessionInView模式

    OpenSessionInView模式解决的问题:   * hibernate事物边界问题   * 因session关闭导致hibernate延迟加载例外的问题 事物边界:     一个事物的完成应该 ...

  2. SSH第一篇【整合SSH步骤、OpenSessionInView】

    前言 到目前为止,Struts2.Hibernate.Spring框架都过了一遍了.也写过了Spring怎么与Struts2整合,Spring与Hibernate整合-本博文主要讲解SSH的整合 整合 ...

  3. 【转】Hibernate 常见异常

    转载地址:http://smartan.iteye.com/blog/1542137 Hibernate 常见异常net.sf.hibernate.MappingException        当出 ...

  4. Struts2,Spring, Hibernate三大框架SSH的整合步骤

    整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...

  5. hibernate中load和get方法的区别

    1.读取时机不同(当lazy=true的时候)    load是采用延迟机制(load语句不读库,等使用非主键时才去读库),而get不采用延  迟机制(get语句时马上读库): 2.搜索不到数据时的情 ...

  6. struts2支持的结果类型

    在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这 些result-ty ...

  7. Spring 管理数据源

    Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...

  8. Hibernate 常见异常

    Hibernate 常见异常net.sf.hibernate.MappingException        当出现net.sf.hibernate.MappingException: Error r ...

  9. OpenSessionInViewFilter 的配置及替代方案(转)

    鸣谢:http://justsee.iteye.com/blog/1174999,http://blog.csdn.net/sunsea08/article/details/4545186 Sprin ...

随机推荐

  1. Tomcat启动报错整理

    1.启动报 Connector attribute SSLCertificateFile must be defined when using SSL with APR conf\server.xml ...

  2. 开窗函数使用及sql自行构建枚举数据用于关联

    1, SELECT  * FROM    ( SELECT    ROW_NUMBER() OVER ( PARTITION BY process_instance_id (区分相似数据的字段,逗号分 ...

  3. javascript中BOM部分基础知识总结

    一.什么是BOM      BOM(Browser Object Document)即浏览器对象模型.      BOM提供了独立于内容 而与浏览器窗口进行交互的对象:      由于BOM主要用于管 ...

  4. 去除GridView选中时的蓝色背景

    解决办法: android:listSelector="#00000000" android:listSelector="@android:color/transpare ...

  5. CSS的一些基础知识

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>标题&l ...

  6. Activity详解二 activity数据传递

    首先看效果图: 1.Bundle类的作用 Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值.相对于Map,它提供了各种常用类型的putXxx()/getXxx()方 ...

  7. 【读书笔记】100个Switf必备tips

    声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.Selector 在Swi ...

  8. Java中的Atomic包

    Atomic包的作用 方便程序员在多线程环境下,无锁的进行原子操作 Atomic包核心 Atomic包里的类基本都是使用Unsafe实现的包装类,核心操作是CAS原子操作: 关于CAS compare ...

  9. maven工程模块化

    前言 项目的模块化有利于任务分工,后期维护,易扩展,模块还可以独立成服务单独部署等: 创建packaging类型为POM的父项目 我用的maven插件是m2e,相信大部分人在eclipse装的也是m2 ...

  10. [SSIS] 在脚本里面使用数据库连接字符串进行查询等处理, 入坑

    入坑.!!!!! SSIS 中dts包 设置的  ADO.Net连接, 在传入脚本的时候, 我要使用 数据库连接,进行数据的删除操作. 于是我使用了 了如下的 代码 使用的是windows 身份验证, ...