baomidou动态数据库@DS

发布时间 2023-08-31 14:09:55作者: XUMT111

 

全链路不能使用@Transactional

public interface XXXBasicMapper {
    @DS("operating")
    List<XXXBasicVo> findBasicList(XXXBasicPageDto dto);

@Service
@DS("operating")
public class XXXXXXBasicServiceImpl2 implements IXXXBasicService2 {


spring:
  datasource:
    hikari:
      pool-name: hikariCP-biz
      auto-commit: false
      read-only: false
      maximum-pool-size: 10
      max-lifetime: 1800000
      connection-timeout: 30000
      idle-timeout: 600000
    #动态数据源配置
    dynamic:
      #主数据源
      primary: master
      datasource:
        #数据源a01
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://${MYSQL_URL:xxx:3306}/${MYSQL_DBNAME:xxx}?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai
          username: ${MYSQL_USER:root}
          password: ${MYSQL_PASSWORD:xxx}
        #运营中间库
        operating:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://${MYSQL_URL:xxx:3306}/${MYSQL_DBNAME:xxxxxx}?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai
          username: ${MYSQL_USER:root}
          password: ${MYSQL_PASSWORD:xxx}

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.8</version>
        </dependency>
       <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>

 

@MapperScan("com.xmh.mapper")
public class XmhApplication {

src\main\resources\mapper\XxxBasicMapper.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.xmh.mapper.XxxBasicMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.xmh.model.entity.XxxBasic">
        <id column="id" property="id"/>
        <result column="type" property="type"/>
        <result column="level" property="level"/>   
    </resultMap>

    <sql id="Basic_Column_List">   
        id, type, level, name
    </sql>

    <resultMap id="BasicResultMap" type="com.xmh.model.vo.XxxBasicVo">
        <id column="id" property="id"/>
        <result column="type" property="type"/>
        <result column="level" property="level"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="BasicNeed_Column_List">
        ab
        .
        `code`
        , ab.`name`, ab.type, ab.`level`, ab.dept_name as offerDept,
        slw.title, slw.dept_name as proposeDept, slw.create_name, slw.create_time, slw.id
    </sql>

    <select id="findBasicList" resultMap="BasicResultMap"
            parameterType="com.xmh.model.dto.XxxBasicPageDto">
        select
        <include refid="Basic_Column_List"/>
        from Xxx_basic
        <trim prefix="where" prefixOverrides="and|or">
            node = 1 and status != 2
            <if test="name!=null and name!=''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="code != null and code != ''">
                and code = #{code}
            </if>
            <if test="deptCode != null and deptCode != ''">
                and dept_code = #{deptCode}
            </if>
            <if test="keyword != null and keyword != ''">
                and (name like concat('%', #{keyword}, '%') or code like concat('%', #{keyword}, '%'))
            </if>
        </trim>
        <if test="orderBy == null or orderBy == ''">
            order by sort desc, update_time desc
        </if>
    </select>
</mapper>