From 12c82c37acb998a3c269871e22e339d66b9f1c1d Mon Sep 17 00:00:00 2001 From: liulin <lin.liu@aliyun.com> Date: 星期五, 02 八月 2024 01:16:44 +0800 Subject: [PATCH] 自动生成代码 --- src/test/java/com/lunhan/xxx/host/GenCodeGauss.java | 134 +++++++++++++++++++++------------ sql/init.sql | 50 ++++++++++++ src/main/java/com/lunhan/xxx/service/TestInfoService.java | 8 +- 3 files changed, 138 insertions(+), 54 deletions(-) diff --git a/sql/init.sql b/sql/init.sql new file mode 100644 index 0000000..73c01d5 --- /dev/null +++ b/sql/init.sql @@ -0,0 +1,50 @@ +CREATE OR REPLACE FUNCTION "public"."update_timestamp_column"() + RETURNS "pg_catalog"."trigger" AS $BODY$ +BEGIN +NEW.timestamp_column := current_timestamp; +RETURN NEW; +END; +$BODY$ + LANGUAGE plpgsql VOLATILE + COST 100 + +-- test_info - 测试信息表 +DROP TABLE IF EXISTS "public"."test_info"; +CREATE TABLE "public"."test_info" ( + "id" int8 NOT NULL, + "name" varchar(50) COLLATE "pg_catalog"."default" NOT NULL DEFAULT NULL::character varying, + "user_id" int8 NOT NULL DEFAULT 0, + "phone" varchar(50) COLLATE "pg_catalog"."default" NOT NULL DEFAULT NULL::character varying, + "balance" numeric(18,4) NOT NULL DEFAULT 0, + "sex" int2 NOT NULL DEFAULT 0, + "birthday" varchar(50) COLLATE "pg_catalog"."default" NOT NULL DEFAULT NULL::character varying, + "sort" int4 NOT NULL DEFAULT 0, + "status" int4 NOT NULL DEFAULT 0, + "comment" varchar(500) COLLATE "pg_catalog"."default" NOT NULL DEFAULT NULL::character varying, + "create_time" int8 NOT NULL DEFAULT 0, + "update_time" timestamp(3) NOT NULL DEFAULT pg_systimestamp(), + "is_delete" int2 NOT NULL DEFAULT 0 +) +; +COMMENT ON TABLE "public"."test_info" IS '测试信息表'; +COMMENT ON COLUMN "public"."test_info"."id" IS '主键id'; +COMMENT ON COLUMN "public"."test_info"."name" IS '名称'; +COMMENT ON COLUMN "public"."test_info"."user_id" IS '用户id'; +COMMENT ON COLUMN "public"."test_info"."phone" IS '电话'; +COMMENT ON COLUMN "public"."test_info"."balance" IS 'balance余额'; +COMMENT ON COLUMN "public"."test_info"."sex" IS '性别 男=1,女=2,其他=3'; +COMMENT ON COLUMN "public"."test_info"."birthday" IS '生日'; +COMMENT ON COLUMN "public"."test_info"."sort" IS '排序值'; +COMMENT ON COLUMN "public"."test_info"."status" IS '状态 正常=200,停用=300,删除=400'; +COMMENT ON COLUMN "public"."test_info"."comment" IS '备注'; +COMMENT ON COLUMN "public"."test_info"."create_time" IS '数据创建时间'; +COMMENT ON COLUMN "public"."test_info"."update_time" IS '最后更新时间'; +COMMENT ON COLUMN "public"."test_info"."is_delete" IS '是否删除(逻辑删除)'; +-- 设置主键 +ALTER TABLE "public"."test_info" ADD CONSTRAINT "PK_test_info" PRIMARY KEY ("id"); +-- 索引 +CREATE INDEX "IDX_test_info_create_time" ON "public"."test_info"("create_time"); +-- 触发器:自动赋值最后更新时间 +CREATE TRIGGER "TG_test_info" BEFORE UPDATE OF "update_time" ON "public"."test_info" +FOR EACH ROW +EXECUTE PROCEDURE "public"."update_timestamp_column"(); diff --git a/src/main/java/com/lunhan/xxx/service/TestInfoService.java b/src/main/java/com/lunhan/xxx/service/TestInfoService.java index 8c282f6..e05a50c 100644 --- a/src/main/java/com/lunhan/xxx/service/TestInfoService.java +++ b/src/main/java/com/lunhan/xxx/service/TestInfoService.java @@ -129,7 +129,7 @@ } public ExecutedResult<String> listSetSort(ReqListSetSort request) { - // 类别id列表 + // id列表 List<Long> listId = request.getList().stream().map(ReqSetSort::getId).collect(Collectors.toList()); // 验证记录是否存在 ExecutedResult<List<TestInfoPO>> checkExists = this.check4Id(listId); @@ -221,16 +221,16 @@ return ExecutedResult.success(exists); } protected ExecutedResult<List<TestInfoPO>> check4Id(List<Long> listId) { - // 从数据库查找类别 + // 从数据库查找测试信息 List<TestInfoPO> list = mapper.getList(listId); if (ListUtil.isNullOrEmpty(list)) { return ExecutedResult.failed("[测试信息]不存在." + listId); } - // 数据库找到的类别id列表 + // 数据库找到的id列表 List<Long> listIdFind = list.stream().map(TestInfoPO::getId).collect(Collectors.toList()); // 数量不一致 if (listId.size() != listIdFind.size()) { - // 筛选数据库不存在的类别 + // 筛选数据库不存在的测试信息 List<Long> listIdNotFound = listId.stream().filter(c -> !listIdFind.contains(c)).collect(Collectors.toList()); if (ListUtil.isNullOrEmpty(list)) { return ExecutedResult.failed("[测试信息]不存在." + listIdNotFound); diff --git a/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java b/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java index 689a93f..de0f69b 100644 --- a/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java +++ b/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java @@ -79,7 +79,7 @@ private static final Integer SET_PO_Getter = 1; //endregion - //region po类头部文字设置 + //region 类头部文字设置 private static final String SET_BEFORE_TEXT = "/**\n" + "# __----~~~~~~~~~~~------___\n" + @@ -105,6 +105,8 @@ "*/"; //endregion + //region import依赖架包 + //region po类 import 内容设置 private static final String[] SET_PO_IMPORT = new String[]{ "import com.baomidou.mybatisplus.annotation.*;", @@ -122,7 +124,7 @@ }; //endregion - //region 实体映射类 import 内容设置 + //region 实体属性映射类 import 内容设置 private static final String[] SET_CONVERT_IMPORT = new String[]{ "import org.mapstruct.Mapper;" + "import org.mapstruct.factory.Mappers;" + @@ -131,18 +133,15 @@ }; //endregion - //region dao类 import 内容设置 - private static final String[] SET_DAO_IMPORT = new String[]{ + //region mapper实现类 import 内容设置 + private static final String[] SET_MAPPER_IMPL_IMPORT = new String[]{ "import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;", "import com.baomidou.mybatisplus.extension.plugins.pagination.Page;", "import com.lunhan.xxx.common.PagerResult;", "import com.lunhan.xxx.common.enums.EYesOrNo;", - "import com.lunhan.xxx.common.util.ListUtil;", - "import com.lunhan.xxx.common.util.NumericUtil;", - "import com.lunhan.xxx.common.util.StringUtil;", - "import com.lunhan.xxx.entity.dto.OrderByDTO;", - "import com.lunhan.xxx.entity.dto.SearchBasicDTO;", - "import com.lunhan.xxx.entity.enums.EOrderBy;", + "import com.lunhan.xxx.common.util.*;", + "import com.lunhan.xxx.entity.dto.*;", + "import com.lunhan.xxx.entity.enums.*;", "import com.lunhan.xxx.repository.BasicMapperImpl;", "import org.springframework.stereotype.Repository;" }; @@ -181,6 +180,8 @@ private static final String[] SET_SEARCH_IMPORT = new String[]{ "import " + GenCodeGauss.PackageSet.SEARCH.replaceAll("\\.search", "") + ".SearchBasicDTO;", }; + //endregion + //endregion //region 类注释模版 @@ -241,7 +242,7 @@ * 只生成以下配置的表 */ private static final List<String> ONLY_TABLES = Arrays.asList( - "water_meter_apply" + "admin_menus" ); public static void main(String[] args) { @@ -265,7 +266,7 @@ genSearch(columns, tableDesc); genMapper(columns, tableDesc); genModifyDto(columns, tableName, tableDesc); - genDao(tableName, tableDesc); + genMapperImpl(tableName, tableDesc); genConvertMapper(tableName, tableDesc); genService(tableName, tableDesc); genController(tableName, tableDesc); @@ -521,7 +522,7 @@ writeFile(GenCodeGauss.OutSet.SEARCH + "/", searchName + ".java", content.toString()); } - private static void genDao(String tableName, String tableDesc) { + private static void genMapperImpl(String tableName, String tableDesc) { String modelName = underline2Camel(tableName, true); String mapperName = modelName + SuffixSet.MAPPER; String daoName = modelName + GenCodeGauss.SuffixSet.MAPPER_IMPL; @@ -537,9 +538,9 @@ content.append(GenCodeGauss.PackageSet.MAPPER_IMPL); content.append(ConstantFactory.STR_SEMICOLON); content.append(ConstantFactory.STR_NEWLINE); - if (SET_DAO_IMPORT.length > 0) { + if (SET_MAPPER_IMPL_IMPORT.length > 0) { content.append(ConstantFactory.STR_NEWLINE); - for (String s : SET_DAO_IMPORT) { + for (String s : SET_MAPPER_IMPL_IMPORT) { content.append(s); content.append(ConstantFactory.STR_NEWLINE); } @@ -574,7 +575,7 @@ content.append("\t}\n"); content.append(ConstantFactory.STR_NEWLINE); content.append("\t@Override\n" + - "\tpublic PagerResult<" + poName + "> search(" + searchName + " request) {\n" + + "\tpublic PagerResult<" + poName + "> search(SearchBasicDTO request) {\n" + "\t\t// 还原查询条件真实类型\n" + "\t\t" + searchName + " search = (" + searchName + ")request;\n" + "\t\t// 查询条件\n" + @@ -591,12 +592,12 @@ "\t\t// 数据创建时间-截止\n" + "\t\tqueryWrapper.le(NumericUtil.tryParseLong(search.getCreateTimeEnd()).compareTo(0L) > 0, " + poName + "::getCreateTime, search.getCreateTimeEnd());\n" + "\t\t// 关键字\n" + - "\t\tif (StringUtil.isNotNullOrEmpty(search.getKeywords())) {\n" + - "\t\t\tqueryWrapper.and(q ->\n" + - "\t\t\t\tq.like(" + poName + "::getName, search.getKeywords())\n" + - "\t\t\t\t.or().like(" + poName + "::getPhone, search.getKeywords())\n" + - "\t\t\t);\n" + - "\t\t}\n" + + "\t\t//if (StringUtil.isNotNullOrEmpty(search.getKeywords())) {\n" + + "\t\t//\tqueryWrapper.and(q ->\n" + + "\t\t//\t\tq.like(" + poName + "::getName, search.getKeywords())\n" + + "\t\t//\t\t.or().like(" + poName + "::getPhone, search.getKeywords())\n" + + "\t\t//\t);\n" + + "\t\t//}\n" + "\n" + "\t\t// 排序处理\n" + "\t\tif (ListUtil.isNotNullOrEmpty(search.getOrderBy())) {\n" + @@ -980,7 +981,7 @@ content.append(serviceName); content.append(" extends BaseService {\n"); content.append(" @Autowired\n" + - " private " + daoName + " dao;\n"); + " private " + daoName + " mapper;\n"); content.append(ConstantFactory.STR_NEWLINE); content.append(" public ExecutedResult<Long> create(" + createDTOName + " request) {\n" + " // 转换po\n" + @@ -992,8 +993,8 @@ " // 是否删除(逻辑删除)初始值\n" + " //item.setIsDelete(EYesOrNo.NO.getValue());\n" + "\n" + - " Boolean result = this.dao.add(item);\n" + - " if (BooleanUtils.isFalse(result)) {\n" + + " int rowCount = mapper.insert(item);\n" + + " if (rowCount != 1) {\n" + " return ExecutedResult.failed(\"创建[" + tableDesc + "]失败。\");\n" + " }\n" + " return ExecutedResult.success(item.getId());\n" + @@ -1008,8 +1009,8 @@ " // 转换po\n" + " " + poName + " item = CopierUtil.mapTo(request, " + poName + ".class);\n" + "\n" + - " Boolean result = this.dao.modify(item);\n" + - " if (BooleanUtils.isFalse(result)) {\n" + + " int rowCount = mapper.updateById(item);\n" + + " if (rowCount != 1) {\n" + " return ExecutedResult.failed(\"编辑[" + tableDesc + "]失败。\");\n" + " }\n" + " return ExecutedResult.success();\n" + @@ -1018,7 +1019,7 @@ " public ExecutedResult<" + responseDTOName + "> get(Long id) {\n" + " " + responseDTOName + " result = new " + responseDTOName + "();\n" + "\n" + - " " + poName + " find = dao.getById(id);\n" + + " " + poName + " find = mapper.get(id);\n" + " if (null != find) {\n" + " result = CopierUtil.mapTo(find, " + responseDTOName + ".class);\n" + " }\n" + @@ -1035,8 +1036,8 @@ "// item.setId(id);\n" + "// item.setStatus(EState.DISABLED.getValue());\n" + "//\n" + - "// Boolean result = this.dao.modify(item);\n" + - "// if (BooleanUtils.isFalse(result)) {\n" + + "// int rowCount = mapper.updateById(item);\n" + + "// if (rowCount != 1) {\n" + "// return ExecutedResult.failed(\"停用[" + tableDesc + "]失败。\");\n" + "// }\n" + "// return ExecutedResult.success();\n" + @@ -1052,8 +1053,8 @@ "// item.setId(id);\n" + "// item.setStatus(EState.NORMAL.getValue());\n" + "//\n" + - "// Boolean result = this.dao.modify(item);\n" + - "// if (BooleanUtils.isFalse(result)) {\n" + + "// int rowCount = mapper.updateById(item);\\n\" +\n" + + "// if (rowCount != 1) {\n" + "// return ExecutedResult.failed(\"启用[" + tableDesc + "]失败。\");\n" + "// }\n" + "// return ExecutedResult.success();\n" + @@ -1069,15 +1070,39 @@ "// item.setId(request.getId());\n" + "// item.setSort(request.getSort());\n" + "//\n" + - "// Boolean result = this.dao.modify(item);\n" + - "// if (BooleanUtils.isFalse(result)) {\n" + + "// int rowCount = mapper.updateById(item);\\n\" +\n" + + "// if (rowCount != 1) {\n" + "// return ExecutedResult.failed(\"设置[" + tableDesc + "]排序值失败。\");\n" + "// }\n" + "// return ExecutedResult.success();\n" + "// }\n" + "//\n" + + "// public ExecutedResult<String> listSetSort(ReqListSetSort request) {\n" + + "// // id列表\n" + + "// List<Long> listId = request.getList().stream().map(ReqSetSort::getId).collect(Collectors.toList());\n" + + "// // 验证记录是否存在\n" + + "// ExecutedResult<List<" + poName + ">> checkExists = this.check4Id(listId);\n" + + "// if (checkExists.isFailed()) {\n" + + "// return ExecutedResult.failed(checkExists.getMsg());\n" + + "// }\n" + + "//\n" + + "// List<" + poName + "> listUpdate = request.getList().stream()\n" + + "// .map(c -> {\n" + + "// " + poName + " item = new " + poName + "();\n" + + "// item.setId(c.getId());\n" + + "// item.setSort(c.getSort());\n" + + "// return item;\n" + + "// })\n" + + "// .collect(Collectors.toList());\n" + + "// Boolean result = mapper.modifyList(listUpdate);\n" + + "// if (result) {\n" + + "// return ExecutedResult.success();\n" + + "// }\n" + + "// return ExecutedResult.failed(\"[" + tableDesc + "]设置排序值失败\");\n" + + "// }\n" + + "//\n" + "// public ExecutedResult<String> remove(Long id) {\n" + - "// Boolean result = this.dao.remove(id);\n" + + "// Boolean result = mapper.deleteLogic(id);\n" + "// if (BooleanUtils.isFalse(result)) {\n" + "// return ExecutedResult.failed(\"删除[" + tableDesc + "]失败。\");\n" + "// }\n" + @@ -1085,7 +1110,7 @@ "// }\n" + "//\n" + "// public ExecutedResult<String> removeList(List<Long> ids) {\n" + - "// Boolean result = this.dao.removeByListId(ids);\n" + + "// Boolean result = mapper.deleteLogic(ids);\n" + "// if (BooleanUtils.isFalse(result)) {\n" + "// return ExecutedResult.failed(\"删除[" + tableDesc + "]失败。\");\n" + "// }\n" + @@ -1095,7 +1120,7 @@ " public ExecutedResult<List<" + responseDTOName + ">> getList(List<Long> listId) {\n" + " List<" + responseDTOName + "> result = new ArrayList<>();\n" + "\n" + - " List<" + poName + "> list = this.dao.getListById(listId);\n" + + " List<" + poName + "> list = mapper.getList(listId);\n" + " if (ListUtil.isNotNullOrEmpty(list)) {\n" + " result = CopierUtil.mapTo(list, " + responseDTOName + ".class);\n" + " }\n" + @@ -1112,7 +1137,7 @@ " search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());\n" + " }\n" + "\n" + - " PagerResult<" + poName + "> pageList = dao.search(search);\n" + + " PagerResult<" + poName + "> pageList = mapper.search(search);\n" + " List<" + responseDTOName + "> listVo = new ArrayList<>();\n" + " List<" + poName + "> list = pageList.getList();\n" + " if (ListUtil.isNotNullOrEmpty(list)) {\n" + @@ -1126,12 +1151,30 @@ " }\n" + "\n" + " protected ExecutedResult<" + poName + "> check4Id(Long id) {\n" + - " " + poName + " exists = dao.getById(id);\n" + + " " + poName + " exists = mapper.get(id);\n" + " if (Objects.isNull(exists)) {\n" + " return ExecutedResult.failed(\"[" + tableDesc + "]不存在:\" + id);\n" + " }\n" + " return ExecutedResult.success(exists);\n" + - " }\n"); + " }\n"+ + " protected ExecutedResult<List<" + poName + ">> check4Id(List<Long> listId) {\n" + + " // 从数据库查找" + tableDesc + "\n" + + " List<" + poName + "> list = mapper.getList(listId);\n" + + " if (ListUtil.isNullOrEmpty(list)) {\n" + + " return ExecutedResult.failed(\"[" + tableDesc + "]不存在.\" + listId);\n" + + " }\n" + + " // 数据库找到的id列表\n" + + " List<Long> listIdFind = list.stream().map(" + poName + "::getId).collect(Collectors.toList());\n" + + " // 数量不一致\n" + + " if (listId.size() != listIdFind.size()) {\n" + + " // 筛选数据库不存在的" + tableDesc + "\n" + + " List<Long> listIdNotFound = listId.stream().filter(c -> !listIdFind.contains(c)).collect(Collectors.toList());\n" + + " if (ListUtil.isNullOrEmpty(list)) {\n" + + " return ExecutedResult.failed(\"[" + tableDesc + "]不存在.\" + listIdNotFound);\n" + + " }\n" + + " }\n" + + " return ExecutedResult.success(list);\n" + + " }"); content.append("}"); if (new File(GenCodeGauss.OutSet.SERVICE + serviceName + ".java").exists()) { @@ -1264,16 +1307,6 @@ .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); content.append(ConstantFactory.STR_NEWLINE); - content.append(" @PostMapping(value = \"getList\")\n" + - " public ExecutedResult<List<" + voName + ">> getList(@RequestBody ReqListId request) {\n" + - " return this.service.getList(request.getListId());\n" + - " }\n\n"); - content.append(SET_METHOD_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", "查询[" + tableDesc + "]") - .replaceAll("\\{\\#\\=author\\}", AUTHOR) - .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) - ); - content.append(ConstantFactory.STR_NEWLINE); content.append(" @PostMapping(value = \"search\")\n" + " public ExecutedResult<PagerResult<" + voName + ">> search(@RequestBody " + searchName + " request) {\n" + " return this.service.search(request);\n" + @@ -1312,6 +1345,7 @@ content.append(ConstantFactory.STR_SEMICOLON); content.append(ConstantFactory.STR_NEWLINE); content.append(ConstantFactory.STR_NEWLINE); + content.append(String.format("import %s.BasicMapper;", PackageSet.MAPPER.replace(".mapper", ""))); content.append(String.format("import %s.%s%s;", GenCodeGauss.PackageSet.PO, modelName, GenCodeGauss.SuffixSet.PO)); content.append(ConstantFactory.STR_NEWLINE); content.append(ConstantFactory.STR_NEWLINE); -- Gitblit v1.9.3