Skip to content

Commit

Permalink
优化zlt-db-spring-boot-starter自动装配MybatisPlus配置,去掉DefaultMybatisPlusCon…
Browse files Browse the repository at this point in the history
…fig类
  • Loading branch information
zlt2000 committed Apr 5, 2020
1 parent 3f946d2 commit 06b49c7
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 118 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,7 @@ import java.util.Map;
* @author ${author}
* @date ${datetime}
*/
@Mapper
public interface ${className}Mapper extends SuperMapper<${className}> {
/**
* 分页查询用户列表
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import com.central.db.mapper.SuperMapper;

import com.central.file.model.FileInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
* 上传存储db
*
* @author zlt
*/
@Mapper
public interface FileMapper extends SuperMapper<FileInfo> {
List<FileInfo> findList(Page<FileInfo> page, @Param("f") Map<String, Object> params);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

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> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -14,6 +15,7 @@
* @author zlt
* @data 2018-10-29
*/
@Mapper
public interface SysUserMapper extends SuperMapper<SysUser> {
/**
* 分页查询用户列表
Expand Down
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);
}
}
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);
}
}
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";
}
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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> {

}
2 changes: 1 addition & 1 deletion zlt-doc/sql/oauth-center.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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', '第三方应用');
Loading

0 comments on commit 06b49c7

Please sign in to comment.