MyBatis初级实战之四:druid多数据源(1)

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
initial-size: 5

min-idle: 5

max-active: 20

#配置获取连接等待超时的时间

max-wait: 60000

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

time-between-eviction-runs-millis: 60000

配置一个连接在池中最小生存的时间,单位是毫秒

min-evictable-idle-time-millis: 30000

配置一个连接在池中最大生存的时间,单位是毫秒

max-evictable-idle-time-millis: 300000

validation-query: SELECT 1 FROM user

test-while-idle: true

test-on-borrow: true

test-on-return: false

是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开

pool-prepared-statements: true

max-pool-prepared-statement-per-connection-size: 20

配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall’用于防火墙

filters: stat,wall,slf4j

filter:

stat:

merge-sql: true###

slow-sql-millis: 5000

#3.基础监控配置

web-stat-filter:

enabled: true

url-pattern: /*

#设置不统计哪些URL

exclusions: “.js,.gif,.jpg,.png,.css,.ico,/druid/*”

session-stat-enable: true

session-stat-max-count: 100

stat-view-servlet:

enabled: true

url-pattern: /druid/*

reset-enable: true

#设置监控页面的登录名和密码

login-username: admin

login-password: admin

allow: 127.0.0.1

#deny: 192.168.1.100

日志配置

logging:

level:

root: INFO

com:

bolingcavalry:

druidtwosource:

mapper: debug

  1. user的映射配置,请注意文件位置:
<?xml version="1.0" encoding="UTF-8"?>

insert into user (id, name, age) values (#{id}, #{name}, #{age})

select id, name, age from user where name like concat(‘%’, #{name}, ‘%’)

delete from user where id= #{id}

  1. address的映射配置:
<?xml version="1.0" encoding="UTF-8"?>

insert into address (id, city, street) values (#{id}, #{city}, #{street})

select id, city, street from address where city like concat(‘%’, #{cityname}, ‘%’)

delete from address where id= #{id}

  1. user表的实体类,注意swagger用到的注解:

package com.bolingcavalry.druidtwosource.entity;

import io.swagger.annotations.ApiModel;

import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = “用户实体类”)

public class User {

@ApiModelProperty(value = “用户ID”)

private Integer id;

@ApiModelProperty(value = “用户名”, required = true)

private String name;

@ApiModelProperty(value = “用户地址”, required = false)

private Integer age;

@Override

public String toString() {

return “User{” +

“id=” + id +

“, name='” + name + ‘’’ +

“, age=” + age +

‘}’;

}

…省略get和set方法

}

  1. address表的实体类:

package com.bolingcavalry.druidtwosource.entity;

import io.swagger.annotations.ApiModel;

import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = “地址实体类”)

public class Address {

@ApiModelProperty(value = “地址ID”)

private Integer id;

@ApiModelProperty(value = “城市名”, required = true)

private String city;

@ApiModelProperty(value = “街道名”, required = true)

private String street;

@Override

public String toString() {

return “Address{” +

“id=” + id +

“, city='” + city + ‘’’ +

“, street='” + street + ‘’’ +

‘}’;

}

…省略get和set方法

}

  1. 启动类DuridTwoSourceApplication.java,要注意的是排除掉数据源和事务的自动装配,因为后面会手动编码执行这些配置:

package com.bolingcavalry.druidtwosource;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;

@SpringBootApplication(exclude={

DataSourceAutoConfiguration.class,

DataSourceTransactionManagerAutoConfiguration.class,

})

public class DuridTwoSourceApplication {

public static void main(String[] args) {

SpringApplication.run(DuridTwoSourceApplication.class, args);

}

}

  1. swagger配置:

package com.bolingcavalry.druidtwosource;

import springfox.documentation.service.Contact;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.Tag;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**

  • @Description: swagger配置类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/11 7:54

*/

@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.tags(new Tag(“UserController”, “用户服务”),

new Tag(“AddressController”, “地址服务”))

.select()

// 当前包路径

.apis(RequestHandlerSelectors.basePackage(“com.bolingcavalry.druidtwosource.controller”))

.paths(PathSelectors.any())

.build();

}

