一键生成Java代码结构

一键生成Java代码结构,第1张

一键生成Java代码结构

零、火人在球 感谢同事昊工!在线教学

一、IDEA安装插件

从 setting 里面安装easycode,然后导入这个配置

二、配置内容

配置内容:EasyCodeConfig.json

{"author":"wuhao8","version":"1.2.4","userSecure":"","currTypeMapperGroupName":"Default","currTemplateGroupName":"Default","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.lang.Double"},{"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":"date","javaType":"java.util.Date"},{"matchType":"ORDINARY","columnType":"datetime","javaType":"java.util.Date"},{"matchType":"ORDINARY","columnType":"timestamp","javaType":"java.util.Date"},{"matchType":"ORDINARY","columnType":"time","javaType":"java.time.LocalTime"},{"matchType":"ORDINARY","columnType":"boolean","javaType":"java.lang.Boolean"},{"matchType":"REGEX","columnType":"decimal(\(\d+\))?","javaType":"java.lang.Double"}]}},"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#endnn#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;nnimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};nimport $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;nimport org.springframework.data.domain.Page;nimport org.springframework.data.domain.PageRequest;nimport org.springframework.http.ResponseEntity;nimport org.springframework.web.bind.annotation.*;nnimport javax.annotation.Resource;nnn@RestControllern@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")npublic class $!{tableName} {n    n    @Resourcen    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;nn    n    @GetMappingn    public ResponseEntity> queryByPage($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, PageRequest pageRequest) {n        return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryByPage($!{tool.firstLowerCase($tableInfo.name)}, pageRequest));n    }nn    n    @GetMapping("{id}")n    public ResponseEntity<$!{tableInfo.name}> queryById(@PathVariable("id") $!pk.shortType id) {n        return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id));n    }nn    n    @PostMappingn    public ResponseEntity<$!{tableInfo.name}> add($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {n        return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)}));n    }nn    n    @PutMappingn    public ResponseEntity<$!{tableInfo.name}> edit($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {n        return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)}));n    }nn    n    @DeleteMappingn    public ResponseEntity deleteById($!pk.shortType id) {n        return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));n    }nn}n"},{"name":"dao.java.vm","code":"##定义初始变量n#set($tableName = $tool.append($tableInfo.name, "Dao"))n##设置回调n$!callback.setFileName($tool.append($tableName, ".java"))n$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))nn##拿到主键n#if(!$tableInfo.pkColumn.isEmpty())n    #set($pk = $tableInfo.pkColumn.get(0))n#endnn#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;nnimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};nimport org.apache.ibatis.annotations.Param;nimport org.springframework.data.domain.Pageable;nimport java.util.List;nnnpublic interface $!{tableName} {nn    n    $!{tableInfo.name} queryById($!pk.shortType $!pk.name);nn    n    List<$!{tableInfo.name}> queryAllByLimit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), @Param("pageable") Pageable pageable);nn    n    long count($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));nn    n    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));nn    n    int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities);nn    n    int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities);nn    n    int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));nn    n    int deleteById($!pk.shortType $!pk.name);nn}n"},{"name":"debug.json.vm","code":"// 禁止将生成结果写入到文件n##$!callback.setWriteFile(false)nn//调试表原始对象n##$!tool.debug($tableInfo.obj)nn//调试列原始对象n##$!tool.debug($tableInfo.fullColumn.get(0).obj)nn//调试列原始列类型n##$!tool.debug($tableInfo.fullColumn.get(0).obj)nn//获取原始列类型中的字段n##sqlType = $!tool.getField($tableInfo.fullColumn.get(0).obj.dataType, "typeName")nn//执行原始列类型中的方法nsqlTypeLen = $!tableInfo.fullColumn.get(1).obj.namen"},{"name":"entity.java.vm","code":"##引入宏定义n$!{define.vm}nn##使用宏定义设置回调(保存位置与文件后缀)n#save("/entity", ".java")nn##使用宏定义设置包后缀n#setPackageSuffix("entity")nn##使用全局变量实现默认包导入n$!{autoimport.vm}nimport java.io.Serializable;nn##使用宏定义实现类注释信息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})#endnn    private $!{tool.getClsNameByFullName($column.type)} $!{column.obj.name};n#endnn#foreach($column in $tableInfo.fullColumn)n##使用宏定义实现get,set方法n#getSetMethod($column)n#endn}n"},{"name":"mapper.xml.vm","code":"##引入mybatis支持n$!{mybatisSupport.vm}nn##设置保存名称与保存位置n$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml"))n$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))nn##拿到主键n#if(!$tableInfo.pkColumn.isEmpty())n    #set($pk = $tableInfo.pkColumn.get(0))n#endnnnnnn    n#foreach($column in $tableInfo.fullColumn)n        n#endn    nn    n    n        selectn          #allSqlColumn()nn        from $!tableInfo.obj.namen        n#foreach($column in $tableInfo.fullColumn)n            n                and $!column.obj.name = #{$!column.obj.name}n            n#endn        n        limit #{pageable.offset}, #{pageable.pageSize}n    nn    n