这是我参与更文挑战的第3天,活动详情查看: 更文挑战
EasyCode MybatisCodeHelper
每次创建项目模块时总是需要手动创建ctrl,service,dao三层,过于繁琐
EasyCode MybatisCodeHelper
可以很好帮助我们解决这个问题
下载插件
创建一个boot项目
略
创建数据库
create table user
(id int not null primary key,
username varchar(20) null,
birthday datetime null,
address varchar(20) null
);
配置mysql使用
连接时需要设置时区?serverTimezone=UTC
或设置Advanced的serverTimezone为Asia/Shanghai
user表右键easycode选择Generate Code,没有选项就重启下idea
生成文件如图
配置运行
dao添加@Mapper注解
package com.example.demo.dao;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* (User)表数据库访问层
*
* @author makejava
* @since 2021-06-03 11:24:05
*/
@Mapper
public interface UserDao {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
User queryById(Integer id);
/**
* 查询指定行数据
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
List<User> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);
/**
* 通过实体作为筛选条件查询
*
* @param user 实例对象
* @return 对象列表
*/
List<User> queryAll(User user);
/**
* 新增数据
*
* @param user 实例对象
* @return 影响行数
*/
int insert(User user);
/**
* 修改数据
*
* @param user 实例对象
* @return 影响行数
*/
int update(User user);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int deleteById(Integer id);
}
配置application.properties
#port
server.port=8801
#mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:/mapper/*.xml
#时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
运行测试成功
其他相关
类型映射
假设,我添加一个date类型的字段
File->setting
找到EasyCode映射
默认是没有data类型的映射,添加后成功生成entity实体
自定义模版 Template Setting
尝试自定义controller
默认模版只配置了一个selectOne()
##定义初始变量
#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} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public $!{tableInfo.name} selectOne($!pk.shortType id) {
return this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
}
}
写一个crud模版
##定义初始变量
#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} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public $!{tableInfo.name} selectOne($!pk.shortType id) {
return this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
}
/**
* 新增一条数据
*
* @param $!tool.firstLowerCase($tableInfo.name) 实体类
* @return Response对象
*/
@RequestMapping(value = "insert", method = RequestMethod.POST)
public void insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name));
return;
}
/**
* 修改一条数据
*
* @param $!tool.firstLowerCase($tableInfo.name) 实体类
* @return Response对象
*/
@RequestMapping(value = "update", method = RequestMethod.PUT)
public String update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
$tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name));
if (result != null) {
return "修改成功";
}
return "修改失败";
}
/**
* 删除一条数据
*
* @param $!tool.firstLowerCase($tableInfo.name) 参数对象
* @return Response对象
*/
@RequestMapping(value = "delete", method = RequestMethod.DELETE)
public String delete(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
boolean result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!{tool.firstLowerCase($tableInfo.name)}.getId());
if (result) {
return "删除成功";
}
return "删除失败";
}
}
测试成功运行