IDEA配置EasyCode插件实现代码自动生成

    项目协作开发当中,控制层及其他各个代码层的代码往往不尽相同。为了避免多人协作开发而出现代码格式不齐整,减少开发者的代码开发量,我们推荐使用IDEA的EasyCode插件来解决这一问题,实现项目代码的自动生成。

1.下载并安装EasyCode插件

    在idea的插件市场当中找到EasyCode插件,安装~

2.配置代码统一模板和数据库字段映射

    项目结构一般分为控制层、dao层、实体类层、mapper层、service层及实现层,在模板设置(Template Setting)中可以自定义它们。

    这里放上控制层的模板示例~

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表控制层
 *
 * @author $!author
 * @since $!time.currTime()
 */
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
    /**
     * 服务对象
     */
    @Autowired
    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    public $!{tableInfo.name} query$!{tableInfo.name}ById($!pk.shortType id) {
        return $!{tool.firstLowerCase($tableInfo.name)}Service.query$!{tableInfo.name}ById(id);
    }
    
    /**
     * 通过对象条件分页查询数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
     * @return 符合条件的数据
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonPage queryAll$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}),
    @RequestParam(defaultValue = "1")int pageNo,@RequestParam(defaultValue = "10")int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        return CommonPage.restPage($!{tool.firstLowerCase($tableInfo.name)}Service.queryAll$!{tableInfo.name}($!tool.firstLowerCase($!{tableInfo.name})));
    }
    
     /**
     * 新增
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
     * @return 单条数据
     */
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public $!{tableInfo.name} insert$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($tableInfo.name)}Service.insert$!{tableInfo.name}($!tool.firstLowerCase($!{tableInfo.name}));
    }
    
    /**
     * 更新
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
     * @return 
     */
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public $!{tableInfo.name} update$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($tableInfo.name)}Service.update$!{tableInfo.name}($!tool.firstLowerCase($!{tableInfo.name}));
    }
    
    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 
     */
    @DeleteMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean delete$!{tableInfo.name}ById($!pk.shortType id) {
        return $!{tool.firstLowerCase($tableInfo.name)}Service.delete$!{tableInfo.name}ById(id);
    }

}

    因为代码生成时需要与数据库的实体表属性相对应,所以在类型映射(TypeMapper)中要设置对应的映射。

3.新建数据库连接并生成代码

    在idea当中可以设置连接数据库(这里使用的是MySQL数据库)

    成功连接到数据库后,在需要生成代码的实体表上右键操作即可。

    选择正确的代码输出路径,然后勾选需要生成的类,完成~

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值