详解MyBatis多数据源配置(读写分离)
|
MyBatis多数据源配置(读写分离) 首先说明,本文的配置使用的最直接的方式,实际用起来可能会很麻烦。 实际应用中可能存在多种结合的情况,你可以理解本文的含义,不要死板的使用。 多数据源的可能情况 1.主从 通常是MySQL一主多从的情况,本文的例子就是主从的情况,但是只有两个数据源,所以采用直接配置不会太麻烦,但是不利于后续扩展,主要是作为一个例子来说明,实际操作请慎重考虑。 2.分库 当业务独立性强,数据量大的时候的,为了提高并发,可能会对表进行分库,分库后,每一个数据库都需要配置一个数据源。 这种情况可以参考本文,但是需要注意每一个数据库对应的Mapper要在不同的包下方便区分和配置。 另外分库的情况下也会存在主从的情况,如果你的数据库从库过多,就参考上面提供的方法,或者寻找其他方式解决。 Mapper分包 分库的情况下,不同的数据库的Mapper一定放在不同的包下。 主从的情况下,同一个Mapper会同时存在读写的情况,创建两个并不合适,使用同一个即可。但是这种情况下需要注意,Spring对Mapper自动生成的名字是相同的,而且类型也相同,这是就不能直接注入Mapper接口。需要通过SqlSession来解决。 Spring基础配置 applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.isea533.mybatis.service"/>
<context:property-placeholder location="classpath:config.properties"/>
<aop:aspectj-autoproxy/>
<import resource="spring-datasource-master.xml"/>
<import resource="spring-datasource-slave.xml"/>
</beans>
这个文件,主要是引入了spring-datasource-master.xml和spring-datasource-slave.xml。 spring-datasource-master.xml
<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.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">
<bean id="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${master.jdbc.driverClass}"/>
<property name="url" value="${master.jdbc.url}"/>
<property name="username" value="${master.jdbc.user}"/>
<property name="password" value="${master.jdbc.password}"/>
<property name="filters" value="stat"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="1"/>
<property name="maxWait" value="60000"/>
<property name="minIdle" value="1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
</bean>
<bean id="sqlSessionFactory1"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceMaster"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
</bean>
<bean id="sqlSessionMaster" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory1"/>
</bean>
<aop:config>
<aop:pointcut id="appService"
expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>
<aop:advisor advice-ref="txAdvice1" pointcut-ref="appService"/>
</aop:config>
<tx:advice id="txAdvice1" transaction-manager="transactionManager1">
<tx:attributes>
<tx:method name="select*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="transactionManager1"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceMaster"/>
</bean>
</beans>
spring-datasource-slave.xml 和master区别不大,主要是id名字和数据源配置有区别。
<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.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">
<bean id="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${slave.jdbc.driverClass}"/>
<property name="url" value="${slave.jdbc.url}"/>
<property name="username" value="${slave.jdbc.user}"/>
<property name="password" value="${slave.jdbc.password}"/>
<property name="filters" value="stat"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="1"/>
<property name="maxWait" value="60000"/>
<property name="minIdle" value="1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
</bean>
<bean id="sqlSessionFactory2"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceSlave"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
</bean>
<bean id="sqlSessionSlave" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory2"/>
</bean>
<aop:config>
<aop:pointcut id="appService"
expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>
<aop:advisor advice-ref="txAdvice2" pointcut-ref="appService"/>
</aop:config>
<tx:advice id="txAdvice2" transaction-manager="transactionManager2">
<tx:attributes>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<bean id="transactionManager2"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceSlave"/>
</bean>
</beans>
这里需要注意<tx:method name="*" read-only="true"/>是只读的。如果不是从库,可以按主库进行配置。 (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java使用Hex编码解码实现Aes加密解密功能示例
- Mybatis3 if判断字符串变态写法
- java中double类型运算结果异常的解决方法
- Java中终止线程的三种方法
- java – 有什么问题:LinkedList stringList = new LinkedL
- javaFX应用程序错误:未指定资源
- java – 永远不会被分配为null的volatile变量永远不会包含n
- java – 获取LDAP对象的内部属性
- java.lang.IllegalStateException:在onSaveInstanceState之
- java – JAXB,CXF:没有ObjectFactory与@XmlElementDecl的元
