优化zlt-db-spring-boot-starter自动装配MybatisPlus配置,去掉DefaultMybatisPlusConfig类
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
* **详细在线文档** [点击查看](https://www.kancloud.cn/zlt2000/microservices-platform/936236)
|
||||
* **[项目更新日志](https://www.kancloud.cn/zlt2000/microservices-platform/936235)**
|
||||
* **[文档更新日志](https://www.kancloud.cn/zlt2000/microservices-platform/936236)**
|
||||
* **演示环境地址**: [http://mp.zlt2000.cn](http://mp.zlt2000.cn/)
|
||||
* **演示环境地址**: [http://zlt2000.cn](http://zlt2000.cn/)
|
||||
* 账号密码:admin/admin
|
||||
* APM监控账号密码:admin/admin
|
||||
* Grafana账号:zlt/zlt123
|
||||
|
||||
@@ -4,6 +4,7 @@ import ${package}.${moduleName}.model.${className};
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -14,6 +15,7 @@ import java.util.Map;
|
||||
* @author ${author}
|
||||
* @date ${datetime}
|
||||
*/
|
||||
@Mapper
|
||||
public interface ${className}Mapper extends SuperMapper<${className}> {
|
||||
/**
|
||||
* 分页查询用户列表
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.central.file.config;
|
||||
|
||||
import com.central.db.config.DefaultMybatisPlusConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/12/10
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan({"com.central.file.mapper*"})
|
||||
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
|
||||
import com.central.file.model.FileInfo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ import org.apache.ibatis.annotations.Param;
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Mapper
|
||||
public interface FileMapper extends SuperMapper<FileInfo> {
|
||||
List<FileInfo> findList(Page<FileInfo> page, @Param("f") Map<String, Object> params);
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.central.user.config;
|
||||
|
||||
import com.central.db.config.DefaultMybatisPlusConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/12/10
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan({"com.central.user.mapper*"})
|
||||
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
|
||||
}
|
||||
@@ -2,12 +2,14 @@ package com.central.user.mapper;
|
||||
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.central.common.model.SysMenu;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysMenuMapper extends SuperMapper<SysMenu> {
|
||||
|
||||
}
|
||||
|
||||
@@ -7,12 +7,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
|
||||
import com.central.common.model.SysRole;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 角色
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysRoleMapper extends SuperMapper<SysRole> {
|
||||
List<SysRole> findList(Page<SysRole> page, @Param("r") Map<String, Object> params);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.central.user.mapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.central.common.model.SysUser;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
@@ -14,6 +15,7 @@ import java.util.Map;
|
||||
* @author zlt
|
||||
* @data 2018-10-29
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysUserMapper extends SuperMapper<SysUser> {
|
||||
/**
|
||||
* 分页查询用户列表
|
||||
|
||||
+31
-11
@@ -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);
|
||||
}
|
||||
}
|
||||
+63
-47
@@ -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
@@ -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";
|
||||
}
|
||||
@@ -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
@@ -1,13 +0,0 @@
|
||||
package com.sharding.demo.config;
|
||||
|
||||
import com.central.db.config.DefaultMybatisPlusConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan({"com.sharding.demo.mapper*"})
|
||||
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
|
||||
}
|
||||
@@ -2,10 +2,12 @@ package com.sharding.demo.mapper;
|
||||
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.sharding.demo.model.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserMapper extends SuperMapper<User> {
|
||||
|
||||
}
|
||||
|
||||
@@ -29,5 +29,5 @@ CREATE TABLE `oauth_client_details` (
|
||||
-- Records of oauth_client_details
|
||||
-- ----------------------------
|
||||
INSERT INTO `oauth_client_details` VALUES (1, 'webApp', NULL, '$2a$10$06msMGYRH8nrm4iVnKFNKOoddB8wOwymVhbUzw/d3ZixD7Nq8ot72', 'webApp', 'app', 'authorization_code,password,refresh_token,client_credentials', NULL, NULL, 3600, NULL, '{}', 'true', NULL, NULL, 'pc端');
|
||||
INSERT INTO `oauth_client_details` VALUES (2, 'app', NULL, '$2a$10$i3F515wEDiB4Gvj9ym9Prui0dasRttEUQ9ink4Wpgb4zEDCAlV8zO', 'app', 'app', 'password,refresh_token', NULL, NULL, 3600, NULL, '{}', 'true', NULL, NULL, '移动端');
|
||||
INSERT INTO `oauth_client_details` VALUES (2, 'app', NULL, '$2a$10$i3F515wEDiB4Gvj9ym9Prui0dasRttEUQ9ink4Wpgb4zEDCAlV8zO', 'app', 'app', 'authorization_code,password,refresh_token', 'http://127.0.0.1:8081/callback.html', NULL, 3600, NULL, '{}', 'true', NULL, NULL, '移动端');
|
||||
INSERT INTO `oauth_client_details` VALUES (3, 'zlt', NULL, '$2a$10$/o.wuORzVcXaezmYVzwYMuoY7qeWXBALwQmkskXD/7C6rqfCyPrna', 'zlt', 'all', 'authorization_code,password,refresh_token,client_credentials', 'http://127.0.0.1:8080/singleLogin', NULL, 3600, 28800, '{}', 'true', '2018-12-27 00:50:30', '2018-12-27 00:50:30', '第三方应用');
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.central.oauth.config;
|
||||
|
||||
import com.central.db.config.DefaultMybatisPlusConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/12/10
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan({"com.central.oauth.mapper*"})
|
||||
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.central.oauth.model.Client;
|
||||
@@ -12,6 +13,7 @@ import com.central.oauth.model.Client;
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Mapper
|
||||
public interface ClientMapper extends SuperMapper<Client> {
|
||||
List<Client> findList(Page<Client> page, @Param("params") Map<String, Object> params );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user