Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2879874
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Java

2012-03-13 17:12:15

首先配置beans.xml

 ..... 略
用到hibernate的LocalSessionFactoryBean,来创建session访问数据库

    
    
    
       cn/itcast/bean/Person.hbm.xml
    

    

    
    
         hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
         hibernate.hbm2ddl.auto=update//更新表结构,这样就可以直接创建表的结构,在开发阶段
         hibernate.show_sql=false
         hibernate.format_sql=false
      

   




   


针对二级缓存的配置
 

Hibernate二级缓存的配置

sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

     dataSource" ref="dataSource"/>

   mappingResources">

     

        cn/itcast/bean/Person.hbm.xml

     

   

     hibernateProperties">

     

          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

          hibernate.hbm2ddl.auto=update

          hibernate.show_sql=false

          hibernate.format_sql=false

          hibernate.cache.use_second_level_cache=true

                 hibernate.cache.use_query_cache=false

                 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

       

    


 
集成例子:
实体bean--Person.hbm.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.         "">
  5. <hibernate-mapping package="cn.itcast.bean">
  6.     <class name="Person" table="person">
  7.         <id name="id">
  8.             <generator class="native"/>
  9.         </id>
  10.         <property name="name" length="10" not-null="true"/>
  11.     </class>
  12. </hibernate-mapping>
beans.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:context=""
  5.        xmlns:aop=""
  6.        xmlns:tx=""
  7.        xsi:schemaLocation="
  8.            /spring-beans-2.5.xsd
  9.             /spring-context-2.5.xsd
  10.             /spring-aop-2.5.xsd
  11.             /spring-tx-2.5.xsd">
  12.      <context:annotation-config/>
  13.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  14.      <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
  15.      <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
  16.      <property name="username" value="root"/>
  17.      <property name="password" value="123456"/>
  18.      <!-- 连接池启动时的初始值 -->
  19.          <property name="initialSize" value="1"/>
  20.          <!-- 连接池的最大值 -->
  21.          <property name="maxActive" value="500"/>
  22.          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  23.          <property name="maxIdle" value="2"/>
  24.          <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  25.          <property name="minIdle" value="1"/>
  26.      </bean>
  27.     
  28.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  29.      <property name="dataSource" ref="dataSource"/>
  30.          <property name="mappingResources">
  31.          <list>
  32.          <value>cn/itcast/bean/Person.hbm.xml</value>
  33.          </list>
  34.          </property>
  35.      <property name="hibernateProperties">
  36.          <value>
  37.          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  38.          hibernate.hbm2ddl.auto=update
  39.          hibernate.show_sql=false
  40.          hibernate.format_sql=false
  41.          hibernate.cache.use_second_level_cache=true
  42.             hibernate.cache.use_query_cache=false
  43.              hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
  44.          </value>
  45.      </property>
  46.     </bean>
  47.     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  48.          <property name="sessionFactory" ref="sessionFactory"/>
  49.     </bean>
  50.     <tx:annotation-driven transaction-manager="txManager"/>
  51.     <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
  52. </beans>

被注入的类

点击(此处)折叠或打开

  1. package cn.itcast.service.impl;

  2. import java.util.List;

  3. import javax.annotation.Resource;

  4. import org.hibernate.SessionFactory;
  5. import org.springframework.transaction.annotation.Propagation;
  6. import org.springframework.transaction.annotation.Transactional;

  7. import cn.itcast.bean.Person;
  8. import cn.itcast.service.PersonService;

  9. @Transactional
  10. public class PersonServiceBean implements PersonService {
  11.     @Resource private SessionFactory sessionFactory;

  12.     //属于jdk规范,持久化,不用save(),

  13.     public void save(Person person){
  14.         sessionFactory.getCurrentSession().persist(person);
  15.     }
  16.     
  17.     public void update(Person person){
  18.         sessionFactory.getCurrentSession().merge(person);
  19.     }
  20.     //只是读取的话,就不用开启事务,会影响性能

  21.     @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
  22.     public Person getPerson(Integer personid){
  23.         return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
  24.     }

  25.     public void delete(Integer personid){
  26.         sessionFactory.getCurrentSession().delete(
  27.                 sessionFactory.getCurrentSession().load(Person.class, personid));//load比get没有数据封装过程,效率高一点

  28.     }
  29.     @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
  30.     @SuppressWarnings("unchecked")
  31.     public List<Person> getPersons(){        
  32.         return sessionFactory.getCurrentSession().createQuery("from Person").list();
  33.     }
  34.     
  35. }


 
阅读(643) | 评论(1) | 转发(0) |
0

上一篇:事务传播属性

下一篇:hibernate二级缓存

给主人留下些什么吧!~~

nba76ers2012-03-13 17:32:48

这里可以用bibernatesupport那个类来访问数据库