主要要是基于MyBatis的SpringBoot多数据源配置,这里利用多数据源演示读写分离,只是纯粹的样式。
application.properties
#读数据库配置 spring.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.db1.driverClassName = com.mysql.jdbc.Driver spring.datasource.db1.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8 spring.datasource.db1.username = root spring.datasource.db1.password = 123456 #写数据库配置 spring.datasource.db2.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.db2.driverClassName = com.mysql.jdbc.Driver spring.datasource.db2.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8 spring.datasource.db2.username = root spring.datasource.db2.password = 123456

ReadDataSourceConfig.java读数据源配置
package com.niugang; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.TransactionManagementConfigurer; import com.alibaba.druid.pool.DruidDataSource; /** * 数据源配置 1 * * @author niugang * */ @Configuration @MapperScan(value = { "com.niugang.dao.read" }, sqlSessionTemplateRef = "sqlSessionTemplate1") public class ReadDataSourceConfig implements TransactionManagementConfigurer { @Bean(name = "dataSource1") @ConfigurationProperties(prefix = "spring.datasource.db1") @Primary //配置主数据库 public DataSource dataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } @Bean(name = "sqlSessionFactory1") @Primary public SqlSessionFactory testSqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource()); //读的myBatis配置文件位置 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/read/*.xml")); return bean.getObject(); } @Bean(name = "sqlSessionTemplate1") @Primary public SqlSessionTemplate testSqlSessionTemplate() throws Exception { return new SqlSessionTemplate(testSqlSessionFactory()); } @Bean(name = "transactionManager1") @Primary public DataSourceTransactionManager testTransactionManager() { return new DataSourceTransactionManager(dataSource()); } @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return testTransactionManager() ; } }

WriterDataSourceConfig.java写数据库配置
package com.niugang; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.TransactionManagementConfigurer; import com.alibaba.druid.pool.DruidDataSource; /** * 数据源配置 1 * * @author niugang **/ @Configuration // @EnableTransactionManagement springboot默认是开启事务的 @MapperScan(value = { "com.niugang.dao.writer" }, sqlSessionTemplateRef = "sqlSessionTemplate2") public class WriterDataSourceConfig implements TransactionManagementConfigurer { @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "spring.datasource.db2") public DataSource dataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } @Bean(name = "sqlSessionFactory2") public SqlSessionFactory testSqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource()); bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/writer/*.xml")); return bean.getObject(); } @Bean(name = "sqlSessionTemplate2") public SqlSessionTemplate testSqlSessionTemplate() throws Exception { return new SqlSessionTemplate(testSqlSessionFactory()); } /** * 配置事务所必须要的 * @return */ @Bean(name = "transactionManager2") public DataSourceTransactionManager testTransactionManager() { return new DataSourceTransactionManager(dataSource()); } /** * 配置事务所必须要 */ @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return testTransactionManager(); } }

Dao层
如果真的要这么做,那么接口命名必须规范,防止调错
ReadUserDao.java 读持久化接口
package com.niugang.dao.read; import java.util.List; import org.springframework.stereotype.Repository; import com.niugang.entity.User; @Repository public interface ReadUserDao { List<User> queryList(User user); User get(Integer id); }

WriterUserDao.java写持久化接口
package com.niugang.dao.writer; import org.springframework.stereotype.Repository; import com.niugang.entity.User; import org.springframework.transaction.annotation.Transactional; @Repository public interface WriterUserDao { @Transactional void save(User user); void delete(Integer id); }

Mapper文件
ReadUser.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.niugang.dao.read.ReadUserDao"> <resultMap id="BaseResultMap" type="com.niugang.entity.User"> <result column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> <result column="phone" property="phone" /> </resultMap> <!--查询字段 --> <sql id="Base_Column_List"> id, name, age,phone </sql> <!-- 查询条件 --> <sql id="queryCondition"> <where> <if test="id!=null"> and id=#{id} </if> <if test="name!=null"> and name=#{name} </if> <if test="age!=null"> and age=#{age} </if> <if test="phone!=null"> and phone=#{phone} </if> </where> </sql> <select id="queryList" resultMap="BaseResultMap" parameterType="com.niugang.entity.User"> select <include refid="Base_Column_List" /> from user <include refid="queryCondition" /> </select> <select id="get" parameterType="int" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where id =#{id} </select> </mapper>

WriterUser.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.niugang.dao.writer.WriterUserDao"> <insert id="save" parameterType="com.niugang.entity.User"> insert into user (name,password,age,phone) values(#{name},#{password},#{age},#{phone}) </insert> <delete id="delete" parameterType="int"> delete from user where id =#{id} </delete> </mapper>
微信公众号

