-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
优化zlt-db-spring-boot-starter自动装配MybatisPlus配置,去掉DefaultMybatisPlusCon…
…fig类
- Loading branch information
Showing
17 changed files
with
154 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
zlt-business/file-center/src/main/java/com/central/file/config/MybatisPlusConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
zlt-business/user-center/src/main/java/com/central/user/config/MybatisPlusConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 31 additions & 11 deletions
42
...zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DateMetaObjectHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,66 @@ | ||
package com.central.db.config; | ||
|
||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; | ||
import com.central.db.properties.MybatisPlusAutoFillProperties; | ||
import org.apache.ibatis.reflection.MetaObject; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* 自定义填充公共 name 字段 | ||
* 自定义填充公共字段 | ||
* | ||
* @author zlt | ||
* @date 2018/12/11 | ||
* <p> | ||
* Blog: https://blog.csdn.net/zlt2000 | ||
* Github: https://github.com/zlt2000 | ||
*/ | ||
public class DateMetaObjectHandler implements MetaObjectHandler { | ||
private final static String UPDATE_TIME = "updateTime"; | ||
private final static String CREATE_TIME = "createTime"; | ||
private MybatisPlusAutoFillProperties autoFillProperties; | ||
|
||
public DateMetaObjectHandler(MybatisPlusAutoFillProperties autoFillProperties) { | ||
this.autoFillProperties = autoFillProperties; | ||
} | ||
|
||
/** | ||
* 是否开启了插入填充 | ||
*/ | ||
@Override | ||
public boolean openInsertFill() { | ||
return autoFillProperties.getEnableInsertFill(); | ||
} | ||
|
||
/** | ||
* 是否开启了更新填充 | ||
*/ | ||
@Override | ||
public boolean openUpdateFill() { | ||
return autoFillProperties.getEnableUpdateFill(); | ||
} | ||
|
||
/** | ||
* 插入填充,字段为空自动填充 | ||
*/ | ||
@Override | ||
public void insertFill(MetaObject metaObject) { | ||
Object createTime = getFieldValByName(CREATE_TIME, metaObject); | ||
Object updateTime = getFieldValByName(UPDATE_TIME, metaObject); | ||
Object createTime = getFieldValByName(autoFillProperties.getCreateTimeField(), metaObject); | ||
Object updateTime = getFieldValByName(autoFillProperties.getUpdateTimeField(), metaObject); | ||
if (createTime == null || updateTime == null) { | ||
Date date = new Date(); | ||
if (createTime == null) { | ||
setFieldValByName(CREATE_TIME, date, metaObject); | ||
setFieldValByName(autoFillProperties.getCreateTimeField(), date, metaObject); | ||
} | ||
if (updateTime == null) { | ||
setFieldValByName(UPDATE_TIME, date, metaObject); | ||
setFieldValByName(autoFillProperties.getUpdateTimeField(), date, metaObject); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 更新填充 | ||
* | ||
* @param metaObject | ||
*/ | ||
@Override | ||
public void updateFill(MetaObject metaObject) { | ||
//mybatis-plus版本2.0.9+ | ||
setFieldValByName(UPDATE_TIME, new Date(), metaObject); | ||
setFieldValByName(autoFillProperties.getUpdateTimeField(), new Date(), metaObject); | ||
} | ||
} |
110 changes: 63 additions & 47 deletions
110
...l/db/config/DefaultMybatisPlusConfig.java → ...l/db/config/MybatisPlusAutoConfigure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,63 @@ | ||
package com.central.db.config; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; | ||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; | ||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; | ||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser; | ||
|
||
import com.central.common.properties.TenantProperties; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
/** | ||
* mybatis-plus配置 | ||
* @author zlt | ||
* @date 2018/12/13 | ||
*/ | ||
@Import(DateMetaObjectHandler.class) | ||
public class DefaultMybatisPlusConfig { | ||
@Autowired | ||
private TenantHandler tenantHandler; | ||
|
||
@Autowired | ||
private ISqlParserFilter sqlParserFilter; | ||
|
||
@Autowired | ||
private TenantProperties tenantProperties; | ||
|
||
/** | ||
* 分页插件,自动识别数据库类型 | ||
*/ | ||
@Bean | ||
public PaginationInterceptor paginationInterceptor() { | ||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); | ||
boolean enableTenant = tenantProperties.getEnable(); | ||
//是否开启多租户隔离 | ||
if (enableTenant) { | ||
TenantSqlParser tenantSqlParser = new TenantSqlParser() | ||
.setTenantHandler(tenantHandler); | ||
paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser)); | ||
paginationInterceptor.setSqlParserFilter(sqlParserFilter); | ||
} | ||
return paginationInterceptor; | ||
} | ||
} | ||
package com.central.db.config; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; | ||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; | ||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; | ||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; | ||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser; | ||
import com.central.common.properties.TenantProperties; | ||
import com.central.db.properties.MybatisPlusAutoFillProperties; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* mybatis-plus自动配置 | ||
* | ||
* @author zlt | ||
* @date 2020/4/5 | ||
* <p> | ||
* Blog: https://blog.csdn.net/zlt2000 | ||
* Github: https://github.com/zlt2000 | ||
*/ | ||
@EnableConfigurationProperties(MybatisPlusAutoFillProperties.class) | ||
public class MybatisPlusAutoConfigure { | ||
@Autowired | ||
private TenantHandler tenantHandler; | ||
|
||
@Autowired | ||
private ISqlParserFilter sqlParserFilter; | ||
|
||
@Autowired | ||
private TenantProperties tenantProperties; | ||
|
||
@Autowired | ||
private MybatisPlusAutoFillProperties autoFillProperties; | ||
|
||
/** | ||
* 分页插件,自动识别数据库类型 | ||
*/ | ||
@Bean | ||
public PaginationInterceptor paginationInterceptor() { | ||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); | ||
boolean enableTenant = tenantProperties.getEnable(); | ||
//是否开启多租户隔离 | ||
if (enableTenant) { | ||
TenantSqlParser tenantSqlParser = new TenantSqlParser() | ||
.setTenantHandler(tenantHandler); | ||
paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser)); | ||
paginationInterceptor.setSqlParserFilter(sqlParserFilter); | ||
} | ||
return paginationInterceptor; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
@ConditionalOnProperty(prefix = "zlt.mybatis-plus.auto-fill", name = "enabled", havingValue = "true", matchIfMissing = true) | ||
public MetaObjectHandler metaObjectHandler() { | ||
return new DateMetaObjectHandler(autoFillProperties); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...g-boot-starter/src/main/java/com/central/db/properties/MybatisPlusAutoFillProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.central.db.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
|
||
/** | ||
* mybatis-plus 配置 | ||
* | ||
* @author zlt | ||
* @date 2020/4/5 | ||
* <p> | ||
* Blog: https://blog.csdn.net/zlt2000 | ||
* Github: https://github.com/zlt2000 | ||
*/ | ||
@Setter | ||
@Getter | ||
@ConfigurationProperties(prefix = "zlt.mybatis-plus.auto-fill") | ||
@RefreshScope | ||
public class MybatisPlusAutoFillProperties { | ||
/** | ||
* 是否开启自动填充字段 | ||
*/ | ||
private Boolean enabled = true; | ||
/** | ||
* 是否开启了插入填充 | ||
*/ | ||
private Boolean enableInsertFill = true; | ||
/** | ||
* 是否开启了更新填充 | ||
*/ | ||
private Boolean enableUpdateFill = true; | ||
/** | ||
* 创建时间字段名 | ||
*/ | ||
private String createTimeField = "createTime"; | ||
/** | ||
* 更新时间字段名 | ||
*/ | ||
private String updateTimeField = "updateTime"; | ||
} |
3 changes: 2 additions & 1 deletion
3
zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.central.db.config.TenantAutoConfigure | ||
com.central.db.config.TenantAutoConfigure,\ | ||
com.central.db.config.MybatisPlusAutoConfigure |
13 changes: 0 additions & 13 deletions
13
zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/config/MybatisPlusConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.