疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Spring+Mybatis读写分离


 

 
说明
 
mybatis插件+扩展Spring数据源是最省力的方式,不需要改动任何原代码,如果是数据库事务,则插件不生效,不会对数据库事务造成影响
 
spring配置
 
<bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource"
在mybatis配置文件中配置插件
 
<configuration>
mybatis读写分离插件源码MyDynamicDataSourcePlugin.java
 
package com.main.common.mybatis;import org.apache.ibatis.executor.Executor;import org.apache.ibatis.executor.keygen.SelectKeyGenerator;import org.apache.ibatis.mapping.BoundSql;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.mapping.SqlCommandType;import org.apache.ibatis.plugin.*;import org.apache.ibatis.session.ResultHandler;import org.apache.ibatis.session.RowBounds;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.transaction.support.TransactionSynchronizationManager;import java.util.Locale;import java.util.Map;import java.util.Properties;import java.util.concurrent.ConcurrentHashMap;/**
自定义数据源切换类MyRoutingDataSource.java
 
package com.main.common.mybatis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import java.util.HashMap;import java.util.Map;/**
自定义数据库事务处理类MyDynamicDataSourceTransactionManager
 
package com.main.common.mybatis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.TransactionDefinition;/**
自定义数据源(读/写)
 
package com.main.common.mybatis;/**
说明
 
mybatis插件+扩展Spring数据源是最省力的方式,不需要改动任何原代码,如果是数据库事务,则插件不生效,不会对数据库事务造成影响
 
spring配置
 
<bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource"
在mybatis配置文件中配置插件
<configuration>
mybatis读写分离插件源码MyDynamicDataSourcePlugin.java
 
package com.main.common.mybatis;import org.apache.ibatis.executor.Executor;import org.apache.ibatis.executor.keygen.SelectKeyGenerator;import org.apache.ibatis.mapping.BoundSql;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.mapping.SqlCommandType;import org.apache.ibatis.plugin.*;import org.apache.ibatis.session.ResultHandler;import org.apache.ibatis.session.RowBounds;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.transaction.support.TransactionSynchronizationManager;import java.util.Locale;import java.util.Map;import java.util.Properties;import java.util.concurrent.ConcurrentHashMap;/**
自定义数据源切换类MyRoutingDataSource.java
 
package com.main.common.mybatis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import java.util.HashMap;import java.util.Map;/**
自定义数据库事务处理类MyDynamicDataSourceTransactionManager
 
package com.main.common.mybatis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.TransactionDefinition;/**
自定义数据源(读/写)
 
package com.main.common.mybatis;/**