首先搭建建构

引入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. css遮罩代码(已验证)

    #mask { background-color: rgb(0, 0, 0); display:none; opacity: 0.0; /* Safari, Opera */ -moz-opacity ...

  2. 【转】zigbee协议的多种profile

  3. yii2 控制器、方法命名规范和访问路由

    如果模块名称或者控制器名称或者动作名称是用的骆驼格式的命名写法,那么路由里面的每个大写单词之间都要用“-”来连接.如 DateTimeController::actionFastForward 相应的 ...

  4. 高性能 Socket 组件 HP-Socket v3.2.1 正式发布

    HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C ...

  5. 利用CSS3中transparent实现三角形及三角形组合图

    ??如何绘制三角形及三角形的组合图案,以下是自己画的草图 源码如下 <!DOCTYPE html> <html lang="en"> <head> ...

  6. vue+ vue-router + webpack 踩坑之旅

    说是踩坑之旅 其实是最近在思考一些问题 然后想实现方案的时候,就慢慢的查到这些方案   老司机可以忽略下面的内容了 1)起因  考虑到数据分离的问题  因为server是express搭的   自然少 ...

  7. iOS---A valid provisioning profile for this executable was not found

    把Target中的Code Signing Identity也设置成iPhone Develop就ok了,这样一切都说的通了,唯一不合理的就是在Project切换Code Signing Identi ...

  8. [css]我要用css画幅画(九) - Apple Logo

    接着之前的[css]我要用css画幅画(八) - Hello Kitty,这次画的是苹果公司的logo 这次打算将分析和实现步骤尽量详细的说一说. 其实之前的也打算详细讲分析和设计过程,不过之前的图比 ...

  9. PS技巧:如何优雅的抠公章?

    搞设计的很苦逼,整天面对各种各样任务,除了修图.排版外,还时不时会有些另类需求.这时如果掌握一些小技巧就不用临时抱佛脚啦. 下面献上一计:教大家怎么用PS抠公章.有需要的拿去,PS:不要干坏事吆! 效 ...

  10. Linux SSH登录慢案例分析

    手头有台Linux服务器ssh登录时超级慢,需要几十秒.其它服务器均没有这个问题.平时登录操作都默默忍了.今天终于忍不住想搞清楚到底什么原因.搜索了一下发现了很多关于ssh登录慢的资料,于是自己也学着 ...