通过EasyCode生成漂亮的DynamicQuery代码, 从此让你告别繁琐的cv大法。

根据国际惯例,我们先来欣赏下自动生成的DynamicQuery代码有多美

Do

/**
 * 用户信息表实体对象
 *
 * @author wangjin
 */
@Table(name = "userinfo")
public class UserinfoDO {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Integer id;
    /**
     * 名称
     */
    @Column
    private String name;
    /**
     * 手机号码
     */
    @Column
    private String mobile;
    /**
     * 身份证
     */
    @Column
    private String idCard;
    /**
     * 1男 2女
     */
    @Column
    private Integer sex;

    ... set get ....
}


Mapper

/**
 * 用户信息表DynamicQueryMapper层 {@link UserinfoDO}
 *
 * @author wangjin
 */
public interface UserinfoMapper extends DynamicQueryMapper<UserinfoDO> {

}


DAO

/**
 * 用户信息表数据库访问层 {@link UserinfoDO}
 * 对UserinfoMapper层做出简单封装 {@link UserinfoMapper}
 *
 * @author wangjin
 */
@Repository
public class UserinfoDAO {

    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 根据ID查询用户信息
     *
     * @param id {@link UserinfoDO#getId()}
     * @return Optional<UserinfoDO>用户信息 {@link UserinfoDO}
     */
    public Optional<UserinfoDO> getUserinfoById(Long id) {
        return userinfoMapper.selectFirstByDynamicQuery(DynamicQuery.createQuery(UserinfoDO.class)
                .and(UserinfoDO::getId, isEqual(id)));
    }

    /**
     * 批量新增用户信息 {@link UserinfoMapper#insertSelective(Object)}
     *
     * @param userinfos {@link UserinfoDO}
     * @return 影响的行数
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)
    public int insertBatchUserinfos(final List<UserinfoDO> userinfos) {
        if (CollectionUtils.isEmpty(userinfos)) {
            return 0;
        }
        final MapperBatchAction<UserinfoMapper> updateBatchAction = MapperBatchAction
                .create(UserinfoMapper.class, sqlSessionFactory);
        for (UserinfoDO userinfo : userinfos) {
            updateBatchAction.addAction(mapper -> mapper.insertSelective(userinfo));
        }
        return updateBatchAction.doBatchActions();
    }

    /**
     * 批量更新用户信息 {@link UserinfoMapper#updateByPrimaryKeySelective(Object)}
     *
     * @param userinfos {@link UserinfoDO}
     * @return 影响的行数
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)
    public int updateBatchUserinfosByPrimaryKey(final List<UserinfoDO> userinfos) {
        if (CollectionUtils.isEmpty(userinfos)) {
            return 0;
        }
        final MapperBatchAction<UserinfoMapper> updateBatchAction = MapperBatchAction
                .create(UserinfoMapper.class, sqlSessionFactory);
        for (UserinfoDO userinfo : userinfos) {
            updateBatchAction.addAction(mapper -> mapper.updateByPrimaryKeySelective(userinfo));
        }
        return updateBatchAction.doBatchActions();
    }

    /**
     * 新增用户信息 {@link UserinfoMapper#insertSelective(Object)}
     *
     * @param userinfo {@link UserinfoDO}
     * @return 影响的行数
     */
    public int insertUserinfo(UserinfoDO userinfo) {
        return userinfoMapper.insertSelective(userinfo);
    }

    /**
     * 更新用户信息 {@link UserinfoMapper#updateByPrimaryKeySelective(Object)}
     *
     * @param userinfo {@link UserinfoDO}
     * @return 影响的行数
     */
    public int updateUserinfoById(UserinfoDO userinfo) {
        return userinfoMapper.updateByPrimaryKeySelective(userinfo);
    }

    /**
     * 根据ID删除用户信息
     *
     * @param id {@link UserinfoDO#getId()}
     * @return 影响的行数
     */
    public int deleteUserinfoById(Long id) {
        return userinfoMapper.deleteByDynamicQuery(DynamicQuery.createQuery(UserinfoDO.class)
                .and(UserinfoDO::getId, isEqual(id)));
    }
}

DTO

/**
 * 用户信息表DTO对象 {@link UserinfoDO}
 *
 * @author wangjin
 */
public class UserinfoDTO {
    @ApiModelProperty("")
    private Integer id;
    @ApiModelProperty("名称")
    private String name;
    @ApiModelProperty("手机号码")
    private String mobile;
    @ApiModelProperty("身份证")
    private String idCard;
    @ApiModelProperty("1男 2女")
    private Integer sex;
    ... set get ....
}

RequestDTO

/**
 * 用户信息表Request DTO对象 {@link UserinfoDO}
 *
 * @author wangjin
 */
public class UserinfoRequestDTO {
    @ApiModelProperty("")
    private Integer id;
    @ApiModelProperty("名称")
    private String name;
    @ApiModelProperty("手机号码")
    private String mobile;
    @ApiModelProperty("身份证")
    private String idCard;
    @ApiModelProperty("1男 2女")
    private Integer sex;
    ... set get ....
}


ResponseDTO

/**
 * 用户信息表Response DTO对象 {@link UserinfoDO}
 *
 * @author wangjin
 */
public class UserinfoResponseDTO {
    @ApiModelProperty("")
    private Integer id;
    @ApiModelProperty("名称")
    private String name;
    @ApiModelProperty("手机号码")
    private String mobile;
    @ApiModelProperty("身份证")
    private String idCard;
    @ApiModelProperty("1男 2女")
    private Integer sex;
    ... set get ....
}



Service

/**
 * 用户信息表Service层 {@link UserinfoDO}
 *
 * @author wangjin
 */
@Service
public class UserinfoServiceImpl implements UserinfoService {

