写在开始:一个搬砖程序员的随缘记录
文章目录
一、结构
1、项目目录结构
![](https://i-blog.csdnimg.cn/blog_migrate/20ced40d4c60782031eed9301c7f6cb4.png)
2、数据库表结构
二、Easycode-MybatisCodeHelper
1、插件下载
下载完重启idea
2、头部注释设置
3、Type Mapper
4、Global Config
主要是针对init文件进行了修改。在生成实体类的时候排除了公共字段
init
##初始化区域
##去掉表的t_前缀
#if($tableInfo.obj.name.startsWith("自定义前缀_"))
$!tableInfo.setName($tool.getClassName($tableInfo.obj.name.substring(4)))
#end
## 标记删除字段
#set($delete = "is_delete")
## 创建时间标识
#set($createTime = "create_time")
## 公共字段
#set($publicFelids = $tool.newHashSet(
"id",
"createUser",
"createTime",
"updateUser",
"updateTime",
"isDelete"
))
##实现动态排除列
#set($temp = $tool.newHashSet("testCreateTime", "otherColumn"))
#foreach($item in $temp)
#set($newList = $tool.newArrayList())
#foreach($column in $tableInfo.fullColumn)
#if($column.name!=$item)
##带有反回值的方法调用时使用$tool.call来消除返回值
$tool.call($newList.add($column))
#end
#end
##重新保存
$tableInfo.setFullColumn($newList)
#end
##对importList进行篡改
#set($temp = $tool.newHashSet())
#foreach($column in $tableInfo.fullColumn)
#if(!$column.type.startsWith("java.lang."))
##带有反回值的方法调用时使用$tool.call来消除返回值
$tool.call($temp.add($column.type))
#end
#end
##覆盖
#set($importList = $temp)
5.需要的jar包、配置和公共实体类
pom.xml
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- mysql -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.1</version>
</dependency>
application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/easy_code?useUnicode=true&characterEncoding=utf-8
username: root
password: admin
driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
global-config:
db-config:
logic-delete-field: is_delete #全局逻辑删除字段值
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
mapperLocations: classpath:mybatis/*
公共字段实体类
AbstractEntity.Java
package com.cn.easycode.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* @author demo
* @date 2021/12/30 14:30
*/
@Data
public class AbstractEntity {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "创建人")
private String createUser;
@ApiModelProperty(value = "创建时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
@ApiModelProperty(value = "修改人")
private String updateUser;
@ApiModelProperty(value = "修改时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
6、Template Setting
可以添加自定义的组,不改变默认的。针对不同的项目结构进行不同的配置
下面是我创建的EasyCode的
创建后是空的。然后按我的习惯需要创建:
entity.java 实体类
controller.java 控制层
service.java 接口层
serviceImpl.java 接口实现层
mapper.java 数据访问层
mapper.xml 数据访问层xml
创建后的
具体的代码配置:
entity.java
##引入宏定义
$!init
$!define
##定义初始变量
#set($tableName=$tool.append($tableInfo.name,""))
##设置回调
$!callback.setFileName($tool.append($tableName,".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath,"/entity"))
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
import com.cn.easycode.entity.AbstractEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import com.baomidou.mybatisplus.annotation.TableName;
#tableComment("数据访问类")
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("$!{tableInfo.obj.name}")
@ApiModel("$!{tableInfo.comment}实体类")
public class $!{tableName} extends AbstractEntity implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.otherColumn)
#set($have = false)
#foreach($felid in $publicFelids)
#if($column.name==$felid)
#set($have = true)
#break
#end
#end
#if(!$have)
#if(${column.comment})@ApiModelProperty(value = "${column.comment}") #end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#end
}
controller.java
##定义初始变量
###set($tableName = $tool.append($tableInfo.name, "Controller"))
#set($tableName = $tool.append($tableInfo.name.replaceFirst("Tbl",""),"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.core.metadata.IPage;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$tool.firstLowerCase($tableInfo.name.replaceFirst("Tbl",""))")
@Slf4j
@Api(tags = "$!{tableInfo.comment}-API")
public class $!{tableName} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 分页查询
*
* @param page 页数
* @param limit 条数
* @return TblUserDat
*/
@GetMapping("/getPage")
@ApiOperation("分页查询")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页数", defaultValue = "1", required = true, paramType = "query"),
@ApiImplicitParam(name = "pageSize", value = "条数", defaultValue = "10", required = true, paramType = "query")}
)
public IPage<$!{tableInfo.name}> getPage(Integer page, Integer limit) {
log.info("getPage, : {}, : {}", page, limit);
return tblUserDatService.getPage(page, limit);
}
/**
* 列表查询(不分页)
*
* @return TblUserDat
*/
@GetMapping("/getList")
@ApiOperation("列表查询(不分页)")
public List<$!{tableInfo.name}> getList() {
return tblUserDatService.getList();
}
/**
* 根据id查询详情
*
* @param id
* @return TblSymlxcDemo
*/
@GetMapping("/getById/{id}")
@ApiOperation("根据id查询详情")
public $!{tableInfo.name} getById(@PathVariable("id") $!pk.shortType id) {
log.info("getById, : {}", id);
return tblUserDatService.getById(id);
}
/**
* 新增
*
* @param dat 入参
*/
@PostMapping("/add")
@ApiOperation("新增")
public $!{tableInfo.name} add(TblUserDat dat) {
log.info("add, : {}", dat);
return tblUserDatService.add(dat);
}
/**
* 修改
*
* @param dat 入参
*/
@PostMapping("/update")
@ApiOperation("修改")
public $!{tableInfo.name} update(TblUserDat dat) {
log.info("update, : {}", dat);
return tblUserDatService.update(dat);
}
/**
* 删除
*
* @param id id
*/
@PostMapping("/delete/{id}")
@ApiOperation("删除")
public $!pk.shortType delete(@PathVariable("id") $!pk.shortType id) {
log.info("delete, : {}", id);
return tblUserDatService.delete(id);
}
}
service.java
##引入宏定义
$!init
$!define
##定义初始变量
#set($tableName=$tool.append($tableInfo.name,"Service"))
#set($reqName=$tool.append("AddOrUpdate",$tableInfo.name.replaceFirst("TblSymlxc",""),"Req"))
##设置回调
$!callback.setFileName($tool.append($tableName,".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath,"/service"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##使用宏定义设置包后缀
#setPackageSuffix("service")
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})业务层接口
*
* @author $!author
* @date $!time.currTime()
*/
public interface $!{tableName} extends IService<$!{tableInfo.name}> {
/**
* 分页查询
*
* @param page 页数
* @param limit 条数
* @return $!{tableInfo.name}
*/
IPage<$!{tableInfo.name}> getPage(Integer page, Integer limit);
/**
* 列表查询(不分页)
*
* @return $!{tableInfo.name}
*/
List<$!{tableInfo.name}> getList();
/**
* 根据id查询详情
*
* @param id
* @return TblSymlxcDemo
*/
$!{tableInfo.name} getById($!pk.shortType id);
/**
* 新增
*
* @param dat 入参
*/
$!{tableInfo.name} add($!{tableInfo.name} dat);
/**
* 修改
*
* @param dat 入参
*/
$!{tableInfo.name} update($!{tableInfo.name} dat);
/**
* 删除
*
* @param id id
*/
$!pk.shortType delete($!pk.shortType id);
}
serviceImpl.java
##引入宏定义
$!init
$!define
##定义初始变量
#set($tableName=$tool.append($tableInfo.name,"ServiceImpl"))
#set($reqName=$tool.append("AddOrUpdate",$tableInfo.name.replaceFirst("TblSymlxc",""),"Req"))
##设置回调
$!callback.setFileName($tool.append($tableName,".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath,"/service/impl"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##使用宏定义设置包后缀
#setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})业务层实现
*
* @author $!author
* @date $!time.currTime()
*/
@Slf4j
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
@Override
public IPage<$!{tableInfo.name}> getPage(Integer page, Integer limit) {
Page<$!{tableInfo.name}> pages = new Page<>(page, limit);
QueryWrapper<$!{tableInfo.name}> queryWrapper = new QueryWrapper<>();
return baseMapper.selectPage(pages, queryWrapper);
}
@Override
public List<$!{tableInfo.name}> getList() {
QueryWrapper<$!{tableInfo.name}> queryWrapper = new QueryWrapper<>();
return baseMapper.selectList(queryWrapper);
}
@Override
public $!{tableInfo.name} getById($!pk.shortType id) {
return baseMapper.selectById(id);
}
@Override
public $!{tableInfo.name} add($!{tableInfo.name} dat) {
dat.setCreateUser(1);
dat.setUpdateUser(1);
baseMapper.insert(dat);
return dat;
}
@Override
public $!{tableInfo.name} update($!{tableInfo.name} dat) {
if (dat.getId() == null) {
log.info("id不能为空");
return dat;
}
dat.setUpdateUser(1);
baseMapper.updateById(dat);
return dat;
}
@Override
public $!pk.shortType delete($!pk.shortType id) {
return baseMapper.deleteById(id);
}
}
mapper.java
##引入宏定义
$!init
$!define
##定义初始变量
#set($tableName=$tool.append($tableInfo.name,"Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName,".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath,"/mapper"))
##使用宏定义设置包后缀
#setPackageSuffix("mapper")
##使用全局变量实现默认包导入
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
/**
* $!{tableInfo.comment}($!{tableInfo.name})持久层
*
* @author $!author
* @date $!time.currTime()
*/
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}>{
}
mapper.xml
##引入宏定义
$!init
$!define
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
#set($tempIndex = $!tableInfo.savePackageName.lastIndexOf('.') + 1)
##set($mapppath=$tableInfo.savePackageName.substring($tempIndex))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mybatis/"))
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper">
</mapper>
根据自己的项目进行修改
Over