mybatis-plus生成mapper扩展文件的方法

2025-05-29 0 85

阅读提示

  具有mybatis基础,熟练使用mybatis-plus。

概述

  我们都知道,mybatis-plus是一个mybatis的增强工具,为简化开发、提高效率而生,我们经常使用mybatis-plus生成controller、service、mapper等文件,对于简单的curd,可以直接使用mybatis-plus封装好的方法。

  然而,我们经常有这样那样的需求,需要额外编写sql实现,如果直接在mapper.xml文件中编写,一旦数据库表结构改动需要重新生成文件就悲催了,不得不花大量精力修改代码。所以,这里介绍一种方式,自动生成mapper扩展文件,我们自定义编写的程序存放在扩展文件中,这样在数据库表结构改动时,不用担心程序会被覆盖,也不用修改代码。

mybatis-plus版本

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-generator</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>org.apache.velocity</groupId>

<artifactId>velocity-engine-core</artifactId>

<version>2.1</version>

</dependency>

mybatis-plus生成mapper扩展文件

  熟悉mybatis-plus的朋友都知道,mybatis-plus提供了一款代码生成器,可以自动生成代码,我们就从这款代码生成器入手。

  代码生成器配置完毕后,运行时会执行 AutoGenerator.execute() 方法,我们先看看这个东东

mybatis-plus生成mapper扩展文件的方法

熟悉的GlobalConfig、DataSourceConfig等等就不介绍了,我们关注的是InjectionConfig、TemplateConfig和ConfigBuilder。这里先讲述一下我们的思路:把ext文件通过配置直接生成,并保留mybatis-plus为service扩展的批量操作,我们需要三个文件,第一个文件生成ext.java,第二个文件生成ext.xml,第三个覆盖serviceImpl文件(保留mybatis-plus为service扩展的批量操作)

mybatis-plus生成mapper扩展文件的方法

**************************干货来咯**************************

?

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
// 生成目录

public static final String OUTPUT_DIR = "项目路径/src/main/java";

// mapperExt目录

public static final String MAPPER_EXT = "ext目录";

// 模板配置,这里可以自定义模板路径,如果路径如下所示,则该部分可以省略

TemplateConfig tc = new TemplateConfig();

tc.setServiceImpl("templates/serviceImpl.java");

ag.setTemplate(tc);

// 自定义配置

InjectionConfig cfg = new InjectionConfig() {

@Override

public void initMap() {

Map<String, Object> map = new HashMap<>(10);

/// 这里可以在 VM 文件中用 ${cfg.MapperExt} 引用该值

map.put("MapperExt", MAPPER_EXT.replace('/', '.'));

this.setMap(map);

}

};

// 自定义输出配置

List<FileOutConfig> focList = new ArrayList<>();

focList.add(new FileOutConfig("templates/mapperExt.xml.vm") {

@Override

public String outputFile(TableInfo tableInfo) {

return String.format("%s/%s/%sMapperExt%s", OUTPUT_DIR, MAPPER_EXT, tableInfo.getEntityName(), StringPool.DOT_XML);

}

});

focList.add(new FileOutConfig("templates/mapperExt.java.vm") {

@Override

public String outputFile(TableInfo tableInfo) {

return String.format("%s/%s/%sMapperExt%s", OUTPUT_DIR, MAPPER_EXT, tableInfo.getEntityName(), StringPool.DOT_JAVA);

}

});

cfg.setFileCreate(new IFileCreate() {

@Override

public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {

// 如果是 mapperExt、service、controller 文件,并且已存在则不创建

if (filePath.contains(MAPPER_EXT) || fileType == FileType.CONTROLLER || fileType == FileType.SERVICE || fileType == FileType.SERVICE_IMPL) {

if (new File(filePath).exists()) {

return false;

}

}

// 判断文件夹是否需要创建

checkDir(filePath);

return true;

}

});

cfg.setFileOutConfigList(focList);

ag.setCfg(cfg);

mapperExt.java.vm配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
package ${cfg.MapperExt};

import ${package.Mapper}.${table.mapperName};

/**

* <p>

* $!{table.comment} MapperExt 接口

* </p>

*

* @author ${author}

* @since ${date}

*/

#if(${kotlin})

interface ${table.mapperName}Ext : ${table.mapperName}

#else

public interface ${table.mapperName}Ext extends ${table.mapperName} {

}

#end

mapperExt.xml.vm配置

?

1

2

3

4

5
<?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="${cfg.MapperExt}.${table.mapperName}Ext">

</mapper>

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
package ${package.ServiceImpl};

import ${package.Entity}.${entity};

import ${cfg.MapperExt}.${table.mapperName}Ext;

import ${package.Service}.${table.serviceName};

import ${superServiceImplClassPackage};

import org.springframework.stereotype.Service;

/**

* <p>

* $!{table.comment} 服务实现类

* </p>

*

* @author ${author}

* @since ${date}

*/

@Service

#if(${kotlin})

open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}Ext, ${entity}>(), ${table.serviceName} {

}

#else

public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}Ext, ${entity}> implements ${table.serviceName} {

}

#end

新思路(2020-04-17补充)

  我们概述中所述问题真的存在吗?mybatis-plus如果需要扩展文件那么他为什么不提供呢?当然是问题不存在,根本不需要扩展文件,如果你存在这样的问题,你总是覆盖文件说明你的用法有问题。

  这里直接说应该怎么使用,我们把本文所述的扩展的.vm文件通通删掉,InjectionConfiginitMap方法清空,focList相关全部删除,修改cfg.setFileCreate(...)如下所示

?

1

2

3

4

5

6

7

8

9

10

11

12
cfg.setFileCreate(new IFileCreate() {

@Override

public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {

// 如果已存在并且不是实体类则不创建

if (new File(filePath).exists() && fileType != FileType.ENTITY) {

return false;

}

// 判断文件夹是否需要创建

checkDir(filePath);

return true;

}

});

  我们分析下:文件不存在一般是我们刚刚新建数据库表或者新增了一个表,此时肯定是要创建文件的。文件存在的时候分两种情况,一是文件是实体类,为了应对将来可能的新增修改删除字段,必须重写,那么使用时不能对该实体做任何增删改操作;二是文件非实体类,也就是service、map、xml等,因为代码生成和第一次生成没有什么区别,也就没有重写的必要,而且很多时候也已经编写了代码。

  如此,我们使用的时候只需要修改我们要生成的表就行了,即使把数据库表全部作为要生成的表也无所谓啦!

到此这篇关于mybatis-plus生成mapper扩展文件的方法的文章就介绍到这了,更多相关mybatis-plus生成mapper扩展文件内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/sinat_30735061/article/details/105444871

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 mybatis-plus生成mapper扩展文件的方法 https://www.kuaiidc.com/118090.html

相关文章

发表评论
暂无评论