    private final UserinfoDAO userinfoDAO;

    /**
     * construction method
     *
     * @param userinfoDAO userinfoDAO {@link UserinfoDAO}
     */
    public UserinfoService(UserinfoDAO userinfoDAO) {
        this.userinfoDAO = userinfoDAO;
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id {@link UserinfoDO#getId()}
     * @return Optional<UserinfoDO>用户信息 {@link UserinfoDO}
     */
    @Override
    public Optional<UserinfoDO> getUserinfoById(Long id) {
        return userinfoDAO.getUserinfoById(id);
    }

    /**
     * 新增用户信息
     *
     * @param userinfoRequestDTO {@link UserinfoRequestDTO}
     */
    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)
    public void insertUserinfo(UserinfoRequestDTO userinfoRequestDTO) {
        UserinfoDO userinfoDO = BeanCopyUtils.copyProperties(userinfoRequestDTO,
                UserinfoDO.class);
        userinfoDAO.insertUserinfo(userinfoDO);
    }

    /**
     * 更新用户信息
     *
     * @param userinfoRequestDTO {@link UserinfoRequestDTO}
     */
    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)
    public void updateUserinfo(UserinfoRequestDTO userinfoRequestDTO) {
        UserinfoDO userinfoDO = BeanCopyUtils.copyProperties(userinfoRequestDTO,
                UserinfoDO.class);
        userinfoDAO.updateUserinfoById(userinfoDO);
    }
}

ServiceImpl

/**
 * 用户信息表Service层 {@link UserinfoDO}
 *
 * @author wangjin
 */
@Service
public class UserinfoServiceImpl implements UserinfoService {

    private final UserinfoDAO userinfoDAO;

