springMvc + mybatis 配置

2017-02-17 11:09

拾人牙慧,整理为个人备份,对mybatis的各种配置参数比较感兴趣。


<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    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/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
                        
    <import resource="spring-jcaptcha.xml" />                        
    <!-- jdbc.properties文件路径  -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:/com.test/config/jdbc.properties" />
    </bean>      
    <!-- 数据源的配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- data source configuration -->
        <property name="initialSize" value="20" /><!-- initial connections -->
        <property name="maxActive" value="50" /><!-- MAX connections -->
        <property name="maxIdle" value="20" /><!-- MAX idle connections -->
        <property name="minIdle" value="10" /><!-- MIN idle connections -->
        
    </bean>
    <!--事务相关控制-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>     
    <context:component-scan  base-package="com.test"/>
    <!--启动注解 @Transactional -->
    
    <tx:annotation-driven></tx:annotation-driven>
   
    <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.test.service.impl.*.*(..)) " />
                <!-- 把事务控制在Business层 -->
        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
    </aop:config>

    <!-- MyBatis sqlSessionFactory 配置 mybatis-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--统一配置加载mapper.xml 包路径,这样可以少写配置代码,有新的mapper加入也不需要修改配置 --/>
       <property name="mapperLocations" value="classpath:com.test/mapper/*.xml" />  

        <!-- 给映射的类配置别名 ,这样所有的mapper中就不用写全路径,使用别名就行了,这配置下,实体类的别名是第一个字母小写的类名-->
            <property name="typeAliasesPackage" value="com.test.domain" />   

        <property name="dataSource" ref="dataSource" />
    </bean>    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    
    <!-- 自动扫描mapper接口,不在需要写实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
            p:basePackage="com.test.mapper"
            p:sqlSessionFactoryBeanName="sqlSessionFactory" />
            
            
    <!-- 设置字段内容长度,这里不做限定   -->    
    <bean id="mappingJacksonHttpMessageConverter"  
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        
    
    <!--