调试mybatis策略及代码模板.
This commit is contained in:
@@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
|
||||
import com.orion.ops.framework.common.constant.FilterOrderConst;
|
||||
import com.orion.ops.framework.common.filter.FilterCreator;
|
||||
import com.orion.ops.framework.common.security.SecurityHolder;
|
||||
import com.orion.ops.framework.mybatis.core.cache.CacheClearFilter;
|
||||
import com.orion.ops.framework.mybatis.core.handler.FieldFillHandler;
|
||||
import com.orion.ops.framework.mybatis.core.utils.DomainFillUtils;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
@@ -29,7 +31,9 @@ public class OrionMybatisAutoConfiguration {
|
||||
* @return 字段填充元数据处理器
|
||||
*/
|
||||
@Bean
|
||||
public MetaObjectHandler defaultMetaObjectHandler() {
|
||||
public MetaObjectHandler defaultMetaObjectHandler(SecurityHolder securityHolder) {
|
||||
// 设置填充工具参数
|
||||
DomainFillUtils.setSecurityHolder(securityHolder);
|
||||
return new FieldFillHandler();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ public class CacheClearFilter extends OncePerRequestFilter {
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
// 清理缓存
|
||||
// TODO TEST
|
||||
CacheHolder.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,34 +21,34 @@ public class CacheHolder {
|
||||
/**
|
||||
* 缓存
|
||||
* <p>
|
||||
* key: mapperClass
|
||||
* key: mapper
|
||||
* value: id > row
|
||||
*/
|
||||
private static final ThreadLocal<MultiHashMap<Class<? extends BaseMapper<?>>, Serializable, Store<?>>> HOLDER = ThreadLocal.withInitial(MultiHashMap::new);
|
||||
private static final ThreadLocal<MultiHashMap<BaseMapper<?>, Serializable, Store<?>>> HOLDER = ThreadLocal.withInitial(MultiHashMap::new);
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
*
|
||||
* @param mapperClass mapperClass
|
||||
* @param id id
|
||||
* @param <T> domain
|
||||
* @param mapper mapper
|
||||
* @param id id
|
||||
* @param <T> domain
|
||||
* @return cacheWrapper
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Store<T> get(Class<? extends BaseMapper<?>> mapperClass, Serializable id) {
|
||||
return (Store<T>) HOLDER.get().get(mapperClass, id);
|
||||
public static <T> Store<T> get(BaseMapper<?> mapper, Serializable id) {
|
||||
return (Store<T>) HOLDER.get().get(mapper, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存
|
||||
*
|
||||
* @param mapperClass mapperClass
|
||||
* @param id id
|
||||
* @param row row
|
||||
* @param <T> domainClass
|
||||
* @param mapper mapper
|
||||
* @param id id
|
||||
* @param row row
|
||||
* @param <T> domainClass
|
||||
*/
|
||||
public static <T> void set(Class<? extends BaseMapper<T>> mapperClass, Serializable id, T row) {
|
||||
HOLDER.get().put(mapperClass, id, new Store<>(row));
|
||||
public static <T> void set(BaseMapper<T> mapper, Serializable id, T row) {
|
||||
HOLDER.get().put(mapper, id, new Store<>(row));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,6 +42,7 @@ public class BaseDO implements Serializable {
|
||||
*/
|
||||
@TableLogic
|
||||
@Schema(description = "是否删除 0未删除 1已删除")
|
||||
@TableField(fill = FieldFill.INSERT, jdbcType = JdbcType.BIT)
|
||||
private Boolean deleted;
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,13 @@ import com.orion.ops.framework.mybatis.core.domain.BaseDO;
|
||||
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -31,13 +35,19 @@ import java.util.stream.Collectors;
|
||||
public class CodeGenerator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@NotNull
|
||||
String outputDir = "D:/MP/";
|
||||
@NotNull
|
||||
String author = Const.ORION_AUTHOR;
|
||||
// 表名
|
||||
String[] tables = {"test_table"};
|
||||
@NotEmpty
|
||||
String[] tables = {"test_table", "table_copy"};
|
||||
// 表业务注释 需要和表一一对应 null则为表注释
|
||||
@NotEmpty
|
||||
String[] comment = {"用户", "复制"};
|
||||
// 模块
|
||||
@NotNull
|
||||
String module = "infra";
|
||||
// 连接
|
||||
// jdbc 配置 - 使用配置文件
|
||||
File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml");
|
||||
YmlExt yaml = YmlExt.load(yamlFile);
|
||||
String url = yaml.getValue("spring.datasource.druid.url");
|
||||
@@ -47,8 +57,7 @@ public class CodeGenerator {
|
||||
// 执行
|
||||
runGenerator(outputDir, author,
|
||||
url, username, password,
|
||||
tables,
|
||||
module);
|
||||
tables, comment, module);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +69,11 @@ public class CodeGenerator {
|
||||
String username,
|
||||
String password,
|
||||
String[] tables,
|
||||
String[] comment,
|
||||
String module) {
|
||||
// 创建引擎
|
||||
VelocityTemplateEngine engine = getEngine(tables, comment);
|
||||
|
||||
// 获取全局配置
|
||||
GlobalConfig globalConfig = getGlobalConfig(outputDir, author);
|
||||
|
||||
@@ -93,7 +106,26 @@ public class CodeGenerator {
|
||||
.injection(injectionConfig);
|
||||
|
||||
// 执行
|
||||
ag.execute(new VelocityTemplateEngine());
|
||||
ag.execute(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取渲染引擎
|
||||
*
|
||||
* @param tables 表
|
||||
* @param comment 表注释
|
||||
* @return
|
||||
*/
|
||||
private static VelocityTemplateEngine getEngine(String[] tables, String[] comment) {
|
||||
if (tables.length != comment.length) {
|
||||
throw new IllegalArgumentException("表称与业务注释长度不匹配");
|
||||
}
|
||||
// 业务注释
|
||||
Map<String, String> tableComment = new HashMap<>();
|
||||
for (int i = 0; i < tables.length; i++) {
|
||||
tableComment.put(tables[i], comment[i]);
|
||||
}
|
||||
return new VelocityTemplateEngine(tableComment);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,6 +305,8 @@ public class CodeGenerator {
|
||||
*/
|
||||
private static InjectionConfig getInjectionConfig() {
|
||||
String[][] customFileDefineArr = new String[][]{
|
||||
// http 文件
|
||||
new String[]{"/templates/orion-controller.http.vm", "%sController.http", "controller"},
|
||||
// vo 文件
|
||||
new String[]{"/templates/orion-entity-vo.java.vm", "%sVO.java", "entity.vo"},
|
||||
// dto 文件
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
|
||||
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
|
||||
import com.orion.lang.define.Console;
|
||||
import com.orion.lang.utils.Strings;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
@@ -35,6 +34,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@@ -49,8 +49,14 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
|
||||
private Map<String, String> tableComment;
|
||||
|
||||
private VelocityEngine velocityEngine;
|
||||
|
||||
public VelocityTemplateEngine(Map<String, String> tableComment) {
|
||||
this.tableComment = tableComment;
|
||||
}
|
||||
|
||||
{
|
||||
try {
|
||||
Class.forName("org.apache.velocity.util.DuckType");
|
||||
@@ -62,7 +68,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
@Override
|
||||
@NotNull
|
||||
public VelocityTemplateEngine init(@NotNull ConfigBuilder configBuilder) {
|
||||
if (null == velocityEngine) {
|
||||
if (velocityEngine == null) {
|
||||
Properties p = new Properties();
|
||||
p.setProperty(ConstVal.VM_LOAD_PATH_KEY, ConstVal.VM_LOAD_PATH_VALUE);
|
||||
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, StringPool.EMPTY);
|
||||
@@ -92,6 +98,24 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
return filePath.endsWith(dotVm) ? filePath : filePath + dotVm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入 api 注释
|
||||
*
|
||||
* @param tableInfo tableInfo
|
||||
* @param objectMap objectMap
|
||||
*/
|
||||
private void putApiComment(TableInfo tableInfo, Map<String, Object> objectMap) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
objectMap.put("apiComment", map);
|
||||
String comment = tableInfo.getComment();
|
||||
map.put("create", "创建" + comment);
|
||||
map.put("update", "通过 id 更新" + comment);
|
||||
map.put("get", "通过 id 查询" + comment);
|
||||
map.put("list", "通过 id 批量查询" + comment);
|
||||
map.put("query", "分页查询" + comment);
|
||||
map.put("delete", "通过 id 删除" + comment);
|
||||
map.put("batchDelete", "通过 id 批量删除" + comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出自定义模板文件
|
||||
@@ -103,13 +127,24 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
*/
|
||||
@Override
|
||||
protected void outputCustomFile(@NotNull List<CustomFile> customFiles, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
|
||||
// 替换业务注释
|
||||
String comment = tableComment.get(tableInfo.getName());
|
||||
if (comment != null) {
|
||||
tableInfo.setComment(comment);
|
||||
}
|
||||
|
||||
// http 注释标识
|
||||
objectMap.put("httpComment", "###");
|
||||
|
||||
// 实体名称
|
||||
String domainName = tableInfo.getEntityName();
|
||||
String mappingHyphen = objectMap.get("controllerMappingHyphen").toString();
|
||||
// 实际实体名称
|
||||
String entityName = domainName.substring(0, domainName.length() - 2);
|
||||
objectMap.put("type", entityName);
|
||||
objectMap.put("typeLower", Strings.firstLower(entityName));
|
||||
objectMap.put("typeHyphen", mappingHyphen.substring(0, mappingHyphen.length() - 3));
|
||||
// 注释
|
||||
this.putApiComment(tableInfo, objectMap);
|
||||
|
||||
// 自定义文件的包
|
||||
List<String> customFilePackages = customFiles.stream()
|
||||
@@ -128,7 +163,6 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
String currentPackage = getConfigBuilder().getPackageConfig().getParent() + "." + file.getPackageName();
|
||||
// 设置当前包
|
||||
objectMap.put("currentPackage", currentPackage);
|
||||
objectMap.forEach(Console::trace);
|
||||
|
||||
// 文件路径
|
||||
String filePath = StringUtils.isNotBlank(file.getFilePath()) ? file.getFilePath() : parentPath;
|
||||
@@ -140,4 +174,5 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
outputFile(new File(fileName), objectMap, file.getTemplatePath(), file.isFileOverride());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.orion.ops.framework.mybatis.core.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.orion.ops.framework.common.security.SecurityHolder;
|
||||
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
|
||||
import com.orion.ops.framework.mybatis.core.utils.DomainFillUtils;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -18,53 +16,19 @@ import java.util.Objects;
|
||||
*/
|
||||
public class FieldFillHandler implements MetaObjectHandler {
|
||||
|
||||
@Resource
|
||||
private SecurityHolder securityHolder;
|
||||
|
||||
// TODO 删除fill baseConst
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO) {
|
||||
BaseDO baseDO = (BaseDO) metaObject.getOriginalObject();
|
||||
|
||||
Date now = new Date();
|
||||
// 创建时间
|
||||
if (Objects.isNull(baseDO.getCreateTime())) {
|
||||
baseDO.setCreateTime(now);
|
||||
}
|
||||
// 更新时间
|
||||
if (Objects.isNull(baseDO.getUpdateTime())) {
|
||||
baseDO.setUpdateTime(now);
|
||||
}
|
||||
|
||||
Long userId = securityHolder.getLoginUserId();
|
||||
// 创建人
|
||||
if (Objects.nonNull(userId) && Objects.isNull(baseDO.getCreator())) {
|
||||
baseDO.setCreator(userId.toString());
|
||||
}
|
||||
// 更新人
|
||||
if (Objects.nonNull(userId) && Objects.isNull(baseDO.getUpdater())) {
|
||||
baseDO.setUpdater(userId.toString());
|
||||
}
|
||||
// 填充插入
|
||||
DomainFillUtils.fillInsert((BaseDO) metaObject.getOriginalObject());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO) {
|
||||
// 更新时间
|
||||
Object updateTime = getFieldValByName("updateTime", metaObject);
|
||||
if (Objects.isNull(updateTime)) {
|
||||
setFieldValByName("updateTime", new Date(), metaObject);
|
||||
}
|
||||
|
||||
// 更新人
|
||||
Object updater = getFieldValByName("updater", metaObject);
|
||||
Long userId = securityHolder.getLoginUserId();
|
||||
if (Objects.nonNull(userId) && Objects.isNull(updater)) {
|
||||
setFieldValByName("updater", userId.toString(), metaObject);
|
||||
}
|
||||
// 填充更新
|
||||
DomainFillUtils.fillUpdate((BaseDO) metaObject.getOriginalObject());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@ import java.util.function.Function;
|
||||
* 缓存查询器
|
||||
* <p>
|
||||
* 查询会存入缓存
|
||||
* <p>
|
||||
* TODO test
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -58,10 +56,12 @@ public class CacheQuery<T> {
|
||||
/**
|
||||
* 强制查询
|
||||
*
|
||||
* @param id id
|
||||
* @return this
|
||||
*/
|
||||
public CacheQuery<T> force() {
|
||||
public CacheQuery<T> force(Serializable id) {
|
||||
this.force = true;
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -70,20 +70,20 @@ public class CacheQuery<T> {
|
||||
return this.get().map(mapper);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Optional<T> get() {
|
||||
Class<? extends BaseMapper<T>> mapperClass = (Class<? extends BaseMapper<T>>) dao.getClass();
|
||||
// 不查询缓存
|
||||
if (!force) {
|
||||
// 从缓存中获取
|
||||
Store<T> store = CacheHolder.get(mapperClass, id);
|
||||
return Optional.ofNullable(store)
|
||||
.map(Store::get);
|
||||
Store<T> store = CacheHolder.get(dao, id);
|
||||
// 命中直接返回
|
||||
if (store != null) {
|
||||
return Optional.of(store).map(Store::get);
|
||||
}
|
||||
}
|
||||
// 查询
|
||||
T row = dao.selectById(id);
|
||||
// 设置缓存
|
||||
CacheHolder.set(mapperClass, id, row);
|
||||
CacheHolder.set(dao, id, row);
|
||||
return Optional.ofNullable(row);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,14 +16,13 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO TEST
|
||||
* varchar -> List<Integer>
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/25 10:33
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedJdbcTypes({JdbcType.CHAR, JdbcType.VARCHAR})
|
||||
@MappedTypes(List.class)
|
||||
public class IntegerListTypeHandler implements ITypeHandler<String, List<Integer>> {
|
||||
|
||||
|
||||
@@ -12,20 +12,18 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* TODO TEST
|
||||
* varchar -> JSONArray
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/25 10:33
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedJdbcTypes({JdbcType.CHAR, JdbcType.VARCHAR})
|
||||
@MappedTypes(JSONArray.class)
|
||||
public class JSONArrayTypeHandler implements ITypeHandler<String, JSONArray> {
|
||||
|
||||
@Override
|
||||
public void setParameter(PreparedStatement ps, int i, JSONArray res, JdbcType jdbcType) throws SQLException {
|
||||
// todo TEST NULL
|
||||
// 设置占位符
|
||||
ps.setString(i, res.toString());
|
||||
}
|
||||
|
||||
@@ -12,20 +12,18 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* TODO TEST
|
||||
* varchar -> JSONObject
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/25 10:33
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedJdbcTypes({JdbcType.CHAR, JdbcType.VARCHAR})
|
||||
@MappedTypes(JSONObject.class)
|
||||
public class JSONObjectTypeHandler implements ITypeHandler<String, JSONObject> {
|
||||
|
||||
@Override
|
||||
public void setParameter(PreparedStatement ps, int i, JSONObject res, JdbcType jdbcType) throws SQLException {
|
||||
// todo TEST NULL
|
||||
// 设置占位符
|
||||
ps.setString(i, res.toString());
|
||||
}
|
||||
|
||||
@@ -16,14 +16,13 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO TEST
|
||||
* varchar -> List<Long>
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/25 10:33
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedJdbcTypes({JdbcType.CHAR, JdbcType.VARCHAR})
|
||||
@MappedTypes(List.class)
|
||||
public class LongListTypeHandler implements ITypeHandler<String, List<Long>> {
|
||||
|
||||
|
||||
@@ -13,14 +13,13 @@ import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* TODO TEST
|
||||
* varchar -> List<String>
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/25 10:33
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedJdbcTypes({JdbcType.CHAR, JdbcType.VARCHAR})
|
||||
@MappedTypes(List.class)
|
||||
public class StringListTypeHandler implements ITypeHandler<String, List<String>> {
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.orion.ops.framework.mybatis.core.utils;
|
||||
|
||||
import com.orion.ops.framework.common.security.SecurityHolder;
|
||||
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 对象填充器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/7/13 10:54
|
||||
*/
|
||||
public class DomainFillUtils {
|
||||
|
||||
/**
|
||||
* 安全信息持有者
|
||||
*/
|
||||
private static SecurityHolder securityHolder;
|
||||
|
||||
/**
|
||||
* 填充插入
|
||||
*
|
||||
* @param baseDO baseDO
|
||||
*/
|
||||
public static void fillInsert(BaseDO baseDO) {
|
||||
Date now = new Date();
|
||||
// 创建时间
|
||||
if (Objects.isNull(baseDO.getCreateTime())) {
|
||||
baseDO.setCreateTime(now);
|
||||
}
|
||||
// 更新时间
|
||||
if (Objects.isNull(baseDO.getUpdateTime())) {
|
||||
baseDO.setUpdateTime(now);
|
||||
}
|
||||
|
||||
Long userId = securityHolder.getLoginUserId();
|
||||
// 创建人
|
||||
if (Objects.nonNull(userId) && Objects.isNull(baseDO.getCreator())) {
|
||||
baseDO.setCreator(userId.toString());
|
||||
}
|
||||
// 更新人
|
||||
if (Objects.nonNull(userId) && Objects.isNull(baseDO.getUpdater())) {
|
||||
baseDO.setUpdater(userId.toString());
|
||||
}
|
||||
// 逻辑删除字段
|
||||
if (Objects.isNull(baseDO.getDeleted())) {
|
||||
baseDO.setDeleted(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充更新
|
||||
*
|
||||
* @param baseDO baseDO
|
||||
*/
|
||||
public static void fillUpdate(BaseDO baseDO) {
|
||||
// 更新时间
|
||||
if (Objects.isNull(baseDO.getUpdateTime())) {
|
||||
baseDO.setUpdateTime(new Date());
|
||||
}
|
||||
// 更新人
|
||||
Long userId = securityHolder.getLoginUserId();
|
||||
if (Objects.nonNull(userId) && Objects.isNull(baseDO.getUpdater())) {
|
||||
baseDO.setUpdater(userId.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static void setSecurityHolder(SecurityHolder securityHolder) {
|
||||
DomainFillUtils.securityHolder = securityHolder;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
${httpComment} ${apiComment.create}
|
||||
POST {{baseUrl}}/${package.ModuleName}/${typeHyphen}/create
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
#foreach($field in ${table.fields})
|
||||
#if("$!field.propertyName" != "id")
|
||||
"${field.propertyName}": ""#if($foreach.hasNext),#end
|
||||
#end
|
||||
#end
|
||||
}
|
||||
|
||||
|
||||
${httpComment} ${apiComment.update}
|
||||
PUT {{baseUrl}}/${package.ModuleName}/${typeHyphen}/update
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
#foreach($field in ${table.fields})
|
||||
"${field.propertyName}": ""#if($foreach.hasNext),#end
|
||||
#end
|
||||
}
|
||||
|
||||
|
||||
${httpComment} ${apiComment.get}
|
||||
GET {{baseUrl}}/${package.ModuleName}/${typeHyphen}/get?id=1
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
${httpComment} ${apiComment.list}
|
||||
GET {{baseUrl}}/${package.ModuleName}/${typeHyphen}/list?idList=1,2,3
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
${httpComment} ${apiComment.query}
|
||||
POST {{baseUrl}}/${package.ModuleName}/${typeHyphen}/query
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
#foreach($field in ${table.fields})
|
||||
"${field.propertyName}": ""#if($foreach.hasNext),#end
|
||||
#end
|
||||
}
|
||||
|
||||
|
||||
${httpComment} ${apiComment.delete}
|
||||
DELETE {{baseUrl}}/${package.ModuleName}/${typeHyphen}/delete?id=1
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
${httpComment} ${apiComment.batchDelete}
|
||||
DELETE {{baseUrl}}/${package.ModuleName}/${typeHyphen}/delete-batch?idList=1,2,3
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
|
||||
@@ -44,21 +44,21 @@ public class ${table.controllerName} {
|
||||
private ${type}Service ${typeLower}Service;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.create}")
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:create')")
|
||||
public Long create${type}(@Validated @RequestBody ${type}CreateRequest request) {
|
||||
return ${typeLower}Service.create${type}(request);
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.update}")
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:update')")
|
||||
public Integer update${type}(@Validated @RequestBody ${type}UpdateRequest request) {
|
||||
return ${typeLower}Service.update${type}(request);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "通过id查询$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.get}")
|
||||
@Parameter(name = "id", description = "id", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
|
||||
public ${type}VO get${type}(@RequestParam("id") Long id) {
|
||||
@@ -66,7 +66,7 @@ public class ${table.controllerName} {
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "通过id批量查询$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.list}")
|
||||
@Parameter(name = "idList", description = "idList", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
|
||||
public List<${type}VO> get${type}List(@RequestParam("idList") List<Long> idList) {
|
||||
@@ -74,14 +74,14 @@ public class ${table.controllerName} {
|
||||
}
|
||||
|
||||
@PostMapping("/query")
|
||||
@Operation(summary = "分页查询$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.query}")
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
|
||||
public DataGrid<${type}VO> get${type}Page(@Validated @RequestBody ${type}QueryRequest request) {
|
||||
return ${typeLower}Service.get${type}Page(request);
|
||||
}
|
||||
|
||||
@PutMapping("/delete")
|
||||
@Operation(summary = "删除$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.delete}")
|
||||
@Parameter(name = "id", description = "id", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:delete')")
|
||||
public Integer delete${type}(@RequestParam("id") Long id) {
|
||||
@@ -89,7 +89,7 @@ public class ${table.controllerName} {
|
||||
}
|
||||
|
||||
@PutMapping("/delete-batch")
|
||||
@Operation(summary = "批量删除$!{table.comment}")
|
||||
@Operation(summary = "${apiComment.batchDelete}")
|
||||
@Parameter(name = "idList", description = "idList", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:delete')")
|
||||
public Integer batchDelete${type}(@RequestParam("idList") List<Long> idList) {
|
||||
|
||||
@@ -8,7 +8,7 @@ import lombok.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* $!{table.comment} 数据库对象
|
||||
* $!{table.comment} 实体对象
|
||||
*
|
||||
* @author ${author}
|
||||
* @version 1.0.0
|
||||
@@ -20,9 +20,9 @@ import java.util.*;
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
#if(${table.convert})
|
||||
@TableName("${schemaName}${table.name}")
|
||||
@TableName(value = "${schemaName}${table.name}", autoResultMap = true)
|
||||
#end
|
||||
@Schema(name = "${entity}", description = "$!{table.comment} 数据库对象")
|
||||
@Schema(name = "${entity}", description = "$!{table.comment} 实体对象")
|
||||
#if(${superEntityClass})
|
||||
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
|
||||
#elseif(${entitySerialVersionUID})
|
||||
|
||||
@@ -22,12 +22,14 @@ import java.util.*;
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "${type}CreateRequest", description = "$!{table.comment} 创建请求对象")
|
||||
public class ${type}CreateRequest implements Serializable {
|
||||
#foreach($field in ${table.fields})
|
||||
|
||||
#foreach($field in ${table.fields})
|
||||
#if("$!field.propertyName" != "id")
|
||||
#if("$!field.comment" != "")
|
||||
@Schema(description = "${field.comment}")
|
||||
#end
|
||||
private ${field.propertyType} ${field.propertyName};
|
||||
#end
|
||||
|
||||
#end
|
||||
#end
|
||||
}
|
||||
|
||||
@@ -29,10 +29,7 @@
|
||||
#if(${baseColumnList})
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
#foreach($field in ${table.commonFields})
|
||||
${field.columnName},
|
||||
#end
|
||||
${table.fieldNames}
|
||||
#foreach($field in ${table.commonFields})#if(${foreach.count} == 1) #end${field.columnName}, #end${table.fieldNames}
|
||||
</sql>
|
||||
|
||||
#end
|
||||
|
||||
@@ -31,8 +31,6 @@ import java.util.List;
|
||||
@Service
|
||||
public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
|
||||
// TODO 需要自行实现 page 和 check 方法
|
||||
|
||||
@Resource
|
||||
private ${type}DAO ${typeLower}DAO;
|
||||
|
||||
@@ -42,7 +40,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
${type}DO record = ${type}Convert.MAPPER.to(request);
|
||||
record.setId(null);
|
||||
// 查询是否存在
|
||||
this.checkTestTablePresent(record);
|
||||
this.check${type}Present(record);
|
||||
// 插入
|
||||
int effect = ${typeLower}DAO.insert(record);
|
||||
log.info("${type}Service-add${type} effect: {}, domain: {}", effect, JSON.toJSONString(record));
|
||||
@@ -55,7 +53,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
${type}DO record = ${type}Convert.MAPPER.to(request);
|
||||
Valid.notNull(record.getId(), ErrorMessage.ID_MISSING);
|
||||
// 查询是否存在
|
||||
this.checkTestTablePresent(record);
|
||||
this.check${type}Present(record);
|
||||
// 更新
|
||||
int effect = ${typeLower}DAO.updateById(record);
|
||||
log.info("${type}Service-update${type} effect: {}, domain: {}", effect, JSON.toJSONString(record));
|
||||
@@ -87,7 +85,10 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
@Override
|
||||
public DataGrid<${type}VO> get${type}Page(${type}QueryRequest request) {
|
||||
// 构造条件
|
||||
LambdaQueryWrapper<${type}DO> wrapper = Conditions.wrapper(${type}DO.class);
|
||||
LambdaQueryWrapper<${type}DO> wrapper = Conditions.wrapper(${type}DO.class)
|
||||
#foreach($field in ${table.fields})
|
||||
.eq(${type}DO::get${field.capitalName}, request.get${field.capitalName}())#if(!$foreach.hasNext);#end
|
||||
#end
|
||||
// 查询
|
||||
return ${typeLower}DAO.of()
|
||||
.wrapper(wrapper)
|
||||
@@ -97,12 +98,16 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
|
||||
@Override
|
||||
public Integer delete${type}(Long id) {
|
||||
return ${typeLower}DAO.deleteById(id);
|
||||
int effect = ${typeLower}DAO.deleteById(id);
|
||||
log.info("${type}Service-delete${type} id: {}, effect: {}", id, effect);
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer batchDelete${type}(List<Long> idList) {
|
||||
return ${typeLower}DAO.deleteBatchIds(idList);
|
||||
int effect = ${typeLower}DAO.deleteBatchIds(idList);
|
||||
log.info("${type}Service-batchDelete${type} idList: {}, effect: {}", JSON.toJSONString(idList), effect);
|
||||
return effect;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,12 +118,16 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
private void check${type}Present(${type}DO domain) {
|
||||
// 构造条件
|
||||
LambdaQueryWrapper<${type}DO> wrapper = Conditions.wrapper(${type}DO.class)
|
||||
.eq(${type}DO::getId, domain.getId());
|
||||
// .eq(XXXDO::getXXX, domain.getXXX());
|
||||
// 更新时忽略当前记录
|
||||
.ne(${type}DO::getId, domain.getId())
|
||||
// 用其他字段做重复校验
|
||||
#foreach($field in ${table.fields})
|
||||
#if("$!field.propertyName" != "id")
|
||||
.eq(${type}DO::get${field.capitalName}, domain.get${field.capitalName}())#if(!$foreach.hasNext);#end
|
||||
#end
|
||||
#end
|
||||
// 检查是否存在
|
||||
boolean present = testTableDAO.of()
|
||||
.wrapper(wrapper)
|
||||
.present();
|
||||
boolean present = ${typeLower}DAO.of().wrapper(wrapper).present();
|
||||
if (present) {
|
||||
throw ErrorCode.DATA_PRESENT.exception();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.List;
|
||||
public interface ${table.serviceName} {
|
||||
|
||||
/**
|
||||
* 插入$!{table.comment}
|
||||
* ${apiComment.create}
|
||||
*
|
||||
* @param request request
|
||||
* @return id
|
||||
@@ -25,7 +25,7 @@ public interface ${table.serviceName} {
|
||||
Long create${type}(${type}CreateRequest request);
|
||||
|
||||
/**
|
||||
* 通过 id 更新$!{table.comment}
|
||||
* ${apiComment.update}
|
||||
*
|
||||
* @param request request
|
||||
* @return effect
|
||||
@@ -33,7 +33,7 @@ public interface ${table.serviceName} {
|
||||
Integer update${type}(${type}UpdateRequest request);
|
||||
|
||||
/**
|
||||
* 通过 id 查询$!{table.comment}
|
||||
* ${apiComment.get}
|
||||
*
|
||||
* @param id id
|
||||
* @return row
|
||||
@@ -41,7 +41,7 @@ public interface ${table.serviceName} {
|
||||
${type}VO get${type}(Long id);
|
||||
|
||||
/**
|
||||
* 通过 id 批量查询$!{table.comment}
|
||||
* ${apiComment.list}
|
||||
*
|
||||
* @param idList idList
|
||||
* @return rows
|
||||
@@ -49,7 +49,7 @@ public interface ${table.serviceName} {
|
||||
List<${type}VO> get${type}List(List<Long> idList);
|
||||
|
||||
/**
|
||||
* 分页查询$!{table.comment}
|
||||
* ${apiComment.query}
|
||||
*
|
||||
* @param request request
|
||||
* @return rows
|
||||
@@ -57,7 +57,7 @@ public interface ${table.serviceName} {
|
||||
DataGrid<${type}VO> get${type}Page(${type}QueryRequest request);
|
||||
|
||||
/**
|
||||
* 通过 id 删除$!{table.comment}
|
||||
* ${apiComment.delete}
|
||||
*
|
||||
* @param id id
|
||||
* @return effect
|
||||
@@ -65,7 +65,7 @@ public interface ${table.serviceName} {
|
||||
Integer delete${type}(Long id);
|
||||
|
||||
/**
|
||||
* 通过 id 批量删除$!{table.comment}
|
||||
* ${apiComment.batchDelete}
|
||||
*
|
||||
* @param idList idList
|
||||
* @return effect
|
||||
|
||||
@@ -151,7 +151,10 @@ public class OrionSwaggerAutoConfiguration {
|
||||
.name(HttpHeaders.AUTHORIZATION)
|
||||
.description("认证 Token")
|
||||
.in(String.valueOf(SecurityScheme.In.HEADER))
|
||||
.schema(new StringSchema()._default("Bearer ").name("NAME").description("认证 Token"));
|
||||
.schema(new StringSchema()
|
||||
._default("Bearer 1")
|
||||
.name(HttpHeaders.AUTHORIZATION)
|
||||
.description("认证 Token"));
|
||||
}
|
||||
|
||||
@Value("${orion.api.prefix}")
|
||||
|
||||
@@ -2,9 +2,3 @@
|
||||
GET {{baseUrl}}/server/bootstrap/health
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
###
|
||||
GET http://127.0.0.1:9200/infra/test-table/get
|
||||
Authorization: {{token}}
|
||||
|
||||
###
|
||||
|
||||
Reference in New Issue
Block a user