最近由于工作需要,公司要求新项目要升级到spring4 + hibernate4。按照spring3 + hibernate3时代的配置,走到数据库访问层,由于抛弃了HibernateTemplate方式,只能直接使用Session。然而简单修改配置后,出现了getCurrentSession()报错的问题。网上查阅了好久,发现提出的解决方式都是不用getCurrentSession(),改用openSession()。博主就较劲了,既然不能用,为什么会提供这种方式,而如果使用MyEclipse最新版的话,自动生成的DAO里面也是用的getCurrentSession()方式,然后根据spring的文档,翻阅源码等方式,最终配置成了可以使用getCurrentSession()的配置方式。
以上都是废话。下面上配置文件。
首先web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>security</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>charEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param><!-- 在这里,使用了不同的配置文件,即MVC方面,单独使用一个配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
进行MVC配置的appServlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:s="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<annotation-driven />
<!-- 对于静态资源,js、css、images等访问,全部映射到/resources/下的目录 -->
<resources mapping="/js/**" location="/resources/js/" />
<resources mapping="/css/**" location="/resources/css/" />
<resources mapping="/images/**" location="/resources/images/" />
<resources mapping="/resources/**" location="/resources/" />
<!-- 配置展示页面为jsp,全部放在/WEB-INF/views/目录下 -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- 由于将spring content和spring MVC分配置文件设置,所以这里就只扫描MVC中controller所在的包
其他包的内容交给spring content来处理。 -->
<context:component-scan base-package="org.nercita.security.controllers" />
</beans:beans>
进行content配置的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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:s="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 数据库连接的配置放到hibernate.cfg.xml中去了 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<!-- 配置sessionFactory扫描models包下的所有类。 -->
<property name="packagesToScan" value="org.nercita.security.db.models" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 将所有的get前缀和find前缀的查询,如果有事务则以当前事务提交,没有事务就以非事务方式提交查询
并且查询结果设定为只读 -->
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<!-- 除get和find前缀的方法外,全部按照事务方式提交查询,并且设置结果非只读 -->
<tx:method name="*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 下面的dao访问切片设置进行事务管理和dao类前面的@Transactional注解是否全是必须添加未进行验证,稳妥起见就都加上了。 -->
<aop:config expose-proxy="true">
<aop:advisor advice-ref="txAdvice" pointcut="execution(* org.nercita.security.db.daos..*(..))" />
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true" />
<tx:annotation-driven />
<context:component-scan base-package="org.nercita.security" />
</beans>
数据库配置,hibernate.cfg.xml,此文件放在classpath中的根目录。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://192.168.9.53:3306/test_security?useUnicode=true&characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.timeout">5000</property>
<!-- 以上配置应该不用细说了,关键点就是使用是连接池,可以不用c3p0, 使用proxool之类的也行,只是一定要用连接池,否则hibernate会报错,
因为在applicationContext中没有配置数据源,spring会用默认的javax.sql.DataSource作为数据源,此数据源不支持事务,所以会抛出
异常Cannot unwrap to requested type [javax.sql.DataSource] -->
</session-factory>
</hibernate-configuration>
DAO的访问类
package org.nercita.security.db.daos;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;
import org.nercita.security.db.models.Account;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/* 此处要增加事务的注解,以便spring在执行到这里是,将session查询注入事务的管理。 */
@Transactional
@Repository
public class AccountDAO {
private static final Logger log = LoggerFactory.getLogger(AccountDAO.class);
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
protected void initDao() {
// do nothing
}
public Account findById(java.lang.Integer id) {
log.debug("getting Account instance with id: " + id);
try {
//所有的查询均可以使用getCurrentSession()来进行。无需对session进行显式的open和close操作,spring的事务管理会自动的打开和关闭。
Account instance = (Account) getCurrentSession().get(Account.class, id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
}
关于jar包的问题,博主使用的maven,以下是引入的内容
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>