mybatis-generator 唯一索引插件

因工作使用到了分库分表中,一般使用含有分库分表键的唯一索引操作数据库,而不是使用子增长id主键进行操作,因此写了一个唯一索引的数据库操作方式。

github地址:https://github.com/suyin58/mybatis-generator-tddl

提供的唯一索引操作包括:

根据唯一索引查询单条数据:selectByUniqueKey
根据唯一索引进行forupdate悲观锁操作:selectByUniqueKeyForUpdate
根据唯一索引更新数据:updateByUniqueKeySelective
根据唯一索引删除数据:deleteByUniqueKey
根据唯一索引进行存在即更新操作:upsertByUniqueKey

需要在配置文件中增加插件:(https://github.com/suyin58/mybatis-generator-tddl/blob/master/generator-test/src/test/resources/generatorConfigMyBatis3.xml)
<!-- 自定义插件开始 -->
        <!--序列化插件-->
        <plugin type="com.toolplat.generator.plugins.SerializablePlugin"/>
        <!-- lombok 插件 -->
        <plugin type="com.toolplat.generator.plugins.LombokPlugin">
            <property name="data" value="true"/>
            <property name="builder" value="true"/>
            <property name="allArgsConstructor" value="true"/>
            <property name="noArgsConstructor" value="true"/>
            <property name="toString" value="true"/>
        </plugin>
        <!-- 自定义unique key操作插件 -->
        <plugin type="com.toolplat.generator.plugins.SelectByUniqueKeyPlugin">
        </plugin>
        <plugin type="com.toolplat.generator.plugins.UpdateByUniqueKeySelectivePlugin">
        </plugin>
        <plugin type="com.toolplat.generator.plugins.DeleteByUniqueKeyPlugin">
        </plugin>
        <!--存在即更新插件-->
        <plugin type="com.toolplat.generator.plugins.UpsertByUniqueKeyPlugin">
        </plugin>
        <plugin type="com.toolplat.generator.plugins.UpsertByPrimaryKeyPlugin">
        </plugin>
        <!--批量插入插件-->
        <plugin type="com.toolplat.generator.plugins.BatchInsertPlugin">
        </plugin>
        <!--for update 插件-->
        <plugin type="com.toolplat.generator.plugins.SelectByPrimaryForUpdate">
        </plugin>
        <plugin type="com.toolplat.generator.plugins.SelectByExampleForUpdate">
        </plugin>
        <plugin type="com.toolplat.generator.plugins.SelectByUniqueKeyForUpdatePlugin">
        </plugin>
        <!--分页插件-->
        <plugin type="com.toolplat.generator.plugins.LimitPlugin">
        </plugin>
        <!-- 自定义插件结束 -->
        <!--自定义注释开始 -->
        <!-- commentGenerator 去除自动生成的注释  -->
        <commentGenerator type="com.toolplat.generator.plugins.CommentGenerator">
            <property name="author" value="szy" />
        </commentGenerator>
        <!--自定义注释结束 -->
View Code

 如果一个表只存在一个非主键的唯一索引,系统可以自动识别。如果存在非主键外的多个唯一索引,需要手动指定需要使用的唯一索引名称

        <table tableName="table_two_unique_key" domainObjectName="TableTwoUniqueKeyPO">
            <property name="uniqueKey" value="uk_org_cid"/>
        </table>
View Code

PS:表中无唯一索引,将不会生成这些唯一索引的相关操作。



原文地址:https://www.cnblogs.com/loveyou/p/13879599.html