• mybatis-plus、hikariCP动态数据源


          最近的项目使用多数据源,于是想起之前mybatis-plus系列的dynamic-datasource-springboot-starter

    由于springboot2.x使用hikari作为默认数据库连接池,就是用springboot2.X+mybatis-plus3.x+dynamic-datasource-springboot-starter3.0.0进行了集成,代码如下

    MybatisConfiguration.java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.share.config;
     
    import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
     
    @Configuration
    public class MybatisConfiguration {
     
        /**
         * 分页插件注册
         * @return
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
     
    }

      BaseEntity.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.share.domain;
     
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableId;
    import lombok.Data;
     
    import java.io.Serializable;
    import java.util.Date;
     
     
    @Data
    public class BaseEntity implements Serializable {
        private static final long serialVersionUID = 5741450406019549099L;
        @TableId(type = IdType.AUTO)
        private Long id;
        @TableField(value = "create_time")
        private Date createTime;
        @TableField(value = "update_time")
        private Date updateTime;
        @TableField(value = "delete_time")
        private Date deleteTime;
     
    }

      User.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package com.share.domain;
     
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableLogic;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.baomidou.mybatisplus.annotation.Version;
    import lombok.Data;
     
    import java.util.Date;
     
     
    @Data
    @TableName(value ="user")
    public class User extends BaseEntity {
        @Version
        private Integer version;
        private String username;
        private String nickname;
        private String password;
        private String email;
        private String phone;
        private Integer age;
        private Integer gender;
        private Integer status;
        @TableField(value = "last_login_time")
        private Date lastLoginTime;
     
        @TableLogic(value = "0", delval = "1")
        private Integer del;
    }

      UserMapper.java

    复制代码
     1 package com.share.mapper;
     2 
     3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
     4 import com.share.domain.User;
     5 
     6 /**
     7  * @desc:
     8  * @author:caifan
     9  * @date:2020/3/15
    10  */
    11 public interface UserMapper extends BaseMapper<User> {
    12 }
    复制代码

    UserRest.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package com.share.rest;
     
    import com.share.domain.User;
    import com.share.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.util.Date;
     
    @RestController
    @RequestMapping("/rest/user")
    public class UserRest {
        @Autowired
        private UserService userService;
     
        @RequestMapping(value = "/save1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        public void save1(@RequestBody User user) {
            if (null == user.getUpdateTime()) {
                user.setUpdateTime(new Date());
            }
            userService.saveUser1(user);
        }
     
        @RequestMapping(value = "/save2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        public void save2(@RequestBody User user) {
            if (null == user.getUpdateTime()) {
                user.setUpdateTime(new Date());
            }
            userService.saveUser2(user);
        }
    }

      UserService.java

    复制代码
     1 package com.share.service;
     2 
     3 import com.baomidou.dynamic.datasource.annotation.DS;
     4 import com.share.domain.User;
     5 import com.share.mapper.UserMapper;
     6 import org.springframework.stereotype.Service;
     7 import org.springframework.transaction.annotation.Transactional;
     8 
     9 import javax.annotation.Resource;
    10 
    11 @Service
    12 @Transactional
    13 @DS("ds1")
    14 public class UserService {
    15     @Resource
    16     private UserMapper userMapper;
    17 
    18     public void saveUser1(User user) {
    19         userMapper.insert(user);
    20     }
    21 
    22     public User findById(Long id) {
    23         return userMapper.selectById(id);
    24     }
    25 
    26     @DS("ds2")
    27     public void saveUser2(User user) {
    28         userMapper.insert(user);
    29     }
    30 
    31 }
    复制代码

    MybatisApplication.java

    复制代码
     1 package com.share;
     2 
     3 import org.mybatis.spring.annotation.MapperScan;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 @SpringBootApplication
     8 @MapperScan(value = {"com.share.mapper"})
     9 public class MybatisApplication {
    10     public static void main(String[] args) {
    11         SpringApplication.run(MybatisApplication.class, args);
    12     }
    13 }
    复制代码

    配置文件application.properties

    复制代码
    server.port=9001
    
    mybatis-plus.global-config.db-config.id-type=auto
    mybatis-plus.global-config.db-config.logic-delete-field=del
    mybatis-plus.global-config.db-config.logic-delete-value=1
    mybatis-plus.global-config.db-config.logic-not-delete-value=0
    
    mybatis-plus.global-config.db-config.field-strategy=not_empty
    spring.datasource.dynamic.primary=ds1
    spring.datasource.dynamic.datasource.ds1.username=root
    spring.datasource.dynamic.datasource.ds1.password=123456
    spring.datasource.dynamic.datasource.ds1.url=jdbc:mysql://localhost:3306/demo_ds_0
    spring.datasource.dynamic.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
    
    #动态数据源配置
    spring.datasource.dynamic.datasource.ds1.hikari.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0
    spring.datasource.dynamic.datasource.ds1.hikari.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.dynamic.datasource.ds1.hikari.username=root
    spring.datasource.dynamic.datasource.ds1.hikari.password=123456
    spring.datasource.dynamic.datasource.ds1.hikari.connection-test-query=select 1 from dual
    spring.datasource.dynamic.datasource.ds1.hikari.is-auto-commit=true
    
    
    spring.datasource.dynamic.datasource.ds2.username=root
    spring.datasource.dynamic.datasource.ds2.password=123456
    spring.datasource.dynamic.datasource.ds2.url=jdbc:mysql://localhost:3306/demo_ds_1
    spring.datasource.dynamic.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.dynamic.datasource.ds2.hikari.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1
    spring.datasource.dynamic.datasource.ds2.hikari.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.dynamic.datasource.ds2.hikari.username=root
    spring.datasource.dynamic.datasource.ds2.hikari.password=123456
    复制代码

    完整代码

  • 相关阅读:
    ORM(四)应用.脚本管理工具
    一个人不只是在做事,还要多思考.
    报表管理系统。
    放在首页,希望你们帮我来解说一下这个简单的问题,有没有其它的实现方式。
    与数据绑定相关的接口
    在sqlserver视图中慎用table.*的方式。
    最近这段时间的杂想。
    Lily.Core组件介绍,第一讲 实体的生成
    一个简单的Tcp编程练习。
    SQL语句对象化,先看示例代码.
  • 原文地址:https://www.cnblogs.com/xiami2046/p/13866709.html
Copyright © 2020-2023  润新知