Spring+Hibernate:使用注释和OSCache整合的简单实例

来源:互联网 发布:安卓windows开始键图标 编辑:程序博客网 时间:2024/05/21 02:50

1.web.xml配置

见本博客中《Spring:使用JdbcTemplate的简单实例-基于注释》一文中web.xml配置

 

2. applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:jee="http://www.springframework.org/schema/jee"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

                     http://www.springframework.org/schema/tx

                     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

                     http://www.springframework.org/schema/jee

                     http://www.springframework.org/schema/jee/spring-jee-2.5.xsd

                     http://www.springframework.org/schema/context

                     http://www.springframework.org/schema/context/spring-context-2.5.xsd

                     http://www.springframework.org/schema/aop

                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

   

    <context:component-scan base-package="com.lz"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="viewResolver"

       class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <property name="viewClass">

           <value>org.springframework.web.servlet.view.JstlView</value>

       </property>

       <property name="prefix">

           <value>/</value>

       </property>

       <property name="suffix">

           <value>.jsp</value>

       </property>

    </bean>

   

    <bean id="dataSource"

       class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName"

       value="oracle.jdbc.driver.OracleDriver">

       </property>

       <property name="url"

       value="">

       </property>

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

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

    </bean>

   

    <!--

    <bean id="dataSource"

       class="org.springframework.jndi.JndiObjectFactoryBean">

       <property name="jndiName" value="java:comp/env/jdbc/Oracle" />

    </bean>

    -->

   

    <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

       <property name="dataSource">

           <ref bean="dataSource" />

       </property>

       <property name="packagesToScan">

           <list>

              <value>com.lz.model</value>              

           </list>

       </property>

       <!--

       <property name="annotatedClasses">

           <list>

              <value>com.lz.model.Test</value>

           </list>

       </property>

       -->

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.Oracle9iDialect

              </prop>

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

              <prop key="hibernate.hbm2ddl.auto">none</prop>

              <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>

              <prop key="hibernate.cache.use_second_level_cache">true</prop>

              <prop key="hibernate.cache.use_query_cache">true</prop>

              <prop key="hibernate.jdbc.batch_size">20</prop>

           </props>

       </property>

    </bean>

   

    <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

       <tx:attributes>

           <tx:method name="get*" read-only="true"></tx:method>

           <tx:method name="find*" read-only="true"></tx:method>

           <tx:method name="*"></tx:method>

       </tx:attributes>

    </tx:advice>

    <aop:config proxy-target-class="true">

       <aop:advisor

           pointcut="execution(* com.lz.service.impl.*Impl.*(..))"

           advice-ref="txAdvice"></aop:advisor>

    </aop:config>

</beans>

 

3. Test.java

package com.lz.model;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.SequenceGenerator;

import javax.persistence.Table;

 

import org.hibernate.annotations.Cache;

import org.hibernate.annotations.CacheConcurrencyStrategy;

 

/**

 * Test entity.

 *

 * @author MyEclipse Persistence Tools

 */

@Entity

@Table(name = "TEST", schema = "TEST")

@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)

public class Test implements java.io.Serializable {

 

    // Fields

 

    /**

     *

     */

    private static final long serialVersionUID = 6570590473361425690L;

    private Long testId;

    private String testName;

 

    // Constructors

 

    /** default constructor */

    public Test() {

    }

 

    /** minimal constructor */

    public Test(Long testId) {

       this.testId = testId;

    }

 

    /** full constructor */

    public Test(Long testId, String testName) {

       this.testId = testId;

       this.testName = testName;

    }

 

    // Property accessors

    @Id

    @SequenceGenerator(name = "TEST_SEQ", sequenceName = "TEST_SEQ", allocationSize=1)

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="TEST_SEQ")

    @Column(name = "TEST_ID", unique = true, nullable = false, precision = 22, scale = 0)

    public Long getTestId() {

       return this.testId;

    }

 

    public void setTestId(Long testId) {

       this.testId = testId;

    }

 

    @Column(name = "TEST_NAME", length = 50)

    public String getTestName() {

       return this.testName;

    }

 

    public void setTestName(String testName) {

       this.testName = testName;

    }

 

    @Override

    public int hashCode() {

       final int prime = 31;

       int result = 1;

       result = prime * result

              + ((testName == null) ? 0 : testName.hashCode());

       result = prime * result + ((testId == null) ? 0 : testId.hashCode());

       return result;

    }

 

    @Override

    public boolean equals(Object obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       final Test other = (Test) obj;

       if (testName == null) {

           if (other.testName != null)

              return false;

       } else if (!testName.equals(other.testName))

           return false;

       if (testId == null) {

           if (other.testId != null)

              return false;

       } else if (!testId.equals(other.testId))

           return false;

       return true;

    }

}

 

4. HibernateBaseDao.java

package com.lz.dao.base;

 

import javax.annotation.Resource;

 

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

public class HibernateBaseDao extends HibernateDaoSupport {

    @Resource(name="sessionFactory")

    public void setSuperSessionFactory(SessionFactory sessionFactory){

        super.setSessionFactory(sessionFactory);

    }

}

 

5. TestDao.java

package com.lz.dao;

 

import java.util.List;

 

import com.lz.model.Test;

 

public interface TestDao {

    public List<Test> find();

}

 

6. TestDaoImpl.java

package com.lz.dao.impl;

 

import java.util.List;

 

import org.springframework.stereotype.Repository;

 

import com.lz.dao.TestDao;

import com.lz.dao.base.HibernateBaseDao;

import com.lz.model.Test;

 

@Repository("testDao")

public class TestDaoImpl extends HibernateBaseDao implements TestDao {

    @SuppressWarnings("unchecked")

    public List<Test> find() {

       getHibernateTemplate().setCacheQueries(true);

       return getHibernateTemplate().find("from Test");

    }

}

 

7. TestService.java

package com.lz.service;

 

import java.util.List;

 

import com.lz.model.Test;

 

public interface TestService {

    List<Test> find();  

}

 

8. TestServiceImpl.java

package com.lz.service.impl;

 

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import com.lz.dao.TestDao;

import com.lz.model.Test;

import com.lz.service.TestService;

 

@Service("testService")

public class TestServiceImpl implements TestService {

 

    @Autowired

    private TestDao testDao;

   

    public List<Test> find() {

       return testDao.find();

    }

}

 

9. TestController.java

package com.lz.web;

 

import javax.servlet.http.HttpServletRequest;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

import com.lz.service.TestService;

 

@Controller

@RequestMapping("/test.html")

public class TestController {

   

    @Autowired

    private TestService testService;

   

    private String formView = "test";

   

    @RequestMapping(params = "method=find")

    public String find(HttpServletRequest request) {

       request.setAttribute("testList", testService.find());

       return formView;

    }

}

 

10. oscache.properties

cache.capacity=10