需要配置yml:参考[mybatisplus 官网文档 ]([https://mp.baomidou.com/guide/dynamic-datasource.html]) ``` datasource: druid: stat-view-servlet: enabled: true loginUsername: admin loginPassword: 123456 allow: web-stat-filter: enabled: true dynamic: druid: # 全局druid参数,绝大部分值和默认保持一致。(现已支持的参数如下,不清楚含义不要乱设置) # 连接池的配置信息 # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,slf4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 datasource: master: url: jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false username: root password: xxx driver-class-name: com.mysql.jdbc.Driver # 多数据源配置 multi-datasource1: url: jdbc:mysql://127.0.0.1:3306/test-multi username: root password: xxx driver-class-name: com.mysql.jdbc.Driver #mybatis plus 设置 mybatis-plus: mapper-locations: classpath*:org/jeecg/modules/**/xml/*Mapper.xml global-config: # 关闭MP3.0自带的banner banner: false db-config: #主键类型 0:"数据库ID自增",1:"该类型为未设置主键类型", 2:"用户输入ID",3:"全局唯一ID (数字类型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)"; id-type: ID_WORKER_STR # 默认数据库表下划线命名 table-underline: true configuration: # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 返回类型为Map,显示null对应的字段 call-setters-on-nulls: true ``` 实体 ~~~ import lombok.Data; @Data public class DcTest { private Integer id; private String name; private String note; } ~~~ mapper(可以不写xml): ~~~ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.jeecg.modules.demo.test.entity.DcTest; public interface DcTestMapper extends BaseMapper<DcTest> { } ~~~ service调用(只需在实现类方法上加个DC注解【**方法注解优先于类上注解**】) ~~~ @Override @DS("multi-datasource1") public void addOnetest(String name) { DcTest one = new DcTest(); one.setId(new Random().nextInt(10000)); one.setName(name); one.setNote("noteone"); this.dcTestMapper.insert(one); } @Override @DS("multi-datasource1") public List<DcTest> queryDcList() { List<DcTest> ls = this.dcTestMapper.selectList(new QueryWrapper<DcTest>()); return ls; } ~~~ controller调用: ~~~ @Autowired private IJeecgDemoService jeecgDemoService; @GetMapping(value = "/multiAddtest/{name}") public String multiAddtest(@PathVariable("name") String name){ this.jeecgDemoService.addOnetest(name); return "success"; } @GetMapping(value = "/multiQuery") public List<DcTest> multiQuery(){ return this.jeecgDemoService.queryDcList(); } ~~~ 最后直接浏览器访问就行了;