//构建 api文档的详细信息函数,注意这里的注解引用的是哪个

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

//页面标题

.title(“MyBatis CURD操作”)

//创建人

.contact(new Contact(“程序员欣宸”, “https://github.com/zq2599/blog_demos”, “zq2599@gmail.com”))

//版本号

.version(“1.0”)

//描述

.description(“API 描述”)

.build();

}

}

  1. 数据源配置TwoDataSourceConfig.java,可见是通过ConfigurationProperties注解来确定配置信息,另外不要忘记在默认数据源上添加Primary注解:

package com.bolingcavalry.druidtwosource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**

  • @Description: druid配置类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/18 08:12

*/

@Configuration

public class TwoDataSourceConfig {

@Primary

@Bean(name = “firstDataSource”)

@ConfigurationProperties(“spring.datasource.druid.first”)

public DataSource first() {

return DruidDataSourceBuilder.create().build();

}

@Bean(name = “secondDataSource”)

@ConfigurationProperties(“spring.datasource.druid.second”)

public DataSource second() {

return DruidDataSourceBuilder.create().build();

}

}

  1. 第一个数据源的mybatis配置类DruidConfigFirst.java,可以结合本篇的第一幅图来看,注意MapperScan注解的两个属性basePackages和sqlSessionTemplateRef是关键,它们最终决定了哪些mapper接口使用哪个数据源,另外注意要带上Primary注解:

package com.bolingcavalry.druidtwosource;

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.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;

/**

  • @Description: druid配置类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/18 08:12

*/

@Configuration

@MapperScan(basePackages = “com.bolingcavalry.druidtwosource.mapper.first”, sqlSessionTemplateRef = “firstSqlSessionTemplate”)

public class DruidConfigFirst {

@Bean(name = “firstSqlSessionFactory”)

@Primary

public SqlSessionFactory sqlSessionFactory(@Qualifier(“firstDataSource”) DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mappers/first/**/*Mapper.xml”));

return bean.getObject();

}

@Bean(name = “firstTransactionManager”)

@Primary

public DataSourceTransactionManager transactionManager(@Qualifier(“firstDataSource”) DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = “firstSqlSessionTemplate”)

@Primary

