Reference documents SpingBoot official website
If the main MyBatis SpringBoot based multi data source configuration, where multiple data sources using separate read and write presentation, just pure form.
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
Read data source configuration 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 // configuration database master public the DataSource the dataSource () { DruidDataSource DruidDataSource = new new DruidDataSource (); return DruidDataSource; } @Bean (name = "sqlSessionFactory1 " ) @Primary public a SqlSessionFactory testSqlSessionFactory () throws Exception { the SqlSessionFactoryBean the bean = new new the SqlSessionFactoryBean (); bean.setDataSource (the dataSource ()); // read the configuration file location 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 write database configuration
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()); } / ** * configuration must be firm * / @Override public the PlatformTransactionManager annotationDrivenTransactionManager () { return testTransactionManager (); } }
Dao layer
If you really want to do this, the interface name must be rules to prevent the wrong tune
ReadUserDao.java read persistence interfaces
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 write persistent Interface
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 file
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>
Micro-channel public number:
JAVA program ape growth path
Resource sharing, recording program ape growing little by little. Focus on Java, Spring, SpringBoot, SpringCloud, distributed, slightly services.