当前位置: 移动技术网 > IT编程>开发语言>Java > mybatis-plus生成mapper扩展文件的方法

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

2020年09月02日  | 移动技术网IT编程  | 我要评论
阅读提示  具有mybatis基础,熟练使用mybatis-plus。概述  我们都知道,mybatis-plus是一个mybatis的增强工具,为简化

阅读提示

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

概述

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

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

mybatis-plus版本

<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() 方法,我们先看看这个东东


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

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

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

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配置

<?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配置

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(...)如下所示

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扩展文件内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网