Mybatis Generator Plugin悲观锁实现示例

 更新时间:2021年09月28日 08:40:20   作者:raledong  
本文将从悲观锁为例,让你快速了解如何实现Mybatis Generator Plugin。文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

Mybatis Generator插件可以快速的实现基础的数据库CRUD操作,它同时支持JAVA语言和Kotlin语言,将程序员从重复的Mapper和Dao层代码编写中释放出来。Mybatis Generator可以自动生成大部分的SQL代码,如update,updateSelectively,insert,insertSelectively,select语句等。但是,当程序中需要SQL不在自动生成的SQL范围内时,就需要使用自定义Mapper来实现,即手动编写DAO层和Mapper文件(这里有一个小坑,当数据库实体增加字段时,对应的自定义Mapper也要及时手动更新)。抛开复杂的定制化SQL如join,group by等,其实还是有一些比较常用的SQL在基础的Mybatis Generator工具中没有自动生成,比如分页能力,悲观锁,乐观锁等,而Mybatis Generator也为这些诉求提供了Plugin的能力。通过自定义实现Plugin可以改变Mybatis Generator在生成Mapper和Dao文件时的行为。本文将从悲观锁为例,让你快速了解如何实现Mybatis Generator Plugin。

实现背景:

  • 数据库:MYSQL
  • mybatis generator runtime:MyBatis3

实现Mybatis悲观锁

当业务出现需要保证强一致的场景时,可以通过在事务中对数据行上悲观锁后再进行操作来实现,这就是经典的”一锁二判三更新“。在交易或是支付系统中,这种诉求非常普遍。Mysql提供了Select...For Update语句来实现对数据行上悲观锁。本文将不对Select...For Update进行详细的介绍,有兴趣的同学可以查看其它文章深入了解。

Mybatis Generator Plugin为这种具有通用性的SQL提供了很好的支持。通过继承org.mybatis.generator.api.PluginAdapter类即可自定义SQL生成逻辑并在在配置文件中使用。PluginAdapter是Plugin接口的实现类,提供了Plugin的默认实现,本文将介绍其中比较重要的几个方法:

public interface Plugin {
    /**
    * 将Mybatis Generator配置文件中的上下文信息传递到Plugin实现类中
    * 这些信息包括数据库链接,类型映射配置等
    */
    void setContext(Context context);

    /**
    * 配置文件中的所有properties标签
    **/
    void setProperties(Properties properties);

    /**
    * 校验该Plugin是否执行,如果返回false,则该插件不会执行
    **/
    boolean validate(List<String> warnings);

    /**
    * 当DAO文件完成生成后会触发该方法,可以通过实现该方法在DAO文件中新增方法或属性
    **/
    boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable);

    /**
    * 当SQL XML 文件生成后会调用该方法,可以通过实现该方法在MAPPER XML文件中新增XML定义
    **/
    boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable);
}

这里结合Mybatis Generator的配置文件和生成的DAO(也称为Client文件)和Mapper XML文件可以更好的理解。Mybatis Generator配置文件样例如下,其中包含了主要的一些配置信息,如用于描述数据库链接的<jdbcConnection>标签,用于定义数据库和Java类型转换的<javaTypeResolver>标签等。

<?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">

<generatorConfiguration>
  <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
        connectionURL="jdbc:db2:TEST"
        userId="db2admin"
        password="db2admin">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="test.xml"  targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <property name="printLog" value="true"/>

    <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table>

  </context>
</generatorConfiguration>

这些都被映射成Context对象,并通过setContext(Context context)方法传递到具体的Plugin实现中:

public class Context extends PropertyHolder{

    /**
    * <context>标签的id属性
    */
    private String id;

    /**
    * jdbc链接信息,对应<jdbcConnection>标签中的信息
    */
    private JDBCConnectionConfiguration jdbcConnectionConfiguration;

    /**
    * 类型映射配置,对应<javaTypeResolver>
    */
    private JavaTypeResolverConfiguration javaTypeResolverConfiguration;

