jeecgboot多数据源的使用

首先使用多数据源需要在application-dev.yml中增加新的数据库连接

datasource:
   datasource: 
          master: 
            url: jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false
            username: root
            password: root
            driver-class-name: com.mysql.jdbc.Driver   
          multi-datasource1: 
            url: jdbc:mysql://localhost:3306/jeecg-boot2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
            username: root
            password: root
            driver-class-name: com.mysql.jdbc.Driver

然后使用@DS注解进行数据源的切换

对于@DS的使用位置应该放在service的实现层,可以在类或方法上使用,其中方法上的注解优先级高于类上的

@Service
@DS("multi-datasource1")
public class JeecgDemoServiceImpl implements JeecgDemoService {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public List<Map<String, Object>> selectAll() {
    return  jdbcTemplate.queryForList("select * from user");
  }
  
  @Override
  @DS("multi-datasource2")
  public List<Map<String, Object>> selectByCondition() {
    return  jdbcTemplate.queryForList("select * from user where age >10");
  }
}

 官方案例:

http://doc.jeecg.com/2043947

原文地址:https://www.cnblogs.com/1gaoyu/p/15699381.html