Skip to content

Commit e266b1b

Browse files
committed
完成Mybatis Plus基础模板
1 parent ae51f56 commit e266b1b

6 files changed

Lines changed: 69 additions & 35 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
##自动导入包(仅导入实体属性需要的包,通常用于实体类)
2-
#foreach($item in $importList)
3-
import $!item;
2+
#foreach($import in $importList)
3+
import $!import;
44
#end

src/main/resources/template/MybatisPlus/controller.vm

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,45 @@ $!define
1010
##包路径(宏定义)
1111
#setPackageSuffix("controller")
1212

13-
import org.springframework.web.bind.annotation.*;
14-
import javax.annotation.Resource;
15-
import com.alibaba.fastjson.JSON;
13+
##定义服务名
14+
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
15+
16+
##定义实体对象名
17+
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
18+
19+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
20+
import com.baomidou.mybatisplus.extension.api.ApiController;
21+
import com.baomidou.mybatisplus.extension.api.ApiResult;
22+
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
1623
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
1724
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
25+
import org.springframework.web.bind.annotation.*;
26+
27+
import javax.annotation.Resource;
28+
import java.io.Serializable;
1829
import java.util.List;
1930

2031
##表注释(宏定义)
2132
#tableComment("表控制层")
2233
@RestController
23-
@RequestMapping("$!tool.firstLowerCase($!{tableName})")
24-
public class $!{tableName} {
34+
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
35+
public class $!{tableName} extends ApiController {
36+
/**
37+
* 服务对象
38+
*/
2539
@Resource
26-
private $!{tableInfo.name}Service $!tool.firstLowerCase($!tableInfo.name)Service;
40+
private $!{tableInfo.name}Service $!{serviceName};
2741

2842
/**
2943
* 分页查询所有数据
3044
*
45+
* @param page 分页对象
46+
* @param $!entityName 查询实体
3147
* @return 所有数据
3248
*/
3349
@GetMapping
34-
public String selectAll() {
35-
return success(this.$!tool.firstLowerCase($!tableInfo.name)Service.selectAll());
50+
public ApiResult selectAll(Page<UserInfo> page, $!tableInfo.name $!entityName) {
51+
return success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName)));
3652
}
3753

3854
/**
@@ -42,30 +58,30 @@ public class $!{tableName} {
4258
* @return 单条数据
4359
*/
4460
@GetMapping("{id}")
45-
public String selectOne(@PathVariable Long id) {
46-
return success(this.$!tool.firstLowerCase($!tableInfo.name)Service.queryById(id));
61+
public ApiResult selectOne(@PathVariable Serializable id) {
62+
return success(this.$!{serviceName}.getById(id));
4763
}
4864

4965
/**
5066
* 新增数据
5167
*
52-
* @param entity 实体对象
68+
* @param $!entityName 实体对象
5369
* @return 新增结果
5470
*/
5571
@PostMapping
56-
public String insert(@RequestBody $!tableInfo.name entity) {
57-
return success(this.$!tool.firstLowerCase($!tableInfo.name)Service.insert(entity));
72+
public ApiResult insert(@RequestBody $!tableInfo.name $!entityName) {
73+
return success(this.$!{serviceName}.save($!entityName));
5874
}
5975

6076
/**
6177
* 修改数据
6278
*
63-
* @param entity 实体对象
79+
* @param $!entityName 实体对象
6480
* @return 修改结果
6581
*/
6682
@PutMapping
67-
public String update(@RequestBody $!tableInfo.name entity) {
68-
return success(this.$!tool.firstLowerCase($!tableInfo.name)Service.update(entity));
83+
public ApiResult update(@RequestBody $!tableInfo.name $!entityName) {
84+
return success(this.$!{serviceName}.updateById($!entityName));
6985
}
7086

7187
/**
@@ -75,17 +91,7 @@ public class $!{tableName} {
7591
* @return 删除结果
7692
*/
7793
@DeleteMapping
78-
public String delete(@RequestParam("idList") List<Long> idList) {
79-
return success(this.$!tool.firstLowerCase($!tableInfo.name)Service.deleteByIdList(idList));
80-
}
81-
82-
/**
83-
* 成功返回方法
84-
*
85-
* @param obj 对象
86-
* @return JSON字符串
87-
*/
88-
public String success(Object obj) {
89-
return JSON.toJSONString(obj);
94+
public ApiResult delete(@RequestParam("idList") List<Long> idList) {
95+
return success(this.$!{serviceName}.removeByIds(idList));
9096
}
9197
}

src/main/resources/template/MybatisPlus/dao.vm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ $!define
1010
##包路径(宏定义)
1111
#setPackageSuffix("dao")
1212

13+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
14+
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
15+
1316
##表注释(宏定义)
1417
#tableComment("表数据库访问层")
15-
public interface $!{tableName} {
18+
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
1619

1720
}

src/main/resources/template/MybatisPlus/entity.vm

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ $!define
88
#setPackageSuffix("entity")
99

1010
##导入包(全局变量)
11-
$!import
11+
$!autoImport
12+
import com.baomidou.mybatisplus.extension.activerecord.Model;
13+
import java.io.Serializable;
1214

1315
##表注释(宏定义)
1416
#tableComment("表实体类")
15-
public class $!{tableInfo.name} {
17+
@SuppressWarnings("serial")
18+
public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> {
1619
#foreach($column in $tableInfo.fullColumn)
1720
#if(${column.comment})//${column.comment}#end
1821

@@ -23,4 +26,16 @@ public class $!{tableInfo.name} {
2326
#getSetMethod($column)
2427
#end
2528

29+
#foreach($column in $tableInfo.pkColumn)
30+
/**
31+
* 获取主键值
32+
*
33+
* @return 主键值
34+
*/
35+
@Override
36+
protected Serializable pkVal() {
37+
return this.$!column.name;
38+
}
39+
#break
40+
#end
2641
}

src/main/resources/template/MybatisPlus/service.vm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ $!define
1010
##包路径(宏定义)
1111
#setPackageSuffix("service")
1212

13+
import com.baomidou.mybatisplus.extension.service.IService;
14+
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
15+
1316
##表注释(宏定义)
1417
#tableComment("表服务接口")
15-
public interface $!{tableName} {
18+
public interface $!{tableName} extends IService<$!tableInfo.name> {
1619

1720
}

src/main/resources/template/MybatisPlus/serviceImpl.vm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ $!define
1010
##包路径(宏定义)
1111
#setPackageSuffix("service.impl")
1212

13+
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
14+
import $!{tableInfo.savePackageName}.dao.UserInfoDao;
15+
import $!{tableInfo.savePackageName}.entity.UserInfo;
16+
import $!{tableInfo.savePackageName}.service.UserInfoService;
17+
import org.springframework.stereotype.Service;
18+
1319
##表注释(宏定义)
1420
#tableComment("表服务实现类")
15-
public class $!{tableName} implements $!{tableInfo.name}Service {
21+
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
22+
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
1623

1724
}

0 commit comments

Comments
 (0)