diff --git a/README.md b/README.md index 08a49845..8edc1823 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/zlt-business/code-generator/src/main/resources/template/Mapper.java.vm b/zlt-business/code-generator/src/main/resources/template/Mapper.java.vm index bcb6f90d..23b7c320 100644 --- a/zlt-business/code-generator/src/main/resources/template/Mapper.java.vm +++ b/zlt-business/code-generator/src/main/resources/template/Mapper.java.vm @@ -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}> { /** * 分页查询用户列表 diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/MybatisPlusConfig.java b/zlt-business/file-center/src/main/java/com/central/file/config/MybatisPlusConfig.java deleted file mode 100644 index 7138c812..00000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/MybatisPlusConfig.java +++ /dev/null @@ -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 { - -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/mapper/FileMapper.java b/zlt-business/file-center/src/main/java/com/central/file/mapper/FileMapper.java index adf3caaa..00c1efc5 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/mapper/FileMapper.java +++ b/zlt-business/file-center/src/main/java/com/central/file/mapper/FileMapper.java @@ -7,6 +7,7 @@ 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 @@ * * @author zlt */ +@Mapper public interface FileMapper extends SuperMapper { List findList(Page page, @Param("f") Map params); } diff --git a/zlt-business/user-center/src/main/java/com/central/user/config/MybatisPlusConfig.java b/zlt-business/user-center/src/main/java/com/central/user/config/MybatisPlusConfig.java deleted file mode 100644 index a70877a1..00000000 --- a/zlt-business/user-center/src/main/java/com/central/user/config/MybatisPlusConfig.java +++ /dev/null @@ -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 { -} diff --git a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysMenuMapper.java b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysMenuMapper.java index d5465c22..3d86269d 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysMenuMapper.java +++ b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysMenuMapper.java @@ -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 { } diff --git a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysRoleMapper.java b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysRoleMapper.java index 2674f0ea..ee4f99b1 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysRoleMapper.java +++ b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysRoleMapper.java @@ -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 { List findList(Page page, @Param("r") Map params); diff --git a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserMapper.java b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserMapper.java index 14ef65d8..c492b893 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserMapper.java +++ b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserMapper.java @@ -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; @@ -14,6 +15,7 @@ * @author zlt * @data 2018-10-29 */ +@Mapper public interface SysUserMapper extends SuperMapper { /** * 分页查询用户列表 diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DateMetaObjectHandler.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DateMetaObjectHandler.java index b131ee21..5c258020 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DateMetaObjectHandler.java +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DateMetaObjectHandler.java @@ -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 + *

+ * 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); } } \ No newline at end of file diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java similarity index 59% rename from zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java rename to zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java index 65946940..05635297 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java @@ -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 + *

+ * 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); + } +} diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/properties/MybatisPlusAutoFillProperties.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/properties/MybatisPlusAutoFillProperties.java new file mode 100644 index 00000000..fe50bdd7 --- /dev/null +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/properties/MybatisPlusAutoFillProperties.java @@ -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 + *

+ * 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"; +} diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories index c928b621..a939175d 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.central.db.config.TenantAutoConfigure +com.central.db.config.TenantAutoConfigure,\ +com.central.db.config.MybatisPlusAutoConfigure diff --git a/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/config/MybatisPlusConfig.java b/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/config/MybatisPlusConfig.java deleted file mode 100644 index e92892f5..00000000 --- a/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/config/MybatisPlusConfig.java +++ /dev/null @@ -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 { -} diff --git a/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/mapper/UserMapper.java b/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/mapper/UserMapper.java index e55f3377..6b9fd211 100644 --- a/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/mapper/UserMapper.java +++ b/zlt-demo/sharding-jdbc-demo/src/main/java/com/sharding/demo/mapper/UserMapper.java @@ -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 { } diff --git a/zlt-doc/sql/oauth-center.sql b/zlt-doc/sql/oauth-center.sql index aad6b786..6ed53f27 100644 --- a/zlt-doc/sql/oauth-center.sql +++ b/zlt-doc/sql/oauth-center.sql @@ -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', '第三方应用'); \ No newline at end of file diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/MybatisPlusConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/MybatisPlusConfig.java deleted file mode 100644 index 3b17833a..00000000 --- a/zlt-uaa/src/main/java/com/central/oauth/config/MybatisPlusConfig.java +++ /dev/null @@ -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 { - -} diff --git a/zlt-uaa/src/main/java/com/central/oauth/mapper/ClientMapper.java b/zlt-uaa/src/main/java/com/central/oauth/mapper/ClientMapper.java index 90c6c168..e796322b 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/mapper/ClientMapper.java +++ b/zlt-uaa/src/main/java/com/central/oauth/mapper/ClientMapper.java @@ -5,6 +5,7 @@ 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 @@ /** * @author zlt */ +@Mapper public interface ClientMapper extends SuperMapper { List findList(Page page, @Param("params") Map params ); }