    /**
    * ...其它标签对应的配置信息
    */
}

setProperties则将context下的<properties>标签收集起来并映射成Properties类,它实际上是一个Map容器,正如Properties类本身就继承了Hashtable。以上文中的配置文件为例,可以通过properties.get("printLog")获得值"true"。

validate方法则代表了这个Plugin是否执行,它通常进行一些非常基础的校验,比如是否兼容对应的数据库驱动或者是Mybatis版本:

    public boolean validate(List<String> warnings) {
        if (StringUtility.stringHasValue(this.getContext().getTargetRuntime()) && !"MyBatis3".equalsIgnoreCase(this.getContext().getTargetRuntime())) {
            logger.warn("itfsw:插件" + this.getClass().getTypeName() + "要求运行targetRuntime必须为MyBatis3!");
            return false;
        } else {
            return true;
        }
    }

如果validate方法返回false,则无论什么场景下都不会运行这个Plugin。

接着是最重要的两个方法,分别是用于在DAO中生成新的方法clientGenerated和在XML文件中生成新的SQL sqlMapDocumentGenerated。

先说clientGenerated,这个方法共有三个参数,interfaze是当前已经生成的客户端Dao接口,topLevelClass是指生成的实现类,这个类可能为空,introspectedTable是指当前处理的数据表,这里包含了从数据库中获取的关于表的各种信息,包括列名称,列类型等。这里可以看一下introspectedTable中几个比较重要的方法:

public abstract class IntrospectedTable {
    /**
    * 该方法可以获得配置文件中该表对应<table>标签下的配置信息,包括映射成的Mapper名称,PO名称等
    * 也可以在table标签下自定义<property>标签并通过getProperty方法获得值
    */
    public TableConfiguration getTableConfiguration() {
        return tableConfiguration;
    }

    /**
    * 这个方法中定义了默认的生成规则,可以通过calculateAllFieldsClass获得返回类型
    */
    public Rules getRules() {
        return rules;
    }
}

悲观锁的clientGenerated方法如下:

    // Plugin配置,是否要生成selectForUpdate语句
    private static final String CONFIG_XML_KEY = "implementSelectForUpdate";

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String implementUpdate = introspectedTable.getTableConfiguration().getProperty(CONFIG_XML_KEY);
        if (StringUtility.isTrue(implementUpdate)) {
            Method method = new Method(METHOD_NAME);
            FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass();
            method.setReturnType(returnType);
            method.addParameter(new Parameter(new FullyQualifiedJavaType("java.lang.Long"), "id"));
            String docComment = "/**\n" +
                    "      * 使用id对数据行上悲观锁\n" +
                    "      */";
            method.addJavaDocLine(docComment);
            interfaze.addMethod(method);
            log.debug("(悲观锁插件):" + interfaze.getType().getShortName() + "增加" + METHOD_NAME + "方法。");
        }

        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

这里可以通过在对应table下新增property标签来决定是否要为这张表生成对应的悲观锁方法,配置样例如下:

 <table tableName="demo" domainObjectName="DemoPO" mapperName="DemoMapper"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               enableInsert="true"
               selectByExampleQueryId="true">
    <property name="implementUpdateWithCAS" value="true"/>
 </table>

代码中通过mybatis提供的Method方法,定义了方法的名称,参数,返回类型等,并使用interfaze.addMethod方法将方法添加到客户端的接口中。

