说明

说明

开发工具默认集成spring data jpa 操作数据库,在实际项目使用不方便,现集成Mybatis以及Mybatis-Plus插件,使用HikariCP作为数据源。

一 、集成数据源

  • 添加以下jar包
HikariCP-2.6.3.jar
  • 数据源配置
spring.datasource.url=jdbc:mysql://ip:port/ctpsp
# 加密数据库连接信息
spring.datasource.username=ENC(ukdPxk2YeSrilKZFvsNr2g==)
spring.datasource.password=ENC(nK0oIOT1Mx6Kn27UhntqBg==)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.pool-name=DataBaseHikari
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1

二 、集成MyBatis和Mybatis-Plus插件

  • 添加以下jar包
jsqlparser-0.9.5.jar
mybatis-3.4.4.jar
mybatis-plus-2.1.9.jar
mybatis-plus-core-2.1.9.jar
mybatis-plus-generate-2.1.9.jar
mybatis-plus-support-2.1.9.jar
mybatis-spring-1.3.1.jar
mybatis-spring-boot-autoconfigure-1.3.0.jar
mybatis-spring-boot-starter-1.3.0.jar
mybatisplus-spring-boot-starter-1.0.5.jar
  • 配置

在启动类上添加mybati扫描路径以及注入分页插件

 @MapperScan("com.gdtnx.cloud.**.mapper")

在启动类中注入分页插件

 @Bean
 public PaginationInterceptor paginationInterceptor() {
   PaginationInterceptor page = new PaginationInterceptor();
   return page;
 }

配置文件中配置扫描Mbatis的XML文件路径

# mybatis-plus
mybatis-plus.mapper-locations = classpath:mapper/*.xml
mybatis-plus.typeAliasesPackage = com.gdtnx.cloud.**.domain

三 、使用

请参照具体代码和官方API文档

四、多数据源

多数据源源代码位于dysource包目录下

  • 在程序的主入口添加以下
@Import(DynamicDataSourceRegister.class)
public class Application {
  public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
  }
}
  • 配置文件中添加多数据源配置信息(用户名密码支持加密)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.25.163:3306/ctpsp?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
#spring.datasource.username=dept
#spring.datasource.password=novell
spring.datasource.username=ENC(ukdPxk2YeSrilKZFvsNr2g==)
spring.datasource.password=ENC(nK0oIOT1Mx6Kn27UhntqBg==)
spring.datasource.maximum-pool-size=80
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20

# 更多数据源
custom.datasource.names=ds1,ds2
custom.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
custom.datasource.ds1.url=jdbc:mysql://192.168.25.163:3306/paca?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
custom.datasource.ds1.username=dept
custom.datasource.ds1.password=ENC(nK0oIOT1Mx6Kn27UhntqBg==>)
custom.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource

custom.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver
custom.datasource.ds2.url=jdbc:mysql://192.168.25.163:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
custom.datasource.ds2.username=dept
custom.datasource.ds2.password=novell
custom.datasource.ds2.type=com.zaxxer.hikari.HikariDataSource
  • 使用
    在service层使用TargetDataSource注解切换数据源

你可能感兴趣的:(说明)