前言
前一篇中我們使用spring boot+mybatis創建了單一數據源,其中單一數據源不需要我們自己手動創建,spring boot自動配置在程序啟動時會替我們創建好數據源。
准備工作
application.yml中配置connection的4個屬性
spring: datasource: read: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.80.129:3306/test username: root password: 123456 write: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.80.129:3306/test username: root password: 123456
多數據源創建方法
1、多數據源主要是需要我們手動來創建DataSource、SqlSessionFactory、SqlSessionTemplate。這里我們基於同一個庫來創建讀寫分離的數據源。這里兩個方法的返回值都是javax.sql.DataSource。
@Configuration public class DataSourceConfig { @Primary @Bean(name="readDataSource") @ConfigurationProperties(prefix = "spring.datasource.read") public DataSource readDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name="writeDataSource") @ConfigurationProperties(prefix = "spring.datasource.write") public DataSource writeDataSource(){ return DataSourceBuilder.create().build(); } }
2、讀寫分離的配置類。也就是分別創建讀寫的SqlSessionFactory和SqlSessionTemplate
@Configuration @MapperScan(basePackages = {"com.zhangfei.dao.read"},sqlSessionFactoryRef = "readSqlSessionFactory") public class MyBatisDbAConfig { @Autowired @Qualifier("readDataSource") private DataSource dataSource; @Bean public SqlSessionFactory readSqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resource=resolver.getResources("classpath:mybatis/read/*.xml"); sqlSessionFactoryBean.setMapperLocations(resource); return sqlSessionFactoryBean.getObject(); } @Bean public SqlSessionTemplate readSqlSession() throws Exception{ SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(readSqlSessionFactory()); return sqlSessionTemplate; } }
@Configuration @MapperScan(basePackages = {"com.zhangfei.dao.write"},sqlSessionFactoryRef = "writeSqlSessionFactory") public class MyBatisDbBConfig { @Autowired @Qualifier("writeDataSource") private DataSource dataSource; @Bean public SqlSessionFactory writeSqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resource=resolver.getResources("classpath:mybatis/write/*.xml"); sqlSessionFactoryBean.setMapperLocations(resource); return sqlSessionFactoryBean.getObject(); } @Bean public SqlSessionTemplate writeSqlSession() throws Exception{ SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(writeSqlSessionFactory()); return sqlSessionTemplate; } }
3、read包下的dao接口
public interface StudentReadDao { List<Student> getStudentList(); Student getById(long id); }
4、wite包下的dao接口
public interface StudentWriteDao { int delete(long id); int insert(Student student); int update(Student student); }
5、分別創建讀寫的mapper文件。我本地分別創建了:resources/mybatis/read/studentdao.xml、/resources/mybatis/write/studentdao.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.zhangfei.dao.read.StudentReadDao"> <select id="getStudentList" resultType="com.zhangfei.entity.Student"> select * from student; </select> <select id="getById" resultType="com.zhangfei.entity.Student"> select * from student where id=#{id}; </select> </mapper>
<?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.zhangfei.dao.write.StudentWriteDao"> <insert id="insert" parameterType="com.zhangfei.entity.Student"> insert into student (name,age) values (#{name},#{age}) </insert> <update id="update" parameterType="com.zhangfei.entity.Student"> update student set name=#{name},age=#{age} where id=#{id} </update> <delete id="delete" parameterType="long"> delete from student where id=#{id} </delete> </mapper>
OK。以上幾部就搞定了讀寫分離的准備工作,接着就可以在controller里調用了。 准備工作完成后,還有重要的一點就是需要在程序入口處排除springboot自動屬性提供的數據源 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@RestController @RequestMapping("/student") public class StudentController { @Autowired StudentReadDao studentDao; @GetMapping("/getbyid/{id}/") public Student getById(@PathVariable("id")long id){ Student student=studentDao.getById(id); return student; } @GetMapping("/all/") public List<Student> getAll(){ return studentDao.getStudentList(); } }
總結
好了,基本上又是3分鍾就搞定了SpringBoot+MyBatis多數據源或者叫做讀寫分離的工作。那么不知道你又沒有反問我們手動創建的DataSource具體類型是什么呢? 這里我們在這里只寫了javax.sql.DataSource接口。 我本地用的是springboot 2.0.2 ,當前 DataSourceBuilder 只支持3種類型的數據源: com.zaxxer.hikari.HikariDataSource、org.apache.tomcat.jdbc.pool.DataSource、org.apache.commons.dbcp2.BasicDataSource。可以在DataSourceBuilder類中看到相關代碼。那么現在這種情況因為我們引用的內嵌tomcat,所以我們這里返回的數據源類型是org.apache.tomcat.jdbc.pool.DataSource。 可以用instanceof驗證一下具體的數據源類型。