SpringMVC 和 Mybatis 整合配置

版本说明:
Spring:3.2.5 Release
Mybatis:3.2.3
Mybatis-Spring:1.2.1
以下的整合方式不需要单独的 Mybatis 配置文件,本以为是必须的,折腾了不少时间!

1. Web.xml 中配置如下:

<!– load spring configuration –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>

<!– load log4j configuration –>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>

<!– 主要为了解决java.beans.Introspector导致内存泄漏 –>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!– Loads the Spring web application context –>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>characterEncoding</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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

除Dispatcher和编码过滤器外,ContextLoaderListener 是用来加载 Spring位于 src 目录下的配置文件,IntrospectorCleanupListener 主要为了解决java.beans.Introspector导致内存泄漏

2. 配置 WEB-INF 下的 Spring 配置文件,此处配置了 Controller 和 模版等相关信息

mvc-servlet.xml(名字与web.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:context=”http://www.springframework.org/schema/context”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd”>

<context:component-scan base-package=”com.dita.puzi.web.controller” />

<!–It registers several features, including JSR-303 validation support, message conversion, and support for field formatting–>
<mvc:annotation-driven />

<!– When using of the default servlet mapping it is also recommended to add this to your Spring MVC configuration,

which ensures that any resource requests not handled by your Spring MVC mappings will be delegated back to
the Servlet container –>
<mvc:default-servlet-handler/>

<bean id=”internalResourceViewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”prefix” value=”/WEB-INF/pages/”/>
<property name=”suffix” value=”.jsp”/>
</bean>

</beans>

3. 在 src 下的 Spring 配置文件中配置了 DataSource,并配置注解方式自动扫描 Bean,还有事务,连接池使用的事Druid(阿里出品),详细如下:

<?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:context=”http://www.springframework.org/schema/context” xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans

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

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

http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd”>

<context:component-scan base-package=”com.dita.puzi”>
<context:exclude-filter type=”annotation” expression=”org.springframework.stereotype.Controller” />
</context:component-scan>

<context:property-placeholder location=”classpath:dataSource.properties”/>

<!– dataSource use Druid –>
<bean id=”dataSource” class=”com.alibaba.druid.pool.DruidDataSource” init-method=”init” destroy-method=”close”>
<!– 基本属性 url、user、password –>
<property name=”url” value=”${jdbc.jdbcUrl}” />
<property name=”username” value=”${jdbc.username}” />
<property name=”password” value=”${jdbc.password}” />

<!– 配置初始化大小、最小、最大 –>
<property name=”initialSize” value=”${initialSize}” />
<property name=”minIdle” value=”${minIdle}” />
<property name=”maxActive” value=”${maxActive}” />

<!– 配置获取连接等待超时的时间 –>
<property name=”maxWait” value=”${maxWait}” />

<!– 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 –>
<property name=”timeBetweenEvictionRunsMillis” value=”${timeBetweenEvictionRunsMillis}” />

<!– 配置一个连接在池中最小生存的时间,单位是毫秒 –>
<property name=”minEvictableIdleTimeMillis” value=”${minEvictableIdleTimeMillis}” />

<property name=”validationQuery” value=”SELECT ‘x’” />
<property name=”testWhileIdle” value=”${testWhileIdle}” />
<property name=”testOnBorrow” value=”${testOnBorrow}” />
<property name=”testOnReturn” value=”${testOnReturn}” />

<!– 打开PSCache,并且指定每个连接上PSCache的大小 –>
<property name=”poolPreparedStatements” value=”${poolPreparedStatements}” />
<property name=”maxPoolPreparedStatementPerConnectionSize” value=”${maxPoolPreparedStatementPerConnectionSize}” />

<!– 配置监控统计拦截的filters –>
<property name=”filters” value=”${filters}” />
</bean>

<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>
<property name=”mapperLocations” value=”classpath*:mapper/*.xml” />
<property name=”typeAliasesPackage” value=”com.dita.puzi.entity” />
<property name=”dataSource” ref=”dataSource” />
</bean>

<bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.dita.puzi.dao” />
<property name=”annotationClass” value=”org.springframework.stereotype.Repository” />
</bean>

<!– 事务管理器 –>
<bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”dataSource” />
</bean>

<!– 事物传播特性 –>
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<tx:attributes>
<tx:method name=”alter*” propagation=”REQUIRED”/>
<tx:method name=”save*” propagation=”REQUIRED”/>
<tx:method name=”update*” propagation=”REQUIRED”/>
<tx:method name=”reset*” propagation=”REQUIRED”/>
<tx:method name=”insert*” propagation=”REQUIRED”/>
<tx:method name=”delete*” propagation=”REQUIRED”/>
<tx:method name=”batchDelete*” propagation=”REQUIRED”/>
<tx:method name=”assign*” propagation=”REQUIRED”/>
<tx:method name=”generate*” propagation=”REQUIRED”/>
<tx:method name=”clear*” propagation=”REQUIRED”/>

<tx:method name=”*” propagation=”SUPPORTS” read-only=”true”/>
</tx:attributes>
</tx:advice>

<!–service层全部有事物参与–>
<aop:config>
<aop:pointcut id=”servicePointcut” expression=”execution(* com.dita.puzi.service.*.*(..))”/>
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”servicePointcut” />
</aop:config>
</beans>

4. 上面 Mybatis 只需通过配置 org.mybatis.spring.SqlSessionFactoryBean 和 org.mybatis.spring.mapper.MapperScannerConfigurer 两个 Bean 即可,不需 Mybatis 配置文件,mapper 必须放在 resources 文件夹下(Intellij Idea 中是这样),否则会引发 Invalid bound statement 错误

DAO 的类加上@Repository

Service 的实现类加上@Service

Controller 的类加上@Controller 并 配置 URL 映射,用注解@RequestMapping 实现,可同时用于类和方法上。

由于实现同样的功能,可以有多种配置方式,再加上版本不同,以及自己对Spring 具体实现不甚了解,所以自己搞得时候有点绕弯。这种方式是完全基于注解,个人喜好可做修改。

——

累了,就休息一下