再到sqlMapDocumentGenerated这个方法,这个方法中传入了Document对象,它对应生成的XML文件,并通过XmlElement来映射XML文件中的元素。通过document.getRootElement().addElement可以将自定义的XML元素插入到Mapper文件中。自定义XML元素就是指拼接XmlElement,XmlElement的addAttribute方法可以为XML元素设置属性,addElement则可以为XML标签添加子元素。有两种类型的子元素,分别是TextElement和XmlElement本身,TextElement则直接填充标签中的内容,而XmlElement则对应新的标签,如<where> <include>等。悲观锁的SQL生成逻辑如下:

    // Plugin配置,是否要生成selectForUpdate语句
    private static final String CONFIG_XML_KEY = "implementSelectForUpdate";

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        String implementUpdate = introspectedTable.getTableConfiguration().getProperty(CONFIG_XML_KEY);
        if (!StringUtility.isTrue(implementUpdate)) {
            return super.sqlMapDocumentGenerated(document, introspectedTable);
        }

        XmlElement selectForUpdate = new XmlElement("select");
        selectForUpdate.addAttribute(new Attribute("id", METHOD_NAME));
        StringBuilder sb;

        String resultMapId = introspectedTable.hasBLOBColumns() ? introspectedTable.getResultMapWithBLOBsId() : introspectedTable.getBaseResultMapId();
        selectForUpdate.addAttribute(new Attribute("resultMap", resultMapId));
        selectForUpdate.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
        selectForUpdate.addElement(new TextElement("select"));

        sb = new StringBuilder();
        if (StringUtility.stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
            sb.append('\'');
            sb.append(introspectedTable.getSelectByExampleQueryId());
            sb.append("' as QUERYID,");
            selectForUpdate.addElement(new TextElement(sb.toString()));
        }

        XmlElement baseColumn = new XmlElement("include");
        baseColumn.addAttribute(new Attribute("refid", introspectedTable.getBaseColumnListId()));
        selectForUpdate.addElement(baseColumn);
        if (introspectedTable.hasBLOBColumns()) {
            selectForUpdate.addElement(new TextElement(","));
            XmlElement blobColumns = new XmlElement("include");
            blobColumns.addAttribute(new Attribute("refid", introspectedTable.getBaseColumnListId()));
            selectForUpdate.addElement(blobColumns);
        }

        sb.setLength(0);
        sb.append("from ");
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
        selectForUpdate.addElement(new TextElement(sb.toString()));
        TextElement whereXml = new TextElement("where id = #{id} for update");
        selectForUpdate.addElement(whereXml);

        document.getRootElement().addElement(selectForUpdate);
        log.debug("(悲观锁插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加" + METHOD_NAME + "方法(" + (introspectedTable.hasBLOBColumns() ? "有" : "无") + "Blob类型))。");
        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }

完整代码

@Slf4j
public class SelectForUpdatePlugin extends PluginAdapter {

    private static final String CONFIG_XML_KEY = "implementSelectForUpdate";

    private static final String METHOD_NAME = "selectByIdForUpdate";

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

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String implementUpdate = introspectedTable.getTableConfiguration().getProperty(CONFIG_XML_KEY);
        if (StringUtility.isTrue(implementUpdate)) {
            Method method = new Method(METHOD_NAME);
            FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass();
            method.setReturnType(returnType);
            method.addParameter(new Parameter(new FullyQualifiedJavaType("java.lang.Long"), "id"));
            String docComment = "/**\n" +
                    "      * 使用id对数据行上悲观锁\n" +
                    "      */";
            method.addJavaDocLine(docComment);
            interfaze.addMethod(method);
            log.debug("(悲观锁插件):" + interfaze.getType().getShortName() + "增加" + METHOD_NAME + "方法。");
        }

        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        String implementUpdate = introspectedTable.getTableConfiguration().getProperty(CONFIG_XML_KEY);
        if (!StringUtility.isTrue(implementUpdate)) {
            return super.sqlMapDocumentGenerated(document, introspectedTable);
        }

        XmlElement selectForUpdate = new XmlElement("select");
        selectForUpdate.addAttribute(new Attribute("id", METHOD_NAME));
        StringBuilder sb;

        String resultMapId = introspectedTable.hasBLOBColumns() ? introspectedTable.getResultMapWithBLOBsId() : introspectedTable.getBaseResultMapId();
        selectForUpdate.addAttribute(new Attribute("resultMap", resultMapId));
        selectForUpdate.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
        selectForUpdate.addElement(new TextElement("select"));

        sb = new StringBuilder();
        if (StringUtility.stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
            sb.append('\'');
            sb.append(introspectedTable.getSelectByExampleQueryId());
            sb.append("' as QUERYID,");
            selectForUpdate.addElement(new TextElement(sb.toString()));
        }

