springboot配置多数据源
一、分包方式配置
dao层按照数据源分包、利用配置多个配置类对应不同包设置数据源以及事务。
1.建库建表(创建两个数据库以及user表和user实体类)
1.1 建库建表
1.2 创建user实体 user.java
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
@Data
@TableName("user")
@AllArgsConstructor
public class User implements Serializable {
@TableId(value = "id")
private Integer id;
@TableField(value = "name")
private String name;
@TableField(value = "password")
private String password;
}
2.配置文件 配置对应数据源
3.项目按照配置文件数据源创建不同的dao层包
4.根据不同包创建不同的数据源配置类
4.1 DemoDataSourceConfig.java
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.fds.datasources.dao.demo", sqlSessionFactoryRef = "demoSqlSessionFactory")
public class DemoDataSourceConfig {
@Primary // 表示这个数据源是默认数据源, 这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(默认数据源)
@Bean("demoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.demo") //读取application.yml中的配置参数映射成为一个对象
public DataSource getDemoDataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean("demoSqlSessionFactory")
public SqlSessionFactory demoSqlSessionFactory(@Qualifier("demoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/demo/*.xml"));
return bean.getObject();
}
@Primary
@Bean("demoSqlSessionTemplate")
public SqlSessionTemplate demoSqlSessionTemplate(@Qualifier("demoSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Primary
@Bean(name = "demoTransactionManager")
public DataSourceTransactionManager demoTransactionManager() {
return new DataSourceTransactionManager(getDemoDataSource());
}
}
4.2 TestDataSourceConfig.java
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.fds.datasources.dao.test", sqlSessionFactoryRef = "testSqlSessionFactory")
public class TestDataSourceConfig {
@Bean("testDataSource")
@ConfigurationProperties(prefix = "spring.datasource.test")
public DataSource getTestDataSource(){
return DataSourceBuilder.create().build();
}
@Bean("testSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test/*.xml"));
return bean.getObject();
}
@Bean("testSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "testTransactionManager")
public DataSourceTransactionManager testTransactionManager() {
return new DataSourceTransactionManager(getTestDataSource());
}
}
5.创建测试
5.1 创建service接口IuserService.java
import java.util.List;
/**
* @author Lkk
*/
public interface IuserService {
/**
* testUseList
* @param user
* @return
*/
List<User> getTestList();
/**
* demoUseList
* @param user
* @return
*/
List<User> getDemoList();
}
5.2 创建实现类UserServiceImpl.java
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @author Lkk
*/
@Service("userService")
public class UserServiceImpl implements IuserService {
@Resource
private TestUserMapper testUserMapper;
@Resource
private DemoUserMapper demoUserMapper;
@Override
public List<User> getTestList() {
return testUserMapper.listUser();
}
@Override
public List<User> getDemoList() {
return demoUserMapper.listUser();
}
}
5.3 创建controller UserController.java
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* @projectName: fds
* @package: com.sdf.gateway.com.sdf.gateway.com.fds.gateway.controller
* @className: UserController
* @author: Lkk
* @description: TODO
* @date: 2022/12/5 15:59
*/
@RestController
@RequestMapping("/user/")
@Api(tags = "用户信息")
public class UserController {
@Resource
private IuserService iuserService;
/**
* 获取demo数据源下user信息
* @return
*/
@ApiOperation(value = "demo数据源用户查询")
@GetMapping("getDemo")
public ResponseEntity getDemo(){
List<User> demoList = iuserService.getDemoList();
return ResponseEntity.ok(demoList);
}
@ApiOperation(value = "test数据源用户查询")
@GetMapping("getTest")
public ResponseEntity getTest(){
List<User> demoList = iuserService.getTestList();
return ResponseEntity.ok(demoList);
}
}