1. 生成工具插件-安装

IDEA工具

image-20230707164419629

1.点击File—》settings—》Pluginsimage-20230707155645132

2. 插件使用

image-20230707160003853

2.1新建GroupName : 这里用唯一名称即可

2.2 点击加号,新增”controller.java.vn、entity.java.vm、mapper.java.vm、mapper.xml.vm、service.java.vm、serviceImpl.java.vm“

3. 修改模版

默认的分页有点问题,转成mybatis-plus的版本即可

1.controller.java.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
#set($objName = $!{tool.firstLowerCase($tableInfo.name)})
##设置回调
$!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.conditions.query.QueryWrapper;
import com.xxyx.result.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
* $!{tableInfo.comment}表控制器
*
* @author $!author
* @since $!time.currTime("yyyy-MM-dd HH:mm:ss")
*/
@Slf4j
@RestController
@RequestMapping("/${objName}")
@RequiredArgsConstructor
public class $!{tableName} {

/**
* 业务对象
*/
private final $!{tableInfo.name}Service ${objName}Service;

/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("/{$!{pk.name}}")
public R selectOne(@PathVariable $!pk.shortType $!{pk.name}) {
return R.success().data(this.${objName}Service.getById($!{pk.name}));
}

/**
* 分页查询数据

* @param page 分页参数
* @param $!tool.firstLowerCase($tableInfo.name) 查询条件封装
* @return 列表数据
*/
@GetMapping("/page")
public R selectPage(Page<$!{tableInfo.name}> page,$!{tableInfo.name} ${objName}) {
return R.success().data(this.${objName}Service.page(page, new QueryWrapper<>(${objName})));
}

/**
* 添加数据
*
* @param $!tool.firstLowerCase($tableInfo.name) 数据对象
* @return 成功与否boolean值
*/
@PostMapping
public R add(@Valid @RequestBody $!{tableInfo.name} ${objName}) {
return R.success().data(this.${objName}Service.save(${objName}));
}

/**
* 编辑数据
*
* @param $!tool.firstLowerCase($tableInfo.name) 数据对象
* @return 成功与否boolean值
*/
@PutMapping
public R edit(@Valid @RequestBody $!{tableInfo.name} ${objName}) {
return R.success().data(this.${objName}Service.updateById(${objName}));
}

/**
* 根据id删除单条数据
*
* @param $!{pk.name} 主键
* @return 成功与否boolean值
*/
@DeleteMapping("/{$!{pk.name}}")
public R remove(@PathVariable $!pk.shortType $!{pk.name}) {
return R.success().data(this.${objName}Service.removeById($!{pk.name}));
}

/**
* 根据id删除数据
*
* @param $!{pk.name}List 主键集合
* @return 成功与否boolean值
*/
@DeleteMapping
public R remove(@RequestBody List<$!pk.shortType> $!{pk.name}List) {
return R.success().data(this.${objName}Service.removeByIds($!{pk.name}List));
}
}

2.entity.java.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
##导入宏定义
$!{define.vm}

##保存文件(宏定义)
#save("/entity", ".java")

##包路径(宏定义)
#setPackageSuffix("entity")

##自动导入包(全局变量)
$!{autoImport.vm}
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.persistence.Table;
import java.io.Serializable;

/**
* $!{tableInfo.comment} 表实体类
*
* @author $!author
* @since $!time.currTime("yyyy-MM-dd HH:mm:ss")
*/
##表注释(宏定义)
#tableComment("表实体类")
@Data
@TableName("$!{tableInfo.obj.name}")
@ApiModel(description = "$!{tableInfo.comment}")
public class $!{tableInfo.name} extends BaseEntity {

private static final long serialVersionUID = $!tool.serial();
## 排除父类基础字段
#set($noFields = $tool.newHashSet("id","isDeleted","createUser","createDept","createTime","updateUser","updateDept","updateTime","status"))
#foreach($column in $tableInfo.fullColumn)
#if (!$noFields.contains($!{column.name}))
##时间类添加注解
#if(${column.comment})

/**
* ${column.comment}
*/
#end

@ApiModelProperty("${column.comment}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};

#end
#end

}

3.mapper.java.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper/$!{tableInfo.name}"))

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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Mapper;

/**
* $!{tableInfo.comment}表数据库访问层
*
* @author $!author
* @since $!time.currTime("yyyy-MM-dd HH:mm:ss")
*/
public interface $!{tableName} extends BaseMapper<$tableInfo.name> {

}

4.mapper.xml.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
##引入mybatis支持
$!mybatisSupport

##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper/$!{tableInfo.name}"))

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

<?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}.$!{tableInfo.name}Mapper">

<resultMap id="BaseResultMap" type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">

<!--@Table $!{tableInfo.originTableName}-->
#foreach($column in $tableInfo.fullColumn)
#if($pk.obj.name == $!column.obj.name)

<id property="$!column.name" column="$!column.obj.name"/>
#else

<result property="$!column.name" column="$!column.obj.name"/>
#end
#end

</resultMap>

<sql id="base_Column_List">
#foreach($column in $tableInfo.fullColumn)
$!column.obj.name#if($velocityHasNext),
#end
#end

</sql>

</mapper>

5.service.java.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

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

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import com.baomidou.mybatisplus.extension.service.IService;

/**
* $!{tableInfo.comment} 业务接口
*
* @author $!author
* @since $!time.currTime("yyyy-MM-dd HH:mm:ss")
*/
public interface $!{tableName} extends IService<$!{tableInfo.name}> {

}

6.serviceImpl.java.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!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

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

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
* $!{tableInfo.comment}表业务实现类
*
* @author $!author
* @since $!time.currTime("yyyy-MM-dd HH:mm:ss")
*/
@Slf4j
@Service
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper=false)
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper,$!{tableInfo.name}> implements $!{tableInfo.name}Service {

}

4. 开始一秒生成

右键IDEA的Database的某一张表,如下图显示:

image-20230707162047303

4.1 点击Generate Code(old)即可,新的不怎么好用。

4.2 选择参数如下图:

image-20230707162426520

4.3 点击OK,大功造成

5. 前端测试(分页)

浏览器地址:http://localhost:8080/student/page?current=1&size=100

返回结果:

1
{"success":true,"code":"200","message":"成功","data":{"body":{"records":[{"id":"2","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"222","age":222},{"id":"3","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"333","age":333},{"id":"4","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"444","age":1},{"id":"5","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"555","age":1},{"id":"6","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"666","age":6},{"id":"7","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"777","age":7},{"id":"8","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"888","age":8},{"id":"9","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"999","age":9},{"id":"10","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"1212","age":12},{"id":"11","createUser":null,"createDept":null,"createTime":null,"updateUser":null,"updateDept":null,"updateTime":null,"name":"wwww","age":100},{"id":"1677203967213711362","createUser":null,"createDept":null,"createTime":"2023-07-07 14:30:01","updateUser":null,"updateDept":null,"updateTime":null,"name":"k3","age":19}],"total":11,"size":100,"current":1,"orders":[],"optimizeCountSql":true,"searchCount":true,"maxLimit":null,"countId":null,"pages":1}}}