惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
人人都是产品经理
人人都是产品经理
月光博客
月光博客
V
V2EX
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
宝玉的分享
宝玉的分享
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
博客园 - 聂微东
博客园 - 司徒正美
博客园 - 【当耐特】
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志

博客园 - 月·漩涡

使用钉钉远程操作你的claude code java写的马赛克拼图生成软件 mybatis-generator 类型转化插件 mybatis-generator 注释插件 mybatis-generator lombok插件 mybatis-generator 自定义插件 Dubbo 使用rest协议发布http服务 Dubbo封装rest服务返回结果 dubbo rest返回值异常Incompatible types: declared root type 解决resteasy上传表单文件名乱码 RSA加密解密及RSA加签验签 使用OTP动态口令(每分钟变一次)进行登录认证 讲一下创业公司的技术架构演进 开源分布式任务调度平台Cuckoo-Schedule Nginx二级域名配置 Java分页下载 Solr6+IKAnalyzer分词环境搭建 消息发送平台简单架构设计 优先级线程池实现
mybatis-generator 唯一索引插件
月·漩涡 · 2020-10-26 · via 博客园 - 月·漩涡

因工作使用到了分库分表中,一般使用含有分库分表键的唯一索引操作数据库,而不是使用子增长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:表中无唯一索引,将不会生成这些唯一索引的相关操作。