From ad56449d12346611f9cdf4e3c7d6f5112589bdd1 Mon Sep 17 00:00:00 2001 From: liulin <lin.liu@aliyun.com> Date: 星期一, 05 八月 2024 14:19:30 +0800 Subject: [PATCH] 修改生成代码逻辑 --- src/main/java/com/lunhan/xxx/repository/mapper/TestInfoMapper.java | 8 ++- src/main/java/com/lunhan/xxx/repository/impl/TestInfoMapperImpl.java | 11 +++++ src/test/java/com/lunhan/xxx/host/GenCodeGauss.java | 65 +++++++++++++++++++++----------- src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java | 7 +-- 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java b/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java index e779814..f8fc616 100644 --- a/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java +++ b/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java @@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -31,8 +30,8 @@ * * @param <T> 数据实体 */ -public abstract class BasicMapperImpl<T> implements BasicMapper<T> { - protected BaseMapper<T> DB; +public abstract class BasicMapperImpl<T, M extends BasicMapper<T>> implements BasicMapper<T> { + protected M DB; protected Class<T> clazz; protected String tableName; @@ -41,7 +40,7 @@ /** * 构造方法 */ - public BasicMapperImpl(BaseMapper<T> db) { + public BasicMapperImpl(M db) { this.clazz = (Class<T>) ( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0]; diff --git a/src/main/java/com/lunhan/xxx/repository/impl/TestInfoMapperImpl.java b/src/main/java/com/lunhan/xxx/repository/impl/TestInfoMapperImpl.java index 7b3a682..5421bf0 100644 --- a/src/main/java/com/lunhan/xxx/repository/impl/TestInfoMapperImpl.java +++ b/src/main/java/com/lunhan/xxx/repository/impl/TestInfoMapperImpl.java @@ -38,6 +38,8 @@ import com.lunhan.xxx.repository.po.TestInfoPO; import org.springframework.stereotype.Repository; +import java.util.List; + /** * 测试信息 数据访问类 * @author lin.liu @@ -45,7 +47,7 @@ * @date 2021/11/23 */ @Repository -public class TestInfoMapperImpl extends BasicMapperImpl<TestInfoPO> { +public class TestInfoMapperImpl extends BasicMapperImpl<TestInfoPO, TestInfoMapper> { public TestInfoMapperImpl(TestInfoMapper mapper) { super(mapper); } @@ -54,6 +56,13 @@ public PagerResult<TestInfoPO> search(SearchBasicDTO request) { // 还原查询条件真实类型 SearchTestInfo search = (SearchTestInfo)request; + + TestInfoPO model1 = DB.testGet(553261510198173696L); + TestInfoPO model2 = DB.testGet2("verona.stroman"); + + List<TestInfoPO> list1 = DB.testList1(search); + List<TestInfoPO> list2 = DB.testList2(search); + // 查询条件 LambdaQueryWrapper<TestInfoPO> queryWrapper = this.query(); // 非逻辑删除 diff --git a/src/main/java/com/lunhan/xxx/repository/mapper/TestInfoMapper.java b/src/main/java/com/lunhan/xxx/repository/mapper/TestInfoMapper.java index 583f123..ae75fc0 100644 --- a/src/main/java/com/lunhan/xxx/repository/mapper/TestInfoMapper.java +++ b/src/main/java/com/lunhan/xxx/repository/mapper/TestInfoMapper.java @@ -8,6 +8,8 @@ import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.util.List; + @Mapper public interface TestInfoMapper extends BasicMapper<TestInfoPO> { // #{param}:会进行预编译,而且进行类型匹配,最后进行变量替换,括号中可以添加映射类型如 @@ -21,13 +23,13 @@ @Select("select * from test_info where id = #{id}") TestInfoPO testGet(Long id); - @Select("select * from test_info where id = #{userName}") + @Select("select * from test_info where name = #{name}") TestInfoPO testGet2(String userName); @Select("select * from test_info where status = #{ew.status}") - TestInfoPO testGet3(@Param(Constants.WRAPPER) SearchTestInfo search); + List<TestInfoPO> testList1(@Param(Constants.WRAPPER) SearchTestInfo search); @Select("select * from test_info where status = #{search.status}") - TestInfoPO testGet4(@Param("search") SearchTestInfo search); + List<TestInfoPO> testList2(@Param("search") SearchTestInfo search); } diff --git a/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java b/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java index dc3f35a..173d8c0 100644 --- a/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java +++ b/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java @@ -23,7 +23,7 @@ private static final String DB_NAME = "water_basic"; private static final String AUTHOR = "lin.liu"; - private static Connection CONN = null; + public static Connection CONN = null; private static final String DRIVER = "org.postgresql.Driver"; private static final String CONNECTION_STR = "jdbc:postgresql://" + HOST + ":" + PORT + "/" + DB_NAME; @@ -243,7 +243,7 @@ * 只生成以下配置的表 */ private static final List<String> ONLY_TABLES = Arrays.asList( - "admin_menus" + ); public static void main(String[] args) { @@ -332,16 +332,18 @@ listColumn.add("(type = IdType.ASSIGN_ID)"); } } - String columnAnnotation = "\t@TableField(\"" + name + "\""; - //忽略最后修改时间这一列的新增和编辑 + if (!"id".equals(name)) { + String columnAnnotation = "\t@TableField(\"" + name + "\""; + //忽略最后修改时间这一列的新增和编辑 // if (UPDATE_TIME_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { // columnAnnotation += ", insertable = false, updatable = false"; // } + listColumn.add(columnAnnotation + ")"); + } if ("datachange_lasttime".equalsIgnoreCase(name)) { propName = "dataChangeLastTime"; columnNameUpper = "DataChangeLastTime"; } - listColumn.add(columnAnnotation + ")"); //追加列 listColumn.add(String.format("\tprivate %s %s;", javaType, propName)); @@ -359,7 +361,7 @@ } content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -422,7 +424,7 @@ content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -487,7 +489,7 @@ } content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -572,7 +574,7 @@ content.append(poName + ";\n"); content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -582,7 +584,7 @@ content.append(daoName); content.append(" extends BasicMapperImpl<"); content.append(modelName + GenCodeGauss.SuffixSet.PO); - content.append("> {"); + content.append(", " + mapperName + "> {"); content.append(ConstantFactory.STR_NEWLINE); content.append("\t" + daoName + "(" + mapperName + " mapper) {\n"); content.append("\t\tsuper(mapper);\n"); @@ -638,15 +640,15 @@ "\t\t\t\t\tswitch (orderBy) {\n" + "\t\t\t\t\t\t// 主键\n" + "\t\t\t\t\t\tcase ID:\n" + - "\t\t\t\t\t\t\tqueryWrapper.orderByAsc(" + poName + "::getId);\n" + + "\t\t\t\t\t\t\tqueryWrapper.orderByDesc(" + poName + "::getId);\n" + "\t\t\t\t\t\t\tbreak;\n" + "\t\t\t\t\t\t// 数据创建时间\n" + "\t\t\t\t\t\tcase CREATE_TIME:\n" + - "\t\t\t\t\t\t\tqueryWrapper.orderByAsc(" + poName + "::getCreateTime);\n" + + "\t\t\t\t\t\t\tqueryWrapper.orderByDesc(" + poName + "::getCreateTime);\n" + "\t\t\t\t\t\t\tbreak;\n" + "\t\t\t\t\t\t// 最后更新时间\n" + "\t\t\t\t\t\tcase UPDATE_TIME:\n" + - "\t\t\t\t\t\t\tqueryWrapper.orderByAsc(" + poName + "::getUpdateTime);\n" + + "\t\t\t\t\t\t\tqueryWrapper.orderByDesc(" + poName + "::getUpdateTime);\n" + "\t\t\t\t\t\t\tbreak;\n" + "\t\t\t\t\t}\n" + "\t\t\t\t}\n" + @@ -654,9 +656,28 @@ "\t\t} else {\n" + "\t\t\tqueryWrapper.orderByDesc(" + poName + "::getId);\n" + "\t\t}\n" + - "\t\tPage<" + poName + "> pageResult = DB.selectPage(new Page<>(search.getPage(), search.getLimit()), queryWrapper);\n" + + "\t\tPage<" + poName + "> pageResult = super.selectPage(new Page<>(search.getPage(), search.getLimit()), queryWrapper);\n" + "\t\treturn new PagerResult<>(pageResult.getSize(), pageResult.getPages(), pageResult.getTotal(), pageResult.getRecords());\n" + - "\t}"); + "\t}\n" + + "\n" + + "\tpublic Boolean add(" + poName + " item) {\n" + + "\t\tint rowCount = super.insert(item);\n" + + "\t\treturn rowCount == 1;\n" + + "\t}\n" + + "\n" + + "\tpublic Boolean addNotIncrement(" + poName + " item) {\n" + + "\t\tint rowCount = super.insert(item);\n" + + "\t\treturn rowCount == 1;\n" + + "\t}\n" + + "\n" + + "\tpublic " + poName + " getById(Long id) {\n" + + "\t\treturn super.get(id);\n" + + "\t}\n" + + "\n" + + "\tpublic List<" + poName + "> getListById(List<Long> listId) {\n" + + "\t\treturn super.getList(listId);\n" + + "\t}\n" + ); content.append("}"); if (new File(GenCodeGauss.OutSet.MAPPER_IMPL + daoName + ".java").exists()) { @@ -745,7 +766,7 @@ } content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -838,7 +859,7 @@ } content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -912,7 +933,7 @@ content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) ); content.append(ConstantFactory.STR_NEWLINE); content.append("@Mapper"); @@ -987,7 +1008,7 @@ content.append(responseDTOName + ";\n"); content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -1007,7 +1028,7 @@ " // 设置记录创建时间\n" + " item.setCreateTime(LocalDateTimeUtil.nowTimeStamp());\n" + " // 是否删除(逻辑删除)初始值\n" + - " //item.setIsDelete(EYesOrNo.NO.getValue());\n" + + " item.setIsDelete(EYesOrNo.NO.getValue());\n" + "\n" + " int rowCount = mapper.insert(item);\n" + " if (rowCount != 1) {\n" + @@ -1247,7 +1268,7 @@ content.append(responseDTOName + ";\n"); content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc) + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc) .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); @@ -1366,7 +1387,7 @@ content.append(ConstantFactory.STR_NEWLINE); content.append(ConstantFactory.STR_NEWLINE); content.append(SET_CLASS_DESC_TPL - .replaceAll("\\{\\#\\=desc\\}", tableDesc + " mapper") + .replaceAll("\\{\\#\\=desc\\}", tableDesc == null ? modelName : tableDesc + " mapper") .replaceAll("\\{\\#\\=author\\}", AUTHOR) .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) ); -- Gitblit v1.9.3