Spring3+mybatis+mysql整合详解(三)

February 11, 2015

继续阅读

下面来看Spring的配置:

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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
    <!-- DAO配置 -->
    <import resource="daoContext.xml" />

    <!-- IoC配置 -->
    <import resource="iocContext.xml" />

    <!-- MVC配置 -->
    <import resource="mvcContext.xml" />

    <!-- AOP配置 -->
    <import resource="aopContext.xml" />
</beans>

daoContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    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-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/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 配置数据源 -->
    <bean id="DataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myfristdb" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>

    <!-- Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。 -->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        <property name="dataSource" ref="DataSource" />
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="TransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="DataSource" />
    </bean>
</beans>

该部分为Mybatis和Spring3的整合配置,包括数据源、SqlSessionFactory及事务管理器的配置。

iocContext.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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <!-- 数据映射器类  mapper bean -->
    <bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="SqlSessionFactory" />
        <!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类  -->
        <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" />
    </bean>

    <bean id="LoginMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="SqlSessionFactory" />
        <property name="mapperInterface" value="com.hl.usersmanager.dao.ILoginMapper" />
    </bean>
</beans>

这部分是Dao层bean的配置。注意,这里bean的class并非直接指向dao层的类,而是指向org.mybatis.spring.mapper.MapperFactoryBean,并将dao层接口IUserMapper作为MapperFactoryBean的mapperInterface属性。 mybatis会自动帮我们创建一个代理类,执行IUserMapper里面的方法。也就是说,只要我们写好了mybatis的SqlMapConfig.xml,dao层只需要一个接口Mapper就行了,甚至连实现类都不要,因为myBatis自动帮我们完成。强大吧!
当然必须为MapperFactoryBean指定一个sqlSessionFactory,那就是前面我们在daoContext.xml中配置的SqlSessionFactory。

mvcContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    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-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/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 参考资料:http://code.google.com/p/bounding/wiki/SpringMVC3 -->

    <!-- 国际化配置 参考:http://hi.baidu.com/sonmeika/blog/item/8069b2dd7db1c9395882dd29.html 
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
    </bean> -->

    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"/>  
    <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.InternalResourceView</value>
        </property>
    </bean>

    <!-- 注解支持 -->  
    <mvc:annotation-driven/>

    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="com.hl.usersmanager">
        <!-- 允许定义过滤器将基包下的某些类纳入或排除
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
    </context:component-scan>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 
    <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->

</beans>

这部分是Spring mvc的配置。 viewResolver视图解析器是必须的。其属性prefix表示某个视图指向到那个路径,suffix属性表示视图的后缀(如果配置为jsp则表示该视图使用jsp页面进行渲染)。例如某个Controller返回一个login视图,根据当前的配置,表示该视图使用/WEB-INF/page/login.jsp进行渲染。这一点跟struts2有点像.
由于我们使用了注解的方式声明Controller,所以我们不再需要在Spring中配置Controller的bean。首先,开启注解支持,然后知道需要扫描注解的路径。

aopContext.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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!-- aop注解支持 -->
    <aop:aspectj-autoproxy/>

    <!-- 
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />-->

    <!-- 拦截器 -->
    <bean id="UserInterceptor" class="com.hl.usersmanager.controller.aop.UserInterceptor"></bean>
</beans>

这里是Spring aop的配置。首先需要开启注解支持。.AnnotationAwareAspectJAutoProxyCreator可以自动完成aop 注解类的加载,但测试时发现跟spring mvc似乎有些冲突,没有测试成功。好吧,只能老老实实的把每个拦截器bean配置起来。