Springboot+mybatis+druid 配置多数据源
Springboot+mybatis+druid 配置多数据源
项目结构
application.yml配置文件
spring: application: name: service datasource: primary: jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL username: gkh password: 123456 driver-class-name: oracle.jdbc.driver.OracleDriver type: com.alibaba.druid.pool.DruidDataSource #使用druid连接池 #url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL #type: oracle.jdbc.pool.OracleDataSource secondary: jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL username: gkh password: 123456 driver-class-name: oracle.jdbc.driver.OracleDriver type: com.alibaba.druid.pool.DruidDataSource #使用druid连接池
主数据源配置代码
package com.gkh.springboot.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.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @Primary:指定为默认数据源,没有该注解会报错,系统找不到默认数据源 * @MapperScan:扫描指定包的mapper作为对应数据源,建议每个数据源都用不同的包区分 * @Qualifier:与@Autowired类似,用作区分如果存在多个实现类要指定要注入哪个 参数为指定Bean的name */ @Configuration @MapperScan(basePackages = "com.gkh.springboot.mapper.primary", sqlSessionFactoryRef = "primarySqlSessionFactory") public class DataSource1Config { /** * 生成数据源,@Primary注解声明为默认数据源 * @return */ @Bean(name="primaryDataSoure") @Primary @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource(){ return DataSourceBuilder.create().build(); } /** * 创建sqlSessionFactory * @param datasource * @return * @throws Exception */ @Bean(name = "primarySqlSessionFactory") @Primary public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSoure") DataSource datasource) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); return bean.getObject(); } /** * 配置事务管理 * @param datasource * @return */ @Bean(name = "primaryTransactionManager") @Primary public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSoure") DataSource datasource){ return new DataSourceTransactionManager(datasource); } @Bean(name = "primarySqlSessionTemplate") @Primary public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
第二个数据源代码
package com.gkh.springboot.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.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.gkh.springboot.mapper.secondary", sqlSessionFactoryRef = "secondSqlSessionFactory") public class DataSource2Config { @Bean(name = "secondDatasource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondDatasource(){ return DataSourceBuilder.create().build(); } @Bean(name = "secondSqlSessionFactory") public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDatasource") DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "secondTransactionManager") public DataSourceTransactionManager sourceTransactionManager(@Qualifier("secondDatasource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name = "secondSqlSessionTemplate") public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
Controller:
UserController
@Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; /** * 通过主键id查询 * @param id * @return */ @GetMapping(value = "/getUser") @ResponseBody public User getUserById(@RequestParam("id") Long id){ return this.userService.getUserById(id); } }
StudentController
@Controller @RequestMapping(value = "/student") public class StudentController { @Autowired private StudentService studentService; @GetMapping(value = "/getStudent/{id}") @ResponseBody public Student getStudent(@PathVariable int id){ return studentService.selectByPrimaryKey(id); } }
service
UserServiceImpl
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return this.userMapper.selectByPrimaryKey(id); } }
StudentServiceImpl
@Service public class StudentServiceImpl implements StudentService { @Autowired StudentMapper studentMapper; @Override public Student selectByPrimaryKey(int id) { return studentMapper.selectByPrimaryKey(id); } }
版权声明:本文为gekh原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。