首先配置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
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "">
- <hibernate-mapping package="cn.itcast.bean">
- <class name="Person" table="person">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="name" length="10" not-null="true"/>
- </class>
- </hibernate-mapping>
beans.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns=""
- xmlns:xsi=""
- xmlns:context=""
- xmlns:aop=""
- xmlns:tx=""
- xsi:schemaLocation="
- /spring-beans-2.5.xsd
- /spring-context-2.5.xsd
- /spring-aop-2.5.xsd
- /spring-tx-2.5.xsd">
- <context:annotation-config/>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="1"/>
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="500"/>
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="2"/>
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="1"/>
- </bean>
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>cn/itcast/bean/Person.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- 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
- </value>
- </property>
- </bean>
- <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <tx:annotation-driven transaction-manager="txManager"/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
- </beans>
被注入的类
- package cn.itcast.service.impl;
- import java.util.List;
- import javax.annotation.Resource;
- import org.hibernate.SessionFactory;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import cn.itcast.bean.Person;
- import cn.itcast.service.PersonService;
- @Transactional
- public class PersonServiceBean implements PersonService {
- @Resource private SessionFactory sessionFactory;
- //属于jdk规范,持久化,不用save(),
- public void save(Person person){
- sessionFactory.getCurrentSession().persist(person);
- }
-
- public void update(Person person){
- sessionFactory.getCurrentSession().merge(person);
- }
- //只是读取的话,就不用开启事务,会影响性能
- @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
- public Person getPerson(Integer personid){
- return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
- }
- public void delete(Integer personid){
- sessionFactory.getCurrentSession().delete(
- sessionFactory.getCurrentSession().load(Person.class, personid));//load比get没有数据封装过程,效率高一点
- }
- @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
- @SuppressWarnings("unchecked")
- public List<Person> getPersons(){
- return sessionFactory.getCurrentSession().createQuery("from Person").list();
- }
-
- }
阅读(638) | 评论(1) | 转发(0) |