spring boot mybatis-plus dynamic-datasource 配置文件 相关依赖环境配置
##yaml配置
server:
port: 8866
servlet:
context-path: /yym
tomcat:
max-threads: 300
connection-timeout: 57000
max-connections: 500
connection-timeout: 57000
spring:
datasource:
dynamic:
primary: master
strict: false # 严格模式
datasource:
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.3.156:3306/yymdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: yym
password: 123456
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username: admin
login-password: admin
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
mybatis-plus:
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
db-config:
id-type: auto
table-underline: true
##父级pom.xml配置
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.yym
yympro
1.0
pom
##子模块pom.xml配置
4.0.0
com.yym
yympro
1.0
com.yym
service
1.0
jar
service
UTF-8
11
11
1.1.22
3.1.1
3.3.2
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.alibaba
druid
${alibaba-druid}
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus-boot-starter}
com.baomidou
dynamic-datasource-spring-boot-starter
${dynamic-datasource-spring-boot-starter.version}
mysql
mysql-connector-java
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
maven-site-plugin
3.7.1
maven-project-info-reports-plugin
3.0.0
org.springframework.boot
spring-boot-maven-plugin
##表结构
CREATE TABLE `t_test` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(48) COLLATE utf8mb4_general_ci DEFAULT NULL,
`address` VARCHAR(256) COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
##Test.java
package com.yym.entity;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("t_test")
public class Test {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
##TestMapper.xml
##TestMapper.java
package com.yym.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yym.entity.Test;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestMapper extends BaseMapper {
}
##TestService.java
package com.yym.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yym.entity.Test;
import com.yym.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
private TestMapper testMapper;
public Test test(Test test) {
return testMapper.selectOne(new QueryWrapper().eq("name", test.getName()));
}
}
##TestController
sudo cmake . -DCMAKE_INSTALL_PREFIX=/home/yym/mysql8-install/mysql -DMYSQL_DATADIR=/home/yym/mysql8-install/data -DSYSCONFDIR=/home/yym/mysql8-install/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/home/yym/mysql8/mysql-8.1.0/boost -DWITH_DEBUG=1 -DFORCE_INSOURCE_BUILD=1
package com.yym.controller;
import com.yym.entity.Test;
import com.yym.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/test/v1/")
public class TestController {
@Autowired
private TestService testService;
Logger logger = LoggerFactory.getLogger(TestController.class);
@GetMapping("test")
public String test() {
Test test = new Test();
test.setName("yym");
Test testDB = testService.test(test);
return testDB.getAddress();
}
}
##项目启动类BootStrap.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootStrap {
public static void main(String[] args) {
SpringApplication.run(BootStrap.class, args);
}
}
##浏览器访问
192.168.3.188:8866/yym/test/v1/test