Easy code(代码自动生成包括模板)
准备操作
1、下载插件
- ps:重启才能奏效
2、配置模板
controller.java.vm
##定义初始变量
#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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.domain.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import $!{tableInfo.savePackageName}.util.R;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
/**
* $!{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;
/**
* 分页查询所有数据
*/
@GetMapping
public R page(@RequestParam int current,@RequestParam int size){
Page<$!{tableInfo.name}> page=new Page<>(current,size);
return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.page(page));}
/**
* 通过主键查询单条数据
*/
@GetMapping("{id}")
public R selectOne(@PathVariable Serializable id){
return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.getById(id));}
/**
* 新增数据
*/
@PostMapping
public R save(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.save($!tool.firstLowerCase($tableInfo.name)));}
/**
* 修改数据
*/
@PutMapping
public R updateById(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.updateById($!tool.firstLowerCase($tableInfo.name)));}
/**
* 单条/批量删除数据
*/
@DeleteMapping
public R delete(@RequestParam List<Long> id){
return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.removeByIds(id));}
}
dao.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Mapper")
##保存文件(宏定义)
#save("/mapper", "Mapper.java")
##包路径(宏定义)
#setPackageSuffix("mapper")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import org.apache.ibatis.annotations.Mapper;
##表注释(宏定义)
#tableComment("表数据库访问层")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
entity.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Mapper")
##保存文件(宏定义)
#save("/mapper", "Mapper.java")
##包路径(宏定义)
#setPackageSuffix("mapper")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import org.apache.ibatis.annotations.Mapper;
##表注释(宏定义)
#tableComment("表数据库访问层")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
service.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Service")
##保存文件(宏定义)
#save("/service", "Service.java")
##包路径(宏定义)
#setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
serviceImpl.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")
##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")
##包路径(宏定义)
#setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
##表注释(宏定义)
#tableComment("表服务实现类")
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
r.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/util", "R.java")
##使用宏定义设置包后缀
#setPackageSuffix("util")
import lombok.Data;
/**
* 统一返回对象R
*
* @author $!author
* @since $!time.currTime()
*/
@Data
public class R {
private int code;
private String message;
private Object data;
/**
* 设置数据
*
* @param data 数据
* @return R
*/
public R setData(Object data) {
this.data = data;
return this;
}
/**
* 操作成功
*
* @return R
*/
public static R ok() {
R r = new R();
r.code = 200;
r.message = "OK";
return r;
}
/**
* 操作失败
*
* @return R
*/
public static R fail() {
R r = new R();
r.code = 205;
r.message = "fail";
return r;
}
/**
* 系统异常
*/
public static R exp() {
R r = new R();
r.code = 500;
r.message = "exception";
return r;
}
}
interceptor3.5.1.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/interceptor", "MybatisPlusPageInterceptor.java")
##使用宏定义设置包后缀
#setPackageSuffix("interceptor")
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* MyBatis-Plus 3.5.1 拦截器
*
* @author $!author
* @since $!time.currTime()
*/
@Configuration
public class MybatisPlusPageInterceptor {
/**
* 新版分页插件设置
*/
@Bean
public MybatisPlusInterceptor getPaginationInnerInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
handler3.5.1.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/handler","MyMetaObjectHandler.java")
##使用宏定义设置包后缀
#setPackageSuffix("handler")
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* MyBatis-Plus 3.5.1 处理器
*
* @author $!author
* @since $!time.currTime()
*/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
/**
* 插入时的填充策略
*
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime" , new Date(), metaObject);
this.setFieldValByName("updateTime" , new Date(), metaObject);
}
/**
* 更新时的填充策略
*
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime" , new Date(), metaObject);
}
}
application.yaml.vm
##设置保存名称与保存位置
$!callback.setFileName("application.yaml")
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources"))
spring:
datasource:
url: url
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
#配置日志,我们所用的sql现在是不可见的,我们希望知道他是怎么执行的,所以我们必须要查看日志!
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
开始生成代码
1、准备好数据库
- ps:数据库名字别忘了改成你自己的
url:jdbc:mysql://localhost:3306/bookdb?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8&useUnicode=true&allowPublicKeyRetrieval=true
2、开始生成代码
- 看看效果