本文以分包的方式来区分不同的数据源,也就是不同的包,连接不同的数据库
开发背景
1. SpringBoot2.0.4
2. Mybatis3.4.4
3. jdk1.8
dataSource1 连接数据库为springboot(调用UserTest1Service中方法,即操作springboot 数据库)
dataSource2 连接数据库为springboot1(调用UserTest2Service中方法,即操作springboot1 数据库)
<dependencies>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.4version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatypegroupId>
<artifactId>jackson-datatype-jodaartifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.modulegroupId>
<artifactId>jackson-module-parameter-namesartifactId>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.5version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.9version>
dependency>
dependencies>
##数据源1
##driverClassName driver-class-name
spring.datasource.test1.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.test1.jdbc-url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.test1.username: root
spring.datasource.test1.password: root
#数据源2
spring.datasource.test2.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.test2.jdbc-url: jdbc:mysql://127.0.0.1:3306/springboot1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.test2.username: root
spring.datasource.test2.password: root
不要怀疑,application.yml内容就只有这么多接下来看具体代码
@Mapper
public interface UserMapper {
@Select("SELECT * FROM USER WHERE id = #{id}")
User selectUser(Integer id);
@Select("SELECT * FROM USER ")
List<User> findByPage();
@Insert("insert into user ( name, age, password) values(#{name},#{age},#{password})")
Integer save(User user);
}
public interface UserTest1Service {
User selectUser(Integer id);
PageInfo findByPage(Integer pageNum, Integer pageSize);
Integer save(User user);
}
@Service("userService1")
public class UserServicetTest1Impl implements UserTest1Service {
@Autowired
private UserMapper userDao;
@Override
public PageInfo findByPage(Integer pageNum,Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List userDomains = userDao.findByPage();
PageInfo result = new PageInfo(userDomains);
return result;
}
@Override
public Integer save(User user) {
return this.userDao.save(user);
}
@Override
public User selectUser(Integer id) {
System.out.println();
return userDao.selectUser(id);
}
public class User {
private Integer id;
private String name;
private Integer age;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Configuration //注册到springboot 容器中
@MapperScan(basePackages = "com.jessDl.dataSource1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//加载其他文件,如mapper.xml
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
//事务管理
@Bean(name = "test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration //注册到springboot 容器中
@MapperScan(basePackages = "com.jessDl.dataSource2", sqlSessionTemplateRef = "test2SqlSessionTemplate")
public class DataSource2Config {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//加载其他文件,如mapper.xml
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
//事务管理
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@RestController
@RequestMapping("/datasource")
public class DataSourcesController {
@Autowired
@Qualifier("userService1")
private UserTest1Service userService1;
@Autowired
@Qualifier("userService2")
private UserTest2Service userService2;
@RequestMapping("/user1")
public User getUser(){
return this.userService1.selectUser(1);
}
@RequestMapping("/user2")
public User getUser2(){
return this.userService2.selectUser(1);
}
@RequestMapping("/save")
public Integer save1(){
User user = new User();
user.setAge(22);
user.setName("多数据源");
user.setPassword("3333333");
return this.userService2.save(user);
}
@RequestMapping("findPage")
public PageInfo findByPage(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5")
int pageSize){
return userService2.findByPage(pageNum,pageSize);
}
}
@SpringBootApplication
public class DataSourceApplication {
public static void main(String[] args) {
SpringApplication.run(DataSourceApplication.class, args);
}
}
启动后在浏览器访问http://localhost:8080/datasource/save2,往数据源2(数据库springboot1)中user表插入一条数据
返回1,说明已经添加数据成功
好了,SpringBoot Mybatis 已分包的方式实现 多数据源配置到这里就结束了,如果有误或者撸友们有其他方式欢迎提出