文章目录
- 一、改造多数据源
- 1. 依赖引入
- 2. 启动类添加注解
- 3. 配置多数据源
- 二、案例实战
- 2.1. controller
- 2.2. service
- 2.3. impl
- 2.4. mapper
- 2.5. xml
- 三、分页失效解决方案
一、改造多数据源
1. 依赖引入
目前改用dynamic-datasource方式多数据源处理,配置如下图:
<!--置动态数据源--><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.4.1</version></dependency>
2. 启动类添加注解
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
3. 配置多数据源
########################################## 多数据源配置 ############################################
spring:datasource:dynamic:primary: master #设置默认的数据源或者数据源组,默认值即为masterstrict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源datasource:master:url: jdbc:mysql://localhost:3306/dbmysql?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=trueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置filters: wall,mergeStatoracle_1:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@192.168.xxx.xxx:1521:orclusername: orclpassword: orclfilters: wall,mergeStat
二、案例实战
使用规范:只需要在持久层接口类上添加@DS(“oracle_1”) 注解,里面写yml配置的数据库别名即可
2.1. controller
/*** 英雄信息表控制器** @author gblfy* @Date 2021-08-13 11:19:00*/
@Controller
@RequestMapping("/hero")
public class HeroController extends BaseController {private String PREFIX = "/antifraud/hero";@Autowiredprivate HeroService heroService;/*** 查询列表** @author gblfy* @Date 2021-08-13*/@ResponseBody@RequestMapping("/list")public LayuiPageInfo list(HeroParam heroParam) {return this.heroService.findPageBySpec(heroParam);}
2.2. service
/*** <p>* 英雄信息表 服务类* </p>** @author gblfy* @since 2021-08-13*/
public interface HeroService extends IService<Hero> {/*** 查询分页数据,Specification模式** @author gblfy* @Date 2021-08-13*/LayuiPageInfo findPageBySpec(HeroParam param);}
2.3. impl
@Service
public class HeroServiceImpl extends ServiceImpl<HeroMapper, Hero> implements HeroService {@Autowiredprivate HeroMapper heroMapper;@Overridepublic LayuiPageInfo findPageBySpec(HeroParam param){Page pageContext = getPageContext();IPage page = heroMapper.customPageList(pageContext, param);return LayuiPageFactory.createPageInfo(page);}private Page getPageContext() {return LayuiPageFactory.defaultPage();}}
2.4. mapper
@DS("oracle_1")
public interface HeroMapper extends BaseMapper<Hero> {/*** 获取分页实体列表** @author gblfy* @Date 2021-08-13*/Page<HeroResult> customPageList(@Param("page") Page page, @Param("paramCondition") HeroParam paramCondition);}
2.5. xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gblfy.modular.order.mapper.HeroMapper"><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="com.gblfy.modular.order.entity.Hero"><id column="SNO" property="sno"/><result column="USER_NAME" property="userName"/><result column="AGE" property="age"/></resultMap><!-- 通用查询结果列 --><sql id="Base_Column_List">SNOAS "sno", USER_NAME AS "userName", AGE AS "age"</sql><select id="customPageList" resultType="com.gblfy.modular.order.model.result.HeroResult"parameterType="com.gblfy.modular.order.model.request.HeroParam">select<include refid="Base_Column_List"/>from HERO where 1 = 1</select>
</mapper>
三、分页失效解决方案
现象:改造完多数据源之后,分页插件就失效了。
方案:重新加载分页插件
- 分页插件
MultiDataMyBatisPlusConfig
package com.gblfy.config.datasource;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author gblfy* @date 2021-08-13*/
@Configuration
public class MultiDataMyBatisPlusConfig {/*** 使用多数据源后分页插件失效,拦截器重新调用分页插件** @return*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();}
}