`
b_l_east
  • 浏览: 636855 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring2.5 + Struts2 + Hibernate3 整合 [2/2]

阅读更多

上一篇 Spring2.5 + Struts2 + Hibernate3 整合 [1/2]

1.      修改 spring 的配置文件,配置整合 hibernate

/WEB-INF/classes/spring.xml 中加入:

    <!-- 配置 dataSource -->

    < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" >

       < property name = "driverClassName" value = "com.mysql.jdbc.Driver" />

       < property name = "url" value = "jdbc:mysql://localhost:3306/dev?useUnicode=true&amp;characterEncoding=utf-8" />

       < property name = "username" value = "root" />

       < property name = "password" value = "root" />

       <!-- 连接池启动初始连接数 -->

       < property name = "initialSize" value = "1" />

       <!-- 连接池最大活动连接数 -->

       < property name = "maxActive" value = "50" />

       <!-- 最大空闲值 -->

       < property name = "maxIdle" value = "2" />

       <!-- 最小空闲值 -->

       < property name = "minIdle" value = "1" />

    </ bean >

    <!-- 整合 Hibernate3 sessionFactory -->

    < bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

       < property name = "dataSource" ref = "dataSource" />

       < property name = "mappingResources" >

           < list >

              < value > com/erison/action/Person.hbm.xml </ value >

           </ list >

       </ property >

       < property name = "hibernateProperties" >

           < props >

              < prop key = "hibernate.dialect" > org.hibernate.dialect.MySQL5Dialect </ prop >

              < prop key = "hibernate.show_sql" > false </ prop >

              < prop key = "hibernate.format_sql" > false </ prop >

           </ props >

       </ property >

    </ bean >

    <!-- 配置 spring 针对 hibernate 的事务管理器 -->

    < bean id = "txManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >

       < property name = "sessionFactory" ref = "sessionFactory" />

    </ bean >

    <!-- 配置使用注解的方式来使用事务 -->

    < tx:annotation-driven transaction-manager = "txManager" />

* 至此,整合 Hibernate3 已经完毕,要想使用还需进行以下步骤。

2.      创建类及测试

1.        创建 DTO com.erison.action.Person

package com.erison.action;

 

public class Person {

    private long id ;

    private String name ;

    private int age ;

    public long getId() {

       return id ;

    }

    public void setId( long id) {

       this . id = id;

    }

    public String getName() {

       return name ;

    }

    public void setName(String name) {

       this . name = name;

    }

    public int getAge() {

       return age ;

    }

    public void setAge( int age) {

       this . age = age;

    }

}

 

2.        创建 DB com.erison.action.DBPerson

package com.erison.action;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

 

public class DBPerson {

    HibernateTemplate template = null ;

    public void setSessionFactory(SessionFactory sessionFactory) {

       template = new HibernateTemplate(sessionFactory);

    }

   

    @Transactional (propagation=Propagation. REQUIRED )

    public void insert(Person person) {

       template .save(person);

       template .get(DBPerson. class , 1);

    }

   

    @Transactional (readOnly= true )

    public Person find( long

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics