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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM