day49_代码生成器EasyCode

发布时间:2023年12月20日

IDEA安装插件

easycode插件

image-20230909231216061

使用

image-20230909231508989

image-20230909231606053

修改模板

因为默认模板有些不适用自己项目,所以需要修改,以下是我的模板json文件

  • 生成了Controller,Service,Dao,Entity
  • 没有前后端交互的R,需要自己导入

在磁盘新建一个EasyCodeConfig.json,将以下代码赋值粘贴进去,

然后在idea的easycode插件中导入

{
  "author" : "Taotie",
  "version" : "1.2.8",
  "userSecure" : "",
  "currTypeMapperGroupName" : "Default",
  "currTemplateGroupName" : "Default",
  "currColumnConfigGroupName" : "Default",
  "currGlobalConfigGroupName" : "Default",
  "typeMapper" : { },
  "template" : {
    "Default" : {
      "name" : "Default",
      "elementList" : [ {
        "name" : "controller.java.vm",
        "code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Controller\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/controller\"))\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n    #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;\nimport org.springframework.web.bind.annotation.*;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.*;\nimport com.qf.util.R;\n/**\n * $!{tableInfo.comment}($!{tableInfo.name})表控制层\n *\n * @author $!author\n * @since $!time.currTime()\n */\n@RestController\n@RequestMapping(\"/api/$!tool.firstLowerCase($tableInfo.name)\")\npublic class $!{tableName} {\n    /**\n     * 服务对象\n     */\n    @Autowired\n    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;\n\n    /**\n     * 分页查询\n     *\n     * @return 查询结果\n     */\n    @GetMapping(\"/list/page\")\n    public R queryByPage($!pk.shortType pageNum) {\n        return R.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryByPage(pageNum));\n    }\n\n    /**\n     * 通过主键查询单条数据\n     *\n     * @param id 主键\n     * @return 单条数据\n     */\n    @GetMapping(\"/{id}\")\n    public R queryById(@PathVariable(\"id\") $!pk.shortType id) {\n        return R.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id));\n    }\n\n    /**\n     * 新增数据\n     *\n     * @param $!{tool.firstLowerCase($tableInfo.name)} 实体\n     * @return 新增结果\n     */\n    @PostMapping(\"/save\")\n    public R add(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {\n        this.$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)});\n        return R.ok();\n    }\n\n    /**\n     * 编辑数据\n     *\n     * @param $!{tool.firstLowerCase($tableInfo.name)} 实体\n     * @return 编辑结果\n     */\n    @PostMapping(\"/edit\")\n    public R edit(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {\n        return R.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)}));\n    }\n\n    /**\n     * 删除数据\n     *\n     * @param id 主键\n     * @return 删除是否成功\n     */\n    @GetMapping(\"/del\")\n    public R deleteById($!pk.shortType id) {\n        return R.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));\n    }\n\n}\n"
      }, {
        "name" : "debug.json.vm",
        "code" : "// 禁止将生成结果写入到文件\n$!callback.setWriteFile(false)\n\n//调试表原始对象\n$!tool.debug($tableInfo.obj)\n\n//调试列原始对象\n$!tool.debug($tableInfo.fullColumn.get(0).obj)\n\n//调试列原始列类型\n$!tool.debug($tableInfo.fullColumn.get(0).obj.dataType)\n\n//获取原始列类型中的字段\nsqlType = $!tool.getField($tableInfo.fullColumn.get(0).obj.dataType, \"typeName\")\n\n//执行原始列类型中的方法\nsqlTypeLen = $!tableInfo.fullColumn.get(0).obj.dataType.getLength()\n"
      }, {
        "name" : "entity.java.vm",
        "code" : "##引入宏定义\n$!{define.vm}\n\n##使用宏定义设置回调(保存位置与文件后缀)\n#save(\"/entity\", \".java\")\n\n##使用宏定义设置包后缀\n#setPackageSuffix(\"entity\")\n\n##使用全局变量实现默认包导入\n$!{autoImport.vm}\nimport java.io.Serializable;\n\n##使用宏定义实现类注释信息\n#tableComment(\"实体类\")\npublic class $!{tableInfo.name} implements Serializable {\n    private static final long serialVersionUID = $!tool.serial();\n#foreach($column in $tableInfo.fullColumn)\n    #if(${column.comment})/**\n     * ${column.comment}\n     */#end\n\n    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n#end\n\n#foreach($column in $tableInfo.fullColumn)\n##使用宏定义实现get,set方法\n#getSetMethod($column)\n#end\n\n}\n"
      }, {
        "name" : "mapper.xml.vm",
        "code" : "##引入mybatis支持\n$!{mybatisSupport.vm}\n\n##设置保存名称与保存位置\n$!callback.setFileName($tool.append($!{tableInfo.name}, \"Mapper.xml\"))\n$!callback.setSavePath($tool.append($modulePath, \"/src/main/resources/mapper\"))\n\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n    #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n<mapper namespace=\"$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper\">\n\n    <resultMap type=\"$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}\" id=\"$!{tableInfo.name}Map\">\n#foreach($column in $tableInfo.fullColumn)\n        <result property=\"$!column.name\" column=\"$!column.obj.name\" jdbcType=\"$!column.ext.jdbcType\"/>\n#end\n    </resultMap>\n\n    <!--查询单个-->\n    <select id=\"queryById\" resultMap=\"$!{tableInfo.name}Map\">\n        select\n          #allSqlColumn()\n###    新版2023.1idea 以及以后的都需要这么改\n###    修改当前文件中$velocityHasNext 为 $foreach.hasNext\n###    还需修改Global Config中mybatisSuport.vm中的$velocityHasNext改为$foreach.hasNext\n        from $!tableInfo.obj.name\n        where $!pk.obj.name = #{$!pk.name}\n    </select>\n\n    <!--查询多行数据-->\n    <select id=\"queryAllByLimit\" resultMap=\"$!{tableInfo.name}Map\">\n        select\n          #allSqlColumn()\n\n        from $!tableInfo.obj.name\n    </select>\n\n    <!--统计总行数-->\n    <select id=\"count\" resultType=\"java.lang.Long\">\n        select count(1)\n        from $!tableInfo.obj.name\n        <where>\n#foreach($column in $tableInfo.fullColumn)\n            <if test=\"$!column.name != null#if($column.type.equals(\"java.lang.String\")) and $!column.name != ''#end\">\n                and $!column.obj.name = #{$!column.name}\n            </if>\n#end\n        </where>\n    </select>\n\n    <!--新增所有列-->\n    <insert id=\"insert\" keyProperty=\"$!pk.name\" useGeneratedKeys=\"true\">\n        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)\n        values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)\n    </insert>\n\n    <insert id=\"insertBatch\" keyProperty=\"$!pk.name\" useGeneratedKeys=\"true\">\n        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)\n        values\n        <foreach collection=\"entities\" item=\"entity\" separator=\",\">\n        (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)\n        </foreach>\n    </insert>\n\n    <insert id=\"insertOrUpdateBatch\" keyProperty=\"$!pk.name\" useGeneratedKeys=\"true\">\n        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)\n        values\n        <foreach collection=\"entities\" item=\"entity\" separator=\",\">\n            (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)\n        </foreach>\n        on duplicate key update\n        #foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($velocityHasNext),\n        #end#end\n\n    </insert>\n\n    <!--通过主键修改数据-->\n    <update id=\"update\">\n        update $!{tableInfo.obj.name}\n        <set>\n#foreach($column in $tableInfo.otherColumn)\n            <if test=\"$!column.name != null#if($column.type.equals(\"java.lang.String\")) and $!column.name != ''#end\">\n                $!column.obj.name = #{$!column.name},\n            </if>\n#end\n        </set>\n        where $!pk.obj.name = #{$!pk.name}\n    </update>\n\n    <!--通过主键删除-->\n    <delete id=\"deleteById\">\n        delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}\n    </delete>\n\n</mapper>\n"
      }, {
        "name" : "service.java.vm",
        "code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Service\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/service\"))\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}service;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport java.util.List;\n/**\n * $!{tableInfo.comment}($!{tableInfo.name})表服务接口\n *\n * @author $!author\n * @since $!time.currTime()\n */\npublic interface $!{tableName} {\n\n    /**\n     * 通过ID查询单条数据\n     *\n     * @param $!pk.name 主键\n     * @return 实例对象\n     */\n    $!{tableInfo.name} queryById($!pk.shortType $!pk.name);\n\n    /**\n     * 分页查询\n     *\n     * @return 查询结果\n     */\n    List<$!{tableInfo.name}> queryByPage($!pk.shortType pageNum);\n\n    /**\n     * 新增数据\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象\n     * @return 实例对象\n     */\n    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n    /**\n     * 修改数据\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象\n     * @return 实例对象\n     */\n    int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n    /**\n     * 通过主键删除数据\n     *\n     * @param $!pk.name 主键\n     * @return 是否成功\n     */\n    boolean deleteById($!pk.shortType $!pk.name);\n\n}"
      }, {
        "name" : "serviceImpl.java.vm",
        "code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"ServiceImpl\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/service/impl\"))\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}service.impl;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;\nimport $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;\nimport org.springframework.stereotype.Service;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport java.util.List;\n/**\n * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类\n *\n * @author $!author\n * @since $!time.currTime()\n */\n@Service(\"$!tool.firstLowerCase($!{tableInfo.name})Service\")\npublic class $!{tableName} implements $!{tableInfo.name}Service {\n    @Autowired\n    private $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;\n\n    /**\n     * 通过ID查询单条数据\n     *\n     * @param $!pk.name 主键\n     * @return 实例对象\n     */\n    @Override\n    public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {\n        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryById($!pk.name);\n    }\n\n    /**\n     * 分页查询\n     *\n     * @return 查询结果\n     */\n    @Override\n    public List<$!{tableInfo.name}> queryByPage($!pk.shortType pageNum) {\n        return this.$!{tool.firstLowerCase($tableInfo.name)}Mapper.queryAllByLimit();\n    }\n\n    /**\n     * 新增数据\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象\n     * @return 实例对象\n     */\n    @Override\n    public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {\n        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.insert($!tool.firstLowerCase($!{tableInfo.name}));\n    }\n\n    /**\n     * 修改数据\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象\n     * @return 实例对象\n     */\n    @Override\n    public int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {\n        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.update($!tool.firstLowerCase($!{tableInfo.name}));\n    }\n\n    /**\n     * 通过主键删除数据\n     *\n     * @param $!pk.name 主键\n     * @return 是否成功\n     */\n    @Override\n    public boolean deleteById($!pk.shortType $!pk.name) {\n        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteById($!pk.name) > 0;\n    }\n}"
      }, {
        "name" : "mapper.java.vm",
        "code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Mapper\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/mapper\"))\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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport org.apache.ibatis.annotations.Param;\nimport java.util.List;\n\n/**\n * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层\n *\n * @author $!author\n * @since $!time.currTime()\n */\npublic interface $!{tableName} {\n\n    /**\n     * 通过ID查询单条数据\n     *\n     * @param $!pk.name 主键\n     * @return 实例对象\n     */\n    $!{tableInfo.name} queryById($!pk.shortType $!pk.name);\n\n    /**\n     * 查询指定行数据\n     *\n     * @return 对象列表\n     */\n    List<$!{tableInfo.name}> queryAllByLimit();\n\n    /**\n     * 统计总行数\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 查询条件\n     * @return 总行数\n     */\n    long count($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n    /**\n     * 新增数据\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象\n     * @return 影响行数\n     */\n    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n    /**\n     * 批量新增数据(MyBatis原生foreach方法)\n     *\n     * @param entities List<$!{tableInfo.name}> 实例对象列表\n     * @return 影响行数\n     */\n    int insertBatch(@Param(\"entities\") List<$!{tableInfo.name}> entities);\n\n    /**\n     * 批量新增或按主键更新数据(MyBatis原生foreach方法)\n     *\n     * @param entities List<$!{tableInfo.name}> 实例对象列表\n     * @return 影响行数\n     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参\n     */\n    int insertOrUpdateBatch(@Param(\"entities\") List<$!{tableInfo.name}> entities);\n\n    /**\n     * 修改数据\n     *\n     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象\n     * @return 影响行数\n     */\n    int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n    /**\n     * 通过主键删除数据\n     *\n     * @param $!pk.name 主键\n     * @return 影响行数\n     */\n    int deleteById($!pk.shortType $!pk.name);\n\n}\n"
      } ]
    }
  },
  "columnConfig" : { },
  "globalConfig" : { }
}

image-20230909232555754

文章来源:https://blog.csdn.net/weixin_39641494/article/details/135113164
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。