        XmlElement baseColumn = new XmlElement("include");
        baseColumn.addAttribute(new Attribute("refid", introspectedTable.getBaseColumnListId()));
        selectForUpdate.addElement(baseColumn);
        if (introspectedTable.hasBLOBColumns()) {
            selectForUpdate.addElement(new TextElement(","));
            XmlElement blobColumns = new XmlElement("include");
            blobColumns.addAttribute(new Attribute("refid", introspectedTable.getBaseColumnListId()));
            selectForUpdate.addElement(blobColumns);
        }

        sb.setLength(0);
        sb.append("from ");
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
        selectForUpdate.addElement(new TextElement(sb.toString()));
        TextElement whereXml = new TextElement("where id = #{id} for update");
        selectForUpdate.addElement(whereXml);

        document.getRootElement().addElement(selectForUpdate);
        log.debug("(悲观锁插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加" + METHOD_NAME + "方法(" + (introspectedTable.hasBLOBColumns() ? "有" : "无") + "Blob类型))。");
        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }
}

到此这篇关于Mybatis Generator Plugin悲观锁实现示例的文章就介绍到这了,更多相关Mybatis Generator Plugin悲观锁 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java图片对比度调整示例代码

    java图片对比度调整示例代码

    这篇文章主要给大家介绍了关于java图片对比度调整的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • Java实现Ip地址获取的示例代码

    Java实现Ip地址获取的示例代码

    这篇文章主要为大家详细介绍了Java实现Ip地址获取的两种方式,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2023-09-09
  • Java中seata框架的XA模式详解

    Java中seata框架的XA模式详解

    这篇文章主要介绍了Java中seata框架的XA模式详解,Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务,Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案,需要的朋友可以参考下
    2023-08-08
  • SpringMVC + jquery.uploadify实现上传文件功能

    SpringMVC + jquery.uploadify实现上传文件功能

    文件上传是很多项目都会使用到的功能,SpringMVC当然也提供了这个功能。不过小编不建议在项目中通过form表单来提交文件上传,这样做的局限性很大。下面这篇文章主要介绍了利用SpringMVC + jquery.uploadify实现上传文件功能的相关资料,需要的朋友可以参考下。
    2017-06-06
  • spring.profiles使用的方法步骤

    spring.profiles使用的方法步骤

    本文主要介绍了spring.profiles使用与spring.profiles.active和spring.profiles.include区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Java进阶:Struts多模块的技巧

    Java进阶:Struts多模块的技巧

    Java进阶:Struts多模块的技巧...
    2006-12-12
  • 如何用JAVA判断当前时间是否为节假日、周末、工作日及调休日(不报错:IOException!)

    如何用JAVA判断当前时间是否为节假日、周末、工作日及调休日(不报错:IOException!)

    最近公司有个业务需要判断工作日,但是每年的节假日不一样,下面这篇文章主要给大家介绍了关于如何用JAVA判断当前时间是否为节假日、周末、工作日及调休日的相关资料,且不报错:IOException!,需要的朋友可以参考下
    2023-12-12
  • Java中Spring Boot支付宝扫码支付及支付回调的实现代码

    Java中Spring Boot支付宝扫码支付及支付回调的实现代码

    这篇文章主要介绍了Java中Spring Boot支付宝扫码支付及支付回调的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • JDK、J2EE、J2SE、J2ME四个易混淆概念区分

    JDK、J2EE、J2SE、J2ME四个易混淆概念区分

    这篇文章将向你详细介绍JDK、J2EE、J2SE、J2ME的概念以及他们的关系区别。
    2015-09-09
  • java实现短地址服务的方法(附代码)

    java实现短地址服务的方法(附代码)

    大多数情况下URL太长,字符多,不便于发布复制和存储,本文就介绍了通过java实现短地址服务,减少了许多使用太长URL带来的不便,需要的朋友可以参考下
    2015-07-07

最新评论