使用Mybatis Generator plugin修改自定义Mapper文件

本文介绍如何使用MyBatis Generator自定义插件,以在生成的Mapper文件中添加@Component注解,实现代码自动生成的定制化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis是一款优秀的半持久层框架,用于减少繁琐的jdbc数据库语句。而mybatis generator则可以生成标准的mybatis文件组,用来支撑业务层的代码;官网上提及它还支持使用Plugin来自定义标准文件。
mybatis generator(以下简称MGB)的插件plugin全部打包在 org.mybatis.generator.plugins 中,使用时要先实现org.mybatis.generator.api.Plugin 接口。MGB提供了org.mybatis.generator.api.PluginAdapter适配器,只要继承适配器就可以达到修改目的。

以添加Mapper文件类注释为例说明如何使用:

1.新建MybatisCustomService类

首先,新建一个类MybatisCustomService,它继承MapperAnnotationPlugin,而MapperAnnotationPlugin继承PluginAdapter,只要重写MapperAnnotationPlugin的方法,就可以添加所需的注解。
需要重写的方法有两个,一个是validate(List warnings),一个是clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
在clientGenerated方法内需要提供注解的包名和注解的名字两个参数,可以添加多个参数。

package com.fancici.codegenerate.service;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.plugins.MapperAnnotationPlugin;

import java.util.List;

/**
 * @Author : fancici
 * @Date : Create in 2019 02 2019/2/19 15:34
 * @Description : mybatis自定义服务。
 * @Paramter :
 **/
public class MybatisCustomService extends MapperAnnotationPlugin {

    @Override
    public boolean validate(List<String> warnings){
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Component"));
        interfaze.addAnnotation("@Component");
        return true;
    }
}

在这里我们往mapper文件上添加了@Component注解,其包名和注解名分别为org.springframework.stereotype.Component 和*@Component*;

2.在mybatis-generator.xml文件中添加MybatisCustomService类插件,令自定义代码生效。

mybatis-generator.xml文件中结点的位置是固定的,随意调整可能会使文件报错,因此我们设置的插件的位置,要在context结点下的第一位。
把MybatisCustomService类插件添加到配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<!-- 代码生成工具配置文件 -->
<!-- 配置参考:http://www.jianshu.com/p/e09d2370b796 -->
<generatorConfiguration>
  <!-- 引入配置文件 -->
  <!--
    <properties resource="config/application-generator.properties"/>
    -->
  <!-- 指定数据连接驱动jar地址 -->
  <classPathEntry location="C:/Users/fancici/.m2/repository/mariadb/mariadb-java-client-2.3.0.jar"/>
  <!-- 一个数据库一个context -->
  <context id="master" targetRuntime="MyBatis3" defaultModelType="flat">
    <!-- 添加插件,使数据文件添加新的注解-->
    <plugin type="com.fancici.codegenerate.service.MybatisCustomService" />
    <commentGenerator>
      <!-- 是否取消注释 -->
      <property name="suppressAllComments" value="true"/>
      <!-- 是否生成注释代时间戳-->
      <property name="suppressDate" value="true"/>
    </commentGenerator>
    <!-- jdbc连接 -->
    <jdbcConnection driverClass="org.mariadb.jdbc.Driver" connectionURL="jdbc:mariadb://127.0.0.1:3306/test?useSSL=false" userId="root" password="123456"/>
    <!-- 类型转换 -->
    <javaTypeResolver>
      <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
      <property name="forceBigDecimals" value="true"/>
    </javaTypeResolver>
    <!-- 生成实体类地址 -->
    <javaModelGenerator targetPackage="com.xxx.model" targetProject="D:/test/src/main/java">
      <!-- 是否在当前路径下新加一层schema,eg:fase路径com.xxx.model, true:com.xxx.model.[schemaName] -->
      <property name="enableSubPackages" value="true"/>
      <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
      <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!-- 生成mapxml文件 -->
    <sqlMapGenerator targetPackage="com.xxx.model" targetProject="D:/test/src/main/java">
      <!-- 是否在当前路径下新加一层schema,eg:fase路径com.xxx.model, true:com.xxx.model.[schemaName] -->
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!-- 生成mapxml对应client,也就是接口dao -->
    <javaClientGenerator targetPackage="com.xxx.model" type="XMLMAPPER" targetProject="D:/test/src/main/java">
      <!-- 是否在当前路径下新加一层schema,eg:fase路径com.xxx.model, true:com.xxx.model.[schemaName] -->
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!-- 配置表信息 -->
    <table tableName="%" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="true" enableDeleteByPrimaryKey="false">
      <generatedKey column="id" sqlStatement="MySql" identity="true"/>
    </table>
  </context>
</generatorConfiguration>

配置完成后,使用Mybatis generator 即可生成自定义代码;

参考文档:http://www.mybatis.org/generator/reference/pluggingIn.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值