该项目主要利用SpringBoot的自动化配置特性来实现快速的将Mybatis-Plus + 多数据源 + Redis二级缓存 + Phoenix引入Springboot项目中,简化原生Mybatis-Plus整合多数据源、Mybatis Redis二级缓存和Phoenix使用。
欢迎使用和star支持,如使用中碰到问题,可以提出Issue,我会尽力完善该Starter
- Mybatis-Plus: 3.2.0
- Spring-Boot: 2.2.2.RELEASE
- Druid: 1.1.16
轻松引入SpringBoot工程中,需要如下步骤:
- 在
pom.xml中引入依赖:
<dependency>
<groupId>com.github.flyingglass</groupId>
<artifactId>redis-mybatis-plus-starter</artifactId>
<version>1.0.0</version>
</dependency>- 在应用主类中增加
@MapperScan(basePackages = "com.xxx.mapper")注解,用于扫描Mapper的Interface,并且排除DruidDataSourceAutoConfigure
@MapperScan(basePackages = "com.xxx.mapper")
@SpringBootApplication(
exclude = DruidDataSourceAutoConfigure.class
)
public class Bootstrap {
public static void main(String[] args) {
SpringApplication.run(Bootstrap.class, args);
}
}- 配置
application.yml
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
reset-enable: true
url-pattern: /druid/*
allow:
deny:
web-stat-filter:
enabled: true
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
profile-enable: true
session-stat-enable: false
dynamic:
# 全局配置,数据源可覆盖
druid:
initial-size: 5 # 初始连接数
max-active: 10 # 最大连接池数量
min-idle: 5 # 最小连接池数量
max-wait: 60000
pool-prepared-statements: false
validation-query: "SELECT 1"
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 3000
min-evictable-idle-time-millis: 300000
# Filter
# wall不兼容phoenix
filters: stat
stat:
log-slow-sql: true
slow-sql-millis: 5000
merge-sql: true
primary: master
datasource:
master:
username: ${mysql.username}
password: ${mysql.password}
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/master_db
mybatis-plus:
# classpath要加上后面的*,表示通配符,匹配任意路径
mapper-locations: classpath*:/mapper/*Mapper.xml
typeAliasesPackage: com.fly.demo.entity
configuration:
map-underscore-to-camel-case: true
cache-enabled: true # 开启xml缓存
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto主要实现了MybatisRedisCache和LoggingRedisCache,其中LoggingRedisCache为MybatisRedisCache的装饰类,主要用日志输出,其中MybatisRedisCache实现了Mybatis二级缓存的Cache接口,通过flushInterval(精确到毫秒)参数控制缓存过期,缓存过期策略为Redis默认的lazy和定期删除策略,默认的过期策略可能expire时间会出现微小偏差,几乎可以忽略。
使用MybatisRedisCache需要如下配置:
- 在
application.yml添加Redis配置:
spring:
redis:
database: 0
host: localhost
port: 6379
timeout: 5000
jedis:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1- 配置
CacheNamespace,支持注解或xml配置(记得在Mybatis-Plus打开cache-enabled属性),演示注解例子:
@CacheNamespace(
implementation = MybatisRedisCache.class,
properties = { @Property(
name = "flushInterval",
value = "5000"
)}
)
public interface TestMapper extends BaseMapper<Test> {
}- 可自定义
RedisTemplate,控制Cache的序列化或者反序列,项目默认会注入spring-data-redis默认的RedisTemplate
- 简单样例,配置
application.yml
spring:
datasource:
dynamic:
primary: master
datasource:
master:
username: ${mysql.username}
password: ${mysql.password}
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${mysql.host}:${mysql.port}/master_db
slave_1:
username: ${mysql.username}
password: ${mysql.password}
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${mysql.host}:${mysql.port}/slave_db- 使用
@DS注解进行切换,建议在Service层添加注解,使用样例:
@DS("slave_1")
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {
}Starter中的BaseDO和DefaultMetaObjectHandler会默认填充gmtCreate和gmtUpdated字段,可以自定义MetaObjectHandler的Bean进行覆盖。Mybatis-Plus整合Phoenix