溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何編寫mybatis-plus、hikariCP動(dòng)態(tài)數(shù)據(jù)源

發(fā)布時(shí)間:2021-10-12 14:42:01 來(lái)源:億速云 閱讀:459 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“如何編寫mybatis-plus、hikariCP動(dòng)態(tài)數(shù)據(jù)源”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何編寫mybatis-plus、hikariCP動(dòng)態(tài)數(shù)據(jù)源”吧!

最近的項(xiàng)目使用多數(shù)據(jù)源,于是想起之前mybatis-plus系列的dynamic-datasource-springboot-starter 由于springboot2.x使用hikari作為默認(rèn)數(shù)據(jù)庫(kù)連接池,就是用springboot2.X+mybatis-plus3.x+dynamic-datasource-springboot-starter3.0.0進(jìn)行了集成,代碼如下 MybatisConfiguration.java

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 {

    /**
     * 分頁(yè)插件注冊(cè)
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

}

BaseEntity.java

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

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

package com.share.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.share.domain.User;

 /**

- @desc:
- @author:caifan
- @date:2020/3/15
- /
public interface UserMapper extends BaseMapper<User> {
}

UserRest.java

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

package com.share.service;

  import com.baomidou.dynamic.datasource.annotation.DS;
  import com.share.domain.User;
  import com.share.mapper.UserMapper;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;

  import javax.annotation.Resource;

 @Service
 @Transactional
 @DS("ds1")
 public class UserService {
     @Resource
     private UserMapper userMapper;


     public void saveUser1(User user) {
         userMapper.insert(user);
     }
    
     public User findById(Long id) {
         return userMapper.selectById(id);
     }
    
     @DS("ds2")
     public void saveUser2(User user) {
         userMapper.insert(user);
     }


 }

MybatisApplication.java

 package com.share;

 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;

 @SpringBootApplication
 @MapperScan(value = {"com.share.mapper"})
 public class MybatisApplication {
     public static void main(String[] args) {
         SpringApplication.run(MybatisApplication.class, args);
     }
 }

配置文件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

#動(dòng)態(tài)數(shù)據(jù)源配置
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

到此,相信大家對(duì)“如何編寫mybatis-plus、hikariCP動(dòng)態(tài)數(shù)據(jù)源”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI