mybatisPlus generator生成xml时自定义sql
最近需要用mybatis生成基础curd的内容,内网开发,mybatis的模板手敲太费劲,所以选择用mybatisPlus,由于项目需要,还是要生成的xml中有基础的增删改。
一、编写生成代码的配置
添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- springboot集成druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
</dependency>
<!-- 需要加一个模板引擎,生成代码的时候必须配置一个,默认是velocity,应该可以去掉,没再深究-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
参考官网的示例,具体参数意义可以看下官网,这么写是生成了实体类、dao、service、xml文件
public class FastAutoGeneratorMysqlTest {
private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG
= new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/test","root","root");
public static void main(String[] args) {
FastAutoGenerator
.create(DATA_SOURCE_CONFIG)
.globalConfig(builder -> { // 设置author及生成目录
builder.author("aaa")
.outputDir("D://");
})
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT){ // int类型映射
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
}))
.packageConfig(builder -> { // 生成代码的包名
builder.parent("com.demo.test")
.moduleName("dao")
.xml("mapper.xml")
.pathInfo(Collections.singletonMap(OutputFile.xml,"D://"));
})
.strategyConfig(builder -> {
builder.addInclude("t_user")
.mapperBuilder()
.superClass(BaseMapper.class)
.enableBaseResultMap() // 生成字段映射
.enableBaseColumnList(); // 生成基础列
})
.execute();
}
}
此时执行,使用的是默认的xml模板文件,生成的xml只有字段映射、全列名两部分。
二、复制模板文件
将mybatisPlus中的xml模板复制到自己项目的resource文件夹下,路径与源码中的保持一致,也就是重写源码中的模板文件(我是这个模板,不同的引擎,对应的文件后缀不同)。
三、编辑模板文件,添加自己需要生成的sql
例如下面的模板中添加了insert sql的生成,具体模板语法不太了解,复制的查询结果映射改的
<?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="${package.Mapper}.${table.mapperName}">
#if(${enableCache})
<!-- 开启二级缓存 -->
<cache type="${cacheClassName}"/>
#end
#if(${baseResultMap})
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<id column="${field.name}" property="${field.propertyName}" />
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段
<result column="${field.name}" property="${field.propertyName}" />
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<result column="${field.name}" property="${field.propertyName}" />
#end
#end
</resultMap>
#end
#if(${baseColumnList})
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
#foreach($field in ${table.commonFields})
${field.columnName},
#end
${table.fieldNames}
</sql>
#end
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<id column="${field.name}" property="${field.propertyName}" />
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段
<result column="${field.name}" property="${field.propertyName}" />
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<result column="${field.name}" property="${field.propertyName}" />
#end
#end
</mapper>
<insert id="insert">
INSERT INTO ${table.name}
<trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<if test="${field.propertyName} != null"> ${field.name},</if>
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段
<if test="${field.propertyName} != null"> ${field.name},</if>
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<if test="${field.propertyName} != null"> ${field.name},</if>
#end
#end
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<if test="${field.propertyName} != null"> #{${field.propertyName}},</if>
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段
<if test="${field.propertyName} != null"> #{${field.propertyName}},</if>
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<if test="${field.propertyName} != null"> #{${field.propertyName}},</if>
#end
#end
</trim>
</insert>
更改完xml模板文件后,再执行代码生成,会按照新的xml模板生成xml文件。