时间:2021-07-01 10:21:17 帮助过:11人阅读
<!-- ========================================配置数据源========================================= --> <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 --> <bean name="pepos" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${pepos.url}" /> <property name="username" value="${pepos.username}" /> <property name="password" value="${pepos.password}" /> <!-- 密码解密--> <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${pepos.publickey}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 <property name="maxIdle" value="20" /> --> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="validationQuery" value="SELECT 1" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> </bean> <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 --> <bean name="payment" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${payment.url}" /> <property name="username" value="${payment.username}" /> <property name="password" value="${payment.password}" /> <!-- 密码解密 --> <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${payment.publickey}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 <property name="maxIdle" value="20" /> --> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="validationQuery" value="SELECT 1" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> </bean> <!-- 需要基础spring的 AbstractRoutingDataSource 来实现动态设置数据库连接池 --> <bean id="dynamicDataSource" class="com.threeweidu.mallmanage.utils.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="pepos" key="pepos"></entry> <entry value-ref="payment" key="payment"></entry>[java] view plain copy print?
</map> </property> <property name="defaultTargetDataSource" ref="pepos"> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dynamicDataSource"></property> </bean> <!-- MyBatis配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dynamicDataSource" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="typeAliasesPackage" value="com.threeweidu.mallmanage.entity" /> <!-- 显式指定Mapper文件位置 --> <property name="mapperLocations" value="classpath:/com/threeweidu/mallmanage/dao/mybatis/mapper/*Mapper.xml" /> </bean> <!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 --> <property name="basePackage" value="com.threeweidu.mallmanage.dao.mybatis" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 配置Spring的事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dynamicDataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 拦截器方式配置事务 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="load*" propagation="REQUIRED" read-only="true"/> <tx:method name="init*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="save*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="add*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="register*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="update*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="delete*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="remove*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.threeweidu.mallmanage.service..*Impl.*(..))"/> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/> </aop:config> </beans>
多个sqlsessionFactroy 模式
spring-mybatis.xml 配置文件
[java] view plain copy print?
<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd "> <import resource="spring-mybatis-shop.xml" /> <import resource="spring-mybatis-payment.xml" /> <!-- <import resource="spring-mybatis-pepos.xml" /> <import resource="spring-mybatis-peposlog.xml" /> <import resource="spring-mybatis-peposchat.xml" /> <import resource="spring-mybatis-vaservice.xml" /> --></beans>
spring-mybatis-payment.xml 配置
[java] view plain copy print?<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd "> <!-- 数据库连接池 --> <bean id="dataSource_payment" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${payment.url}" /> <property name="username" value="${payment.username}" /> <property name="password" value="${payment.password}" /> <!-- 密码解密 --> <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${payment.publickey}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="3" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="200" /> <!-- 连接池最大空闲 <property name="maxIdle" value="20" /> --> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="validationQuery" value="SELECT 1" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> </bean> <!-- MyBatis配置 --> <bean id="sqlSessionFactory_payment" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource_payment" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="typeAliasesPackage" value="com.threeweidu.supplier.entity" /> <!-- 显式指定Mapper文件位置 --> <property name="mapperLocations" value="classpath:/com/threeweidu/supplier/dao/mybatis/payment/mapper/*Mapper.xml" /> </bean> <!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 --> <property name="basePackage" value="com.threeweidu.supplier.dao.mybatis.payment" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_payment" /> </bean> <!-- 配置Spring的事务管理器 --> <bean id="transactionManager_payment" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource_payment" /> </bean> <tx:annotation-driven transaction-manager="transactionManager_payment" proxy-target-class="true" /> <!-- 拦截器方式配置事务 --> <tx:advice id="transactionAdvice_payment" transaction-manager="transactionManager_payment"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/>