public SqlSessionTemplate sqlSessionTemplate(@Qualifier(“firstSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

  1. 第二个数据源的mybatis配置DruidConfigSecond.java,注意不要带Primary注解:

package com.bolingcavalry.druidtwosource;

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.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;

/**

  • @Description: druid配置类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/18 08:12

*/

@Configuration

@MapperScan(basePackages = “com.bolingcavalry.druidtwosource.mapper.second”, sqlSessionTemplateRef = “secondSqlSessionTemplate”)

public class DruidConfigSecond {

@Bean(name = “secondSqlSessionFactory”)

public SqlSessionFactory sqlSessionFactory(@Qualifier(“secondDataSource”) DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mappers/second/**/*Mapper.xml”));

return bean.getObject();

}

@Bean(name = “secondTransactionManager”)

public DataSourceTransactionManager transactionManager(@Qualifier(“secondDataSource”) DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = “secondSqlSessionTemplate”)

public SqlSessionTemplate sqlSessionTemplate(@Qualifier(“secondSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

  1. user表的mapper接口类很简单,只有三个接口,注意package位置:

package com.bolingcavalry.druidtwosource.mapper.first;

import com.bolingcavalry.druidtwosource.entity.User;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public interface UserMapper {

int insertWithFields(User user);

List findByName(String name);

int delete(int id);

}

  1. address表的Mapper接口类:

package com.bolingcavalry.druidtwosource.mapper.second;

import com.bolingcavalry.druidtwosource.entity.Address;

import org.springframework.stereotype.Repository;

import java.util.List;

/**

  • @Description: 地址实体的接口类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/4 8:32

*/

@Repository

public interface AddressMapper {

int insertWithFields(Address address);

List

findByCityName(String cityName);

int delete(int id);

}

  1. user表的service类:

package com.bolingcavalry.druidtwosource.service;

import com.bolingcavalry.druidtwosource.entity.User;

import com.bolingcavalry.druidtwosource.mapper.first.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

public class UserService {

@Autowired

UserMapper userMapper;

public User insertWithFields(User user) {

userMapper.insertWithFields(user);

return user;

}

public List findByName(String name) {

return userMapper.findByName(name);

}

public int delete(int id) {

return userMapper.delete(id);

}

}

  1. address表的service类:

package com.bolingcavalry.druidtwosource.service;

import com.bolingcavalry.druidtwosource.entity.Address;

import com.bolingcavalry.druidtwosource.entity.User;

import com.bolingcavalry.druidtwosource.mapper.first.UserMapper;

import com.bolingcavalry.druidtwosource.mapper.second.AddressMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class AddressService {

@Autowired

AddressMapper addressMapper;

public Address insertWithFields(Address address) {

addressMapper.insertWithFields(address);

return address;

}

public List

findByCityName(String cityName) {

return addressMapper.findByCityName(cityName);

}

public int delete(int id) {

return addressMapper.delete(id);

}

}

  1. user表的controller:

package com.bolingcavalry.druidtwosource.controller;

import com.bolingcavalry.druidtwosource.entity.User;

import com.bolingcavalry.druidtwosource.service.UserService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping(“/user”)

@Api(tags = {“UserController”})

public class UserController {

@Autowired

private UserService userService;

@ApiOperation(value = “新增user记录”, notes=“新增user记录”)

@RequestMapping(value = “/insertwithfields”,method = RequestMethod.PUT)

public User create(@RequestBody User user) {

return userService.insertWithFields(user);

}

@ApiOperation(value = “删除指定ID的user记录”, notes=“删除指定ID的user记录”)

@ApiImplicitParam(name = “id”, value = “用户ID”, paramType = “path”, required = true, dataType = “Integer”)

@RequestMapping(value = “/{id}”, method = RequestMethod.DELETE)

public int delete(@PathVariable int id){

return userService.delete(id);

}

@ApiOperation(value = “根据名称模糊查找所有user记录”, notes=“根据名称模糊查找所有user记录”)

@ApiImplicitParam(name = “name”, value = “用户名”, paramType = “path”, required = true, dataType = “String”)

@RequestMapping(value = “/findbyname/{name}”, method = RequestMethod.GET)

public List findByName(@PathVariable(“name”) String name){

return userService.findByName(name);

}

}

  1. address表的controller:

package com.bolingcavalry.druidtwosource.controller;

import com.bolingcavalry.druidtwosource.entity.Address;

import com.bolingcavalry.druidtwosource.service.AddressService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

  • @Description: user表操作的web接口

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/4 8:31

*/

@RestController

@RequestMapping(“/address”)

@Api(tags = {“AddressController”})

public class AddressController {

@Autowired

private AddressService addressService;

@ApiOperation(value = “新增address记录”, notes=“新增address记录”)

@RequestMapping(value = “/insertwithfields”,method = RequestMethod.PUT)

public Address create(@RequestBody Address address) {

return addressService.insertWithFields(address);

}

@ApiOperation(value = “删除指定ID的address记录”, notes=“删除指定ID的address记录”)

@ApiImplicitParam(name = “id”, value = “地址ID”, paramType = “path”, required = true, dataType = “Integer”)

@RequestMapping(value = “/{id}”, method = RequestMethod.DELETE)

public int delete(@PathVariable int id){

return addressService.delete(id);

}

@ApiOperation(value = “根据城市名模糊查找所address记录”, notes=“根据城市名模糊查找所address记录”)

@ApiImplicitParam(name = “name”, value = “城市名”, paramType = “path”, required = true, dataType = “String”)

@RequestMapping(value = “/findbycityname/{cityname}”, method = RequestMethod.GET)

public List

findByName(@PathVariable(“cityname”) String cityName){

return addressService.findByCityName(cityName);

}

}

  • 至此,编码完成,接下来编写单元测试代码;

单元测试

  1. 新增配置文件application-test.yml,其内容仅有下图红框位置与application.yml不同,其他的全部一致:

在这里插入图片描述

  1. user表的测试用例如下:

package com.bolingcavalry.druidtwosource.controller;

import com.bolingcavalry.druidtwosource.entity.User;

import com.google.gson.Gson;

import com.google.gson.JsonArray;

import com.google.gson.JsonParser;

import org.junit.jupiter.api.*;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.http.MediaType;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.test.web.servlet.MockMvc;

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.UUID;

import static org.hamcrest.Matchers.hasSize;

import static org.hamcrest.Matchers.is;

import static org.hamcrest.core.IsEqual.equalTo;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**

  • @Description: 单元测试类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/9 23:55

*/

@RunWith(SpringRunner.class)

@SpringBootTest

@AutoConfigureMockMvc

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

@ActiveProfiles(“test”)

class UserControllerTest {

@Autowired

private MockMvc mvc;

// user表的name字段,这里为了保证测试时新增和删除的记录是同一条,用UUID作为用户名

static String testName;

@BeforeAll

static void init() {

testName = UUID.randomUUID().toString().replaceAll(“-”,“”);

}

@Test

@Order(1)

void insertWithFields() throws Exception {

String jsonStr = “{“name”: “” + testName + “”, “age”: 10}”;

mvc.perform(

MockMvcRequestBuilders.put(“/user/insertwithfields”)

.contentType(MediaType.APPLICATION_JSON)

.content(jsonStr)

.accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(jsonPath(“$.name”, is(testName)))

.andDo(print())

.andReturn()

.getResponse()

.getContentAsString();

}

@Test

@Order(2)

void findByName() throws Exception {

mvc.perform(MockMvcRequestBuilders.get(“/user/findbyname/”+ testName).accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(jsonPath(“$”, hasSize(1)))

.andDo(print());

}

@Test

@Order(3)

void delete() throws Exception {

// 先根据名称查出记录

String responseString = mvc.perform(MockMvcRequestBuilders.get(“/user/findbyname/”+ testName).accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(jsonPath(“$”, hasSize(1)))

.andDo(print())

.andReturn()

.getResponse()

.getContentAsString();

// 反序列化得到数组

JsonArray jsonArray = JsonParser.parseString(responseString).getAsJsonArray();

// 反序列化得到user实例

User user = new Gson().fromJson(jsonArray.get(0), User.class);

// 执行删除

mvc.perform(MockMvcRequestBuilders.delete(“/user/”+ user.getId()).accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(content().string(equalTo(“1”)))

.andDo(print());

}

}

  1. address表的单元测试如下:

package com.bolingcavalry.druidtwosource.controller;

import com.bolingcavalry.druidtwosource.entity.Address;

import com.google.gson.Gson;

import com.google.gson.JsonArray;

import com.google.gson.JsonParser;

import org.junit.jupiter.api.*;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.http.MediaType;

import org.springframework.test.context.ActiveProfiles;

面试资料整理汇总

成功从小公司跳槽进蚂蚁定级P7,只因刷了七遍这些面试真题

成功从小公司跳槽进蚂蚁定级P7,只因刷了七遍这些面试真题

这些面试题是我朋友进阿里前狂刷七遍以上的面试资料,由于面试文档很多,内容更多,没有办法一一为大家展示出来,所以只好为大家节选出来了一部分供大家参考。

面试的本质不是考试,而是告诉面试官你会做什么,所以,这些面试资料中提到的技术也是要学会的,不然稍微改动一下你就凉凉了

在这里祝大家能够拿到心仪的offer!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
ponse()

.getContentAsString();

// 反序列化得到数组

JsonArray jsonArray = JsonParser.parseString(responseString).getAsJsonArray();

// 反序列化得到user实例

User user = new Gson().fromJson(jsonArray.get(0), User.class);

// 执行删除

mvc.perform(MockMvcRequestBuilders.delete(“/user/”+ user.getId()).accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(content().string(equalTo(“1”)))

.andDo(print());

}

}

  1. address表的单元测试如下:

package com.bolingcavalry.druidtwosource.controller;

import com.bolingcavalry.druidtwosource.entity.Address;

import com.google.gson.Gson;

import com.google.gson.JsonArray;

import com.google.gson.JsonParser;

import org.junit.jupiter.api.*;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.http.MediaType;

import org.springframework.test.context.ActiveProfiles;

面试资料整理汇总

[外链图片转存中…(img-hemsBCie-1714707774077)]

[外链图片转存中…(img-qUbUdkou-1714707774077)]

这些面试题是我朋友进阿里前狂刷七遍以上的面试资料,由于面试文档很多,内容更多,没有办法一一为大家展示出来,所以只好为大家节选出来了一部分供大家参考。

面试的本质不是考试,而是告诉面试官你会做什么,所以,这些面试资料中提到的技术也是要学会的,不然稍微改动一下你就凉凉了

在这里祝大家能够拿到心仪的offer!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot项目中使用MyBatis Plus和Druid数据的步骤如下: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2. 配置Druid数据 在`application.yml`中添加Druid数据的配置: ```yaml spring: datasource: # 主数据 druid: url: jdbc:mysql://localhost:3306/main_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Druid配置 initialSize: 5 minIdle: 5 maxActive: 20 testOnBorrow: false testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 validationQuery: SELECT 1 FROM DUAL # 从数据 druid2: url: jdbc:mysql://localhost:3306/sub_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Druid配置 initialSize: 5 minIdle: 5 maxActive: 20 testOnBorrow: false testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 validationQuery: SELECT 1 FROM DUAL ``` 3. 配置MyBatis Plus 在`application.yml`中添加MyBatis Plus的配置: ```yaml mybatis-plus: # 主数据配置 mapper-locations: classpath:mapper/main/*.xml type-aliases-package: com.example.main.entity global-config: db-config: id-type: auto field-strategy: not_empty logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 从数据配置 multi-datasource: main: mapper-locations: classpath:mapper/main/*.xml type-aliases-package: com.example.main.entity sub: mapper-locations: classpath:mapper/sub/*.xml type-aliases-package: com.example.sub.entity ``` 4. 配置数据路由 在`com.example.config`包下创建`DynamicDataSourceConfig`类,用于配置数据路由: ```java @Configuration public class DynamicDataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid") public DataSource mainDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.druid2") public DataSource subDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); Map<Object, Object> dataSourceMap = new HashMap<>(2); dataSourceMap.put("main", mainDataSource()); dataSourceMap.put("sub", subDataSource()); // 将主数据作为默认数据 dynamicDataSource.setDefaultTargetDataSource(mainDataSource()); dynamicDataSource.setTargetDataSources(dataSourceMap); return dynamicDataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dynamicDataSource()); sqlSessionFactoryBean.setTypeAliasesPackage("com.example.main.entity"); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/main/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate() throws Exception { return new SqlSessionTemplate(sqlSessionFactory()); } } ``` 5. 配置数据切换 在`com.example.config`包下创建`DynamicDataSource`类,用于实现数据切换: ```java public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSource(); } } ``` 在`com.example.config`包下创建`DataSourceContextHolder`类,用于存储当前数据: ```java public class DataSourceContextHolder { private static final ThreadLocal<String> DATASOURCE_CONTEXT_HOLDER = new ThreadLocal<>(); public static void setDataSource(String dataSource) { DATASOURCE_CONTEXT_HOLDER.set(dataSource); } public static String getDataSource() { return DATASOURCE_CONTEXT_HOLDER.get(); } public static void clearDataSource() { DATASOURCE_CONTEXT_HOLDER.remove(); } } ``` 在`com.example.aop`包下创建`DataSourceAspect`类,用于切换数据: ```java @Aspect @Component public class DataSourceAspect { @Pointcut("@annotation(com.example.annotation.DataSource)") public void dataSourcePointCut() { } @Before("dataSourcePointCut()") public void before(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class); if (dataSource != null) { String value = dataSource.value(); DataSourceContextHolder.setDataSource(value); } } @After("dataSourcePointCut()") public void after(JoinPoint joinPoint) { DataSourceContextHolder.clearDataSource(); } } ``` 6. 使用多数据 在需要使用从数据的方法上加上`@DataSource("sub")`注解,如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> listUsers() { DataSourceContextHolder.setDataSource("sub"); List<User> users = userMapper.selectList(null); DataSourceContextHolder.clearDataSource(); return users; } } ``` 这样就完成了Spring Boot项目中使用MyBatis Plus和Druid数据的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值