    /**
     * construction method
     *
     * @param userinfoDAO userinfoDAO {@link UserinfoDAO}
     */
    public UserinfoService(UserinfoDAO userinfoDAO) {
        this.userinfoDAO = userinfoDAO;
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id {@link UserinfoDO#getId()}
     * @return Optional<UserinfoDO>用户信息 {@link UserinfoDO}
     */
    @Override
    public Optional<UserinfoDO> getUserinfoById(Long id) {
        return userinfoDAO.getUserinfoById(id);
    }

    /**
     * 新增用户信息
     *
     * @param userinfoRequestDTO {@link UserinfoRequestDTO}
     */
    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)
    public void insertUserinfo(UserinfoRequestDTO userinfoRequestDTO) {
        UserinfoDO userinfoDO = BeanCopyUtils.copyProperties(userinfoRequestDTO,
                UserinfoDO.class);
        userinfoDAO.insertUserinfo(userinfoDO);
    }

    /**
     * 更新用户信息
     *
     * @param userinfoRequestDTO {@link UserinfoRequestDTO}
     */
    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)
    public void updateUserinfo(UserinfoRequestDTO userinfoRequestDTO) {
        UserinfoDO userinfoDO = BeanCopyUtils.copyProperties(userinfoRequestDTO,
                UserinfoDO.class);
        userinfoDAO.updateUserinfoById(userinfoDO);
    }
}


安装教程

首先在idea插件商店中下载 Easy Code 并安装.
image-1654312325900

打开easy code 配置窗口
复制以下json文本内容. 从剪贴板导入即可使用

{"author":"wangjin","version":"1.2.4","userSecure":"","currTypeMapperGroupName":"Default","currTemplateGroupName":"Dynamic-Query","currColumnConfigGroupName":"Default","currGlobalConfigGroupName":"Default","typeMapper":{"Default":{"name":"Default","elementList":[{"matchType":"REGEX","columnType":"varchar(\\(\\d+\\))?","javaType":"java.lang.String"},{"matchType":"REGEX","columnType":"char(\\(\\d+\\))?","javaType":"java.lang.String"},{"matchType":"REGEX","columnType":"(tiny|medium|long)*text","javaType":"java.lang.String"},{"matchType":"REGEX","columnType":"decimal(\\(\\d+,\\d+\\))?","javaType":"java.math.BigDecimal"},{"matchType":"ORDINARY","columnType":"integer","javaType":"java.lang.Integer"},{"matchType":"REGEX","columnType":"(tiny|small|medium)*int(\\(\\d+\\))?","javaType":"java.lang.Integer"},{"matchType":"ORDINARY","columnType":"int4","javaType":"java.lang.Integer"},{"matchType":"ORDINARY","columnType":"int8","javaType":"java.lang.Long"},{"matchType":"REGEX","columnType":"bigint(\\(\\d+\\))?","javaType":"java.lang.Long"},{"matchType":"ORDINARY","columnType":"timestamp","javaType":"java.sql.Timestamp"},{"matchType":"ORDINARY","columnType":"time","javaType":"java.time.LocalTime"},{"matchType":"ORDINARY","columnType":"boolean","javaType":"java.lang.Boolean"},{"matchType":"REGEX","columnType":"bigint(\\d+).*","javaType":"java.lang.Long"},{"matchType":"REGEX","columnType":"int(\\d+).*","javaType":"java.lang.Integer"},{"matchType":"REGEX","columnType":"tinyint(\\d+).*","javaType":"java.lang.Integer"},{"matchType":"ORDINARY","columnType":"date","javaType":"java.sql.Date"},{"matchType":"ORDINARY","columnType":"datetime","javaType":"java.sql.Date"}]}},"template":{"Dynamic-Query":{"name":"Dynamic-Query","elementList":[{"name":"DO.java.vm","code":"##引入宏定义\n$!{define.vm}\n##使用宏定义设置回调(保存位置与文件后缀)\n#save(\"/model/entity\", \"DO.java\")\n##使用宏定义设置包后缀\n#setPackageSuffix(\"model.entity\")\n#set($ClassName = $tool.append($tableInfo.name, \"DO\"))\n$!callback.setReformat(true)\n##使用全局变量实现默认包导入\n$!{autoImport.vm}\nimport javax.persistence.*;\n##使用宏定义实现类注释信息\n/**\n * $!{tableInfo.comment}表实体对象\n *\n * @author $!author\n */\n@Table(name=\"$tool.hump2Underline($tableInfo.name)\")\npublic class $!{ClassName} {\n    ## 获取主键(一般主键都是实体类名+Id)\n#set($id= \"$tool.firstLowerCase($!{tableInfo.name})Id\")\n#foreach($column in $tableInfo.fullColumn)\n    #if(${column.comment})/**\n     * ${column.comment}\n     */#end\n\n    #if(${column.name} == \"id\")\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY )\n    @Column\n    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n    #else\n    @Column\n    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n    #end\n#end\n#foreach($column in $tableInfo.fullColumn)\n##使用宏定义实现get,set方法\n#getSetMethod($column)\n#end\n\n}\n"},{"name":"DTO.java.vm","code":"##引入宏定义\n$!{define.vm}\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n#set($ClassName = $tool.append($tableInfo.name, \"DTO\"))\n##使用宏定义设置回调(保存位置与文件后缀)\n#save(\"/model/dto\", \"DTO.java\")\n##使用宏定义设置包后缀\n#setPackageSuffix(\"model.dto\")\n$!callback.setReformat(true)\n\n##使用全局变量实现默认包导入\n$!{autoImport.vm}\nimport io.swagger.annotations.ApiModelProperty;\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\n##使用宏定义实现类注释信息\n/**\n * $!{tableInfo.comment}表DTO对象 {@link $!{EntityDO}}\n *\n * @author $!author\n */\npublic class $!{ClassName} {\n    ## 获取主键(一般主键都是实体类名+Id)\n#set($id= \"$tool.firstLowerCase($!{tableInfo.name})Id\")\n#foreach($column in $tableInfo.fullColumn)\n\n\n    @ApiModelProperty(\"#if(${column.comment})${column.comment}#end\")\n    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n#end\n#foreach($column in $tableInfo.fullColumn)\n##使用宏定义实现get,set方法\n#getSetMethod($column)\n#end\n\n}"},{"name":"RequestDTO.java.vm","code":"##引入宏定义\n$!{define.vm}\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n#set($ClassName = $tool.append($tableInfo.name, \"RequestDTO\"))\n##使用宏定义设置回调(保存位置与文件后缀)\n#save(\"/model/dto/request\", \"RequestDTO.java\")\n##使用宏定义设置包后缀\n#setPackageSuffix(\"model.dto.request\")\n$!callback.setReformat(true)\n\n##使用全局变量实现默认包导入\n$!{autoImport.vm}\nimport io.swagger.annotations.ApiModelProperty;\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\n##使用宏定义实现类注释信息\n/**\n * $!{tableInfo.comment}表Request DTO对象 {@link $!{EntityDO}}\n *\n * @author $!author\n */\npublic class $!{ClassName} {\n    ## 获取主键(一般主键都是实体类名+Id)\n#set($id= \"$tool.firstLowerCase($!{tableInfo.name})Id\")\n#foreach($column in $tableInfo.fullColumn)\n    @ApiModelProperty(\"#if(${column.comment})${column.comment}#end\")\n    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n#end\n#foreach($column in $tableInfo.fullColumn)\n##使用宏定义实现get,set方法\n#getSetMethod($column)\n#end\n\n}\n"},{"name":"ResponseDTO.java.vm","code":"##引入宏定义\n$!{define.vm}\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n#set($ClassName = $tool.append($tableInfo.name, \"ResponseDTO\"))\n##使用宏定义设置回调(保存位置与文件后缀)\n#save(\"/model/dto/response\", \"ResponseDTO.java\")\n##使用宏定义设置包后缀\n#setPackageSuffix(\"model.dto.response\")\n$!callback.setReformat(true)\n\n##使用全局变量实现默认包导入\n$!{autoImport.vm}\nimport io.swagger.annotations.ApiModelProperty;\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\n##使用宏定义实现类注释信息\n/**\n * $!{tableInfo.comment}表Response DTO对象 {@link $!{EntityDO}}\n *\n * @author $!author\n */\npublic class $!{ClassName} {\n    ## 获取主键(一般主键都是实体类名+Id)\n#set($id= \"$tool.firstLowerCase($!{tableInfo.name})Id\")\n#foreach($column in $tableInfo.fullColumn)\n    @ApiModelProperty(\"#if(${column.comment})${column.comment}#end\")\n    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n#end\n#foreach($column in $tableInfo.fullColumn)\n##使用宏定义实现get,set方法\n#getSetMethod($column)\n#end\n\n}\n"},{"name":"Mapper.java.vm","code":"##定义初始变量\n#set($ClassName = $tool.append($tableInfo.name, \"Mapper\"))\n#set($Comment = $!{tableInfo.comment})\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n##设置回调\n$!callback.setFileName($tool.append($ClassName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/mapper\"))\n$!callback.setReformat(true)\n\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n    #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}mapper;\n\nimport com.github.wz2cool.dynamic.mybatis.mapper.DynamicQueryMapper;\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\n\n/**\n * $!{Comment}表DynamicQueryMapper层 {@link $!{EntityDO}}\n *\n * @author $!author\n */\npublic interface $!{ClassName} extends DynamicQueryMapper<$!{EntityDO}> {\n\n}\n"},{"name":"DAO.java.vm","code":"##定义初始变量\n#set($ClassName = $tool.append($tableInfo.name, \"DAO\"))\n#set($EntityMapper = $tool.append($tableInfo.name, \"Mapper\"))\n#set($entityMapper = $tool.firstLowerCase($tool.append($tableInfo.name, \"Mapper\")))\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n#set($Entity = $tableInfo.name)\n#set($Entitys = $tool.append($Entity, \"s\"))\n#set($entity = $tool.firstLowerCase($tableInfo.name))\n#set($entitys = $tool.append($entity, \"s\"))\n#set($Comment = $!{tableInfo.comment})\n##设置回调\n$!callback.setFileName($tool.append($ClassName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/dao\"))\n$!callback.setReformat(true)\n\n#if(!$tableInfo.pkColumn.isEmpty())\n    #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;\n\nimport com.github.wz2cool.dynamic.DynamicQuery;\nimport com.github.wz2cool.dynamic.mybatis.mapper.batch.MapperBatchAction;\nimport org.springframework.stereotype.Repository;\nimport $!{tableInfo.savePackageName}.mapper.$!{EntityMapper};\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\nimport org.apache.commons.collections4.CollectionUtils;\nimport org.apache.ibatis.session.SqlSessionFactory;\nimport org.springframework.transaction.annotation.Transactional;\nimport javax.annotation.Resource;\nimport java.util.List;\nimport java.util.Optional;\nimport static com.github.wz2cool.dynamic.builder.DynamicQueryBuilderHelper.isEqual;\n\n\n /**\n * $!{Comment}表数据库访问层 {@link $!{EntityDO}}\n * 对$!{EntityMapper}层做出简单封装 {@link $!{EntityMapper}}\n *\n * @author $!author\n */\n@Repository\npublic class $!{ClassName}  {\n\n    @Resource\n    private $!{EntityMapper} $!{entityMapper};\n    @Resource\n    private SqlSessionFactory sqlSessionFactory;\n    \n    /**\n     * 根据ID查询$!{Comment}\n     *\n     * @param id {@link $!{EntityDO}#getId()}\n     * @return Optional<$!{EntityDO}>$!{Comment} {@link $!{EntityDO}}\n     */\n    public Optional<$!{EntityDO}> get$!{tableInfo.name}ById(Long id) {\n        return $!{entityMapper}.selectFirstByDynamicQuery(DynamicQuery.createQuery($!{EntityDO}.class)\n                .and($!{EntityDO}::getId, isEqual(id)));\n    }\n        \n    /**\n     * 批量新增$!{Comment} {@link $!{EntityMapper}#insertSelective(Object)}\n     *\n     * @param $!{entitys} {@link $!{EntityDO}}\n     * @return 影响的行数\n     */\n    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)\n    public int insertBatch$!{Entitys}(final List<$!{EntityDO}> $!{entitys}) {\n        if (CollectionUtils.isEmpty($!{entitys})) {\n            return 0;\n        }\n        final MapperBatchAction<$!{tableInfo.name}Mapper> updateBatchAction = MapperBatchAction\n                .create($!{tableInfo.name}Mapper.class, sqlSessionFactory);\n        for ($!{EntityDO} $!{entity} : $!{entitys}) {\n            updateBatchAction.addAction(mapper -> mapper.insertSelective($!{entity}));\n        }\n        return updateBatchAction.doBatchActions();\n    }\n\n    /**\n     * 批量更新$!{Comment} {@link $!{EntityMapper}#updateByPrimaryKeySelective(Object)}\n     *\n     * @param $!{entitys} {@link $!{EntityDO}}\n     * @return 影响的行数\n     */\n    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)\n    public int updateBatch$!{Entitys}ByPrimaryKey(final List<$!{EntityDO}> $!{entitys}) {\n        if (CollectionUtils.isEmpty($!{entitys})) {\n            return 0;\n        }\n        final MapperBatchAction<$!{tableInfo.name}Mapper> updateBatchAction = MapperBatchAction\n                .create($!{tableInfo.name}Mapper.class, sqlSessionFactory);\n        for ($!{EntityDO} $!{entity} : $!{entitys}) {\n            updateBatchAction.addAction(mapper -> mapper.updateByPrimaryKeySelective($!{entity}));\n        }\n        return updateBatchAction.doBatchActions();\n    }\n\n    /**\n     * 新增$!{Comment} {@link $!{EntityMapper}#insertSelective(Object)}\n     *\n     * @param $!{entity} {@link $!{EntityDO}}\n     * @return 影响的行数\n     */\n    public int insert$!{Entity}($!{EntityDO} $!{entity}) {\n        return $!{entityMapper}.insertSelective($!{entity});\n    }\n\n    /**\n     * 更新$!{Comment} {@link $!{EntityMapper}#updateByPrimaryKeySelective(Object)}\n     *\n     * @param $!{entity} {@link $!{EntityDO}}\n     * @return 影响的行数\n     */\n    public int update$!{Entity}ById($!{EntityDO} $!{entity}) {\n        return $!{entityMapper}.updateByPrimaryKeySelective($!{entity});\n    }\n\n    /**\n     * 根据ID删除$!{Comment}\n     *\n     * @param id {@link $!{EntityDO}#getId()}\n     * @return 影响的行数\n     */\n    public int delete$!{Entity}ById(Long id) {\n        return $!{entityMapper}.deleteByDynamicQuery(DynamicQuery.createQuery($!{EntityDO}.class)\n                .and($!{EntityDO}::getId, isEqual(id)));\n    }\n}"},{"name":"Service.java.vm","code":"##定义初始变量\n#set($ClassName = $tool.append($tableInfo.name, \"Service\"))\n#set($EntityMapper = $tool.append($tableInfo.name, \"Mapper\"))\n#set($entityMapper = $tool.firstLowerCase($tool.append($tableInfo.name, \"Mapper\")))\n#set($EntityDAO = $tool.append($tableInfo.name, \"DAO\"))\n#set($entityDAO = $tool.firstLowerCase($tool.append($tableInfo.name, \"DAO\")))\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n#set($entityDO = $tool.firstLowerCase($EntityDO))\n#set($EntityRequestDTO = $tool.append($tableInfo.name, \"RequestDTO\"))\n#set($entityRequestDTO = $tool.firstLowerCase($EntityRequestDTO))\n#set($Entity = $tableInfo.name)\n#set($Entitys = $tool.append($Entity, \"s\"))\n#set($entity = $tool.firstLowerCase($tableInfo.name))\n#set($entitys = $tool.append($entity, \"s\"))\n#set($Comment = $!{tableInfo.comment})\n##设置回调\n$!callback.setFileName($tool.append($ClassName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/service\"))\n$!callback.setReformat(true)\n\n#if(!$tableInfo.pkColumn.isEmpty())\n    #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;\n\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\nimport $!{tableInfo.savePackageName}.model.dto.request.$!{EntityRequestDTO};\nimport java.util.Optional;\n\n\n /**\n * $!{Comment}表Service层 {@link $!{EntityDO}}\n *\n * @author $!author\n */\npublic interface $!{ClassName}  {\n\n    /**\n     * 根据ID查询$!{Comment}\n     *\n     * @param id {@link $!{EntityDO}#getId()}\n     * @return Optional<$!{EntityDO}>$!{Comment} {@link $!{EntityDO}}\n     */\n    public Optional<$!{EntityDO}> get$!{tableInfo.name}ById(Long id);\n\n    /**\n     * 新增$!{Comment}\n     *\n     * @param $!{entityRequestDTO} {@link $!{EntityRequestDTO}}\n     */\n    public void insert$!{Entity}($!{EntityRequestDTO} $!{entityRequestDTO});\n\n    /**\n     * 更新$!{Comment}\n     *\n     * @param $!{entityRequestDTO} {@link $!{EntityRequestDTO}}\n     */\n    public void update$!{Entity}($!{EntityRequestDTO} $!{entityRequestDTO});\n}"},{"name":"ServiceImpl.java.vm","code":"##定义初始变量\n#set($ClassName = $tool.append($tableInfo.name, \"Service\"))\n#set($EntityMapper = $tool.append($tableInfo.name, \"Mapper\"))\n#set($entityMapper = $tool.firstLowerCase($tool.append($tableInfo.name, \"Mapper\")))\n#set($EntityDAO = $tool.append($tableInfo.name, \"DAO\"))\n#set($entityDAO = $tool.firstLowerCase($tool.append($tableInfo.name, \"DAO\")))\n#set($EntityDO = $tool.append($tableInfo.name, \"DO\"))\n#set($entityDO = $tool.firstLowerCase($EntityDO))\n#set($EntityRequestDTO = $tool.append($tableInfo.name, \"RequestDTO\"))\n#set($entityRequestDTO = $tool.firstLowerCase($EntityRequestDTO))\n#set($Entity = $tableInfo.name)\n#set($Entitys = $tool.append($Entity, \"s\"))\n#set($entity = $tool.firstLowerCase($tableInfo.name))\n#set($entitys = $tool.append($entity, \"s\"))\n#set($Comment = $!{tableInfo.comment})\n##设置回调\n$!callback.setFileName($tool.append($ClassName, \"Impl.java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/service/impl\"))\n$!callback.setReformat(true)\n\n#if(!$tableInfo.pkColumn.isEmpty())\n    #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;\n\nimport com.innodealing.commons.object.BeanCopyUtils;\nimport $!{tableInfo.savePackageName}.dao.$!{EntityDAO};\nimport $!{tableInfo.savePackageName}.model.entity.$!{EntityDO};\nimport $!{tableInfo.savePackageName}.model.dto.request.$!{EntityRequestDTO};\nimport org.springframework.transaction.annotation.Transactional;\nimport $!{tableInfo.savePackageName}.service.$!{ClassName};\nimport org.springframework.stereotype.Service;\nimport java.util.Optional;\n\n\n /**\n * $!{Comment}表Service层 {@link $!{EntityDO}}\n *\n * @author $!author\n */\n@Service\npublic class $!{ClassName}Impl implements $!{ClassName} {\n\n    private final $!{EntityDAO} $!{entityDAO};\n\n    /**\n     * construction method\n     *\n     * @param $!{entityDAO} $!{entityDAO} {@link $!{EntityDAO}}\n     */\n    public $!{ClassName}($!{EntityDAO} $!{entityDAO}){\n        this.$!{entityDAO} = $!{entityDAO};\n    }\n    \n    /**\n     * 根据ID查询$!{Comment}\n     *\n     * @param id {@link $!{EntityDO}#getId()}\n     * @return Optional<$!{EntityDO}>$!{Comment} {@link $!{EntityDO}}\n     */\n    @Override\n    public Optional<$!{EntityDO}> get$!{tableInfo.name}ById(Long id) {\n        return $!{entityDAO}.get$!{Entity}ById(id);\n    }\n\n    /**\n     * 新增$!{Comment}\n     *\n     * @param $!{entityRequestDTO} {@link $!{EntityRequestDTO}}\n     */\n    @Override\n    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)\n    public void insert$!{Entity}($!{EntityRequestDTO} $!{entityRequestDTO}) {\n        $!{EntityDO} $!{entityDO} = BeanCopyUtils.copyProperties($!{entityRequestDTO},\n                $!{EntityDO}.class);\n        $!{entityDAO}.insert$!{Entity}($!{entityDO});\n    }\n\n    /**\n     * 更新$!{Comment}\n     *\n     * @param $!{entityRequestDTO} {@link $!{EntityRequestDTO}}\n     */\n    @Override\n    @Transactional(rollbackFor = Exception.class, transactionManager = TODO)\n    public void update$!{Entity}($!{EntityRequestDTO} $!{entityRequestDTO}) {\n        $!{EntityDO} $!{entityDO} = BeanCopyUtils.copyProperties($!{entityRequestDTO},\n                $!{EntityDO}.class);\n        $!{entityDAO}.update$!{Entity}ById($!{entityDO});\n    }\n}"}]}},"columnConfig":{"Default":{"name":"Default","elementList":[{"title":"disable","type":"BOOLEAN","selectValue":""},{"title":"support","type":"SELECT","selectValue":"add,edit,query,del,ui"}]}},"globalConfig":{"Default":{"name":"Default","elementList":[{"name":"autoImport.vm","value":"##自动导入包(仅导入实体属性需要的包,通常用于实体类)\n#foreach($import in $importList)\nimport $!import;\n#end"},{"name":"define.vm","value":"##(Velocity宏定义)\n\n##定义设置表名后缀的宏定义,调用方式:#setTableSuffix(\"Test\")\n#macro(setTableSuffix $suffix)\n    #set($tableName = $!tool.append($tableInfo.name, $suffix))\n#end\n\n##定义设置包名后缀的宏定义,调用方式:#setPackageSuffix(\"Test\")\n#macro(setPackageSuffix $suffix)\n#if($suffix!=\"\")package #end#if($tableInfo.savePackageName!=\"\")$!{tableInfo.savePackageName}.#{end}$!suffix;\n#end\n\n##定义直接保存路径与文件名简化的宏定义,调用方式:#save(\"/entity\", \".java\")\n#macro(save $path $fileName)\n    $!callback.setSavePath($tool.append($tableInfo.savePath, $path))\n    $!callback.setFileName($tool.append($tableInfo.name, $fileName))\n#end\n\n##定义表注释的宏定义,调用方式:#tableComment(\"注释信息\")\n#macro(tableComment $desc)\n/**\n * $!{tableInfo.comment}($!{tableInfo.name})$desc\n *\n * @author $!author\n * @since $!time.currTime()\n */\n#end\n\n##定义GET,SET方法的宏定义,调用方式:#getSetMethod($column)\n#macro(getSetMethod $column)\n\n  \n    public $!{tool.getClsNameByFullName($column.type)} get$!{tool.firstUpperCase($column.name)}() {\n        #if($!{tool.getClsNameByFullName($column.type)} == \"Timestamp\")\n        return java.util.Objects.isNull($!{column.name}) ? null : new $!{tool.getClsNameByFullName($column.type)}($!{column.name}.getTime());\n        #else\n        return $!{column.name};\n        #end\n    }\n\n    public void set$!{tool.firstUpperCase($column.name)}($!{tool.getClsNameByFullName($column.type)} $!{column.name}) {\n        #if($!{tool.getClsNameByFullName($column.type)} == \"Timestamp\")\n        this.$!{column.name} = java.util.Objects.isNull($!{column.name}) ? null : new $!{tool.getClsNameByFullName($column.type)}($!{column.name}.getTime());\n        #else\n        this.$!{column.name} = $!{column.name};\n        #end\n    }\n#end"},{"name":"init.vm","value":"##初始化区域\n\n##去掉表的t_前缀\n$!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst(\"book_\",\"\")))\n\n##参考阿里巴巴开发手册,POJO 类中布尔类型的变量,都不要加 is 前缀,否则部分框架解析会引起序列化错误\n#foreach($column in $tableInfo.fullColumn)\n#if($column.name.startsWith(\"is\") && $column.type.equals(\"java.lang.Boolean\"))\n    $!column.setName($tool.firstLowerCase($column.name.substring(2)))\n#end\n#end\n\n##实现动态排除列\n#set($temp = $tool.newHashSet(\"testCreateTime\", \"otherColumn\"))\n#foreach($item in $temp)\n    #set($newList = $tool.newArrayList())\n    #foreach($column in $tableInfo.fullColumn)\n        #if($column.name!=$item)\n            ##带有反回值的方法调用时使用$tool.call来消除返回值\n            $tool.call($newList.add($column))\n        #end\n    #end\n    ##重新保存\n    $tableInfo.setFullColumn($newList)\n#end\n\n##对importList进行篡改\n#set($temp = $tool.newHashSet())\n#foreach($column in $tableInfo.fullColumn)\n    #if(!$column.type.startsWith(\"java.lang.\"))\n        ##带有反回值的方法调用时使用$tool.call来消除返回值\n        $tool.call($temp.add($column.type))\n    #end\n#end\n##覆盖\n#set($importList = $temp)"},{"name":"mybatisSupport.vm","value":"##针对Mybatis 进行支持,主要用于生成xml文件\n#foreach($column in $tableInfo.fullColumn)\n    ##储存列类型\n    $tool.call($column.ext.put(\"sqlType\", $tool.getField($column.obj.dataType, \"typeName\")))\n    #if($tool.newHashSet(\"java.lang.String\").contains($column.type))\n        #set($jdbcType=\"VARCHAR\")\n    #elseif($tool.newHashSet(\"java.lang.Boolean\", \"boolean\").contains($column.type))\n        #set($jdbcType=\"BOOLEAN\")\n    #elseif($tool.newHashSet(\"java.lang.Byte\", \"byte\").contains($column.type))\n        #set($jdbcType=\"BYTE\")\n    #elseif($tool.newHashSet(\"java.lang.Integer\", \"int\", \"java.lang.Short\", \"short\").contains($column.type))\n        #set($jdbcType=\"INTEGER\")\n    #elseif($tool.newHashSet(\"java.lang.Long\", \"long\").contains($column.type))\n        #set($jdbcType=\"INTEGER\")\n    #elseif($tool.newHashSet(\"java.lang.Float\", \"float\", \"java.lang.Double\", \"double\").contains($column.type))\n        #set($jdbcType=\"NUMERIC\")\n    #elseif($tool.newHashSet(\"java.util.Date\", \"java.sql.Timestamp\", \"java.time.Instant\", \"java.time.LocalDateTime\", \"java.time.OffsetDateTime\", \"\tjava.time.ZonedDateTime\").contains($column.type))\n        #set($jdbcType=\"TIMESTAMP\")\n    #elseif($tool.newHashSet(\"java.sql.Date\", \"java.time.LocalDate\").contains($column.type))\n        #set($jdbcType=\"TIMESTAMP\")\n    #else\n        ##其他类型\n        #set($jdbcType=\"VARCHAR\")\n    #end\n    $tool.call($column.ext.put(\"jdbcType\", $jdbcType))\n#end\n\n##定义宏,查询所有列\n#macro(allSqlColumn)#foreach($column in $tableInfo.fullColumn)$column.obj.name#if($velocityHasNext), #end#end#end\n"}]}}}

image-1654312763796

然后就可以尽情的玩耍啦

EasyCode --> Generate Code

image-1654310806330

image-1654311072986

注意: 默认生成实体的set/get只支持Timestamp的clone模式. 要想支持更多set/get clone模式. 则参考:set/get clone模式

打赏
支付宝 微信
上一篇 下一篇