【Mybatis】Mybatis-Generator-Oracle返回主键插件

版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85150631

系列

  1. 【Mybatis】Mybatis-Generator-Postgresql返回主键插件
  2. 【Mybatis】Mybatis-Generator-Oracle返回主键插件
  3. 【Mybatis】Mybatis-Generator-batchDelete(批量删除)方法插件
  4. 【Mybatis】Mybatis-Generator-Postgresql&Mysql(批量新增)方法插件
  5. 【Mybatis】Mybatis-Generator-Oracle(批量新增)方法插件
  6. 【Mybatis】Mybatis-Generator-Mysql(批量更新)方法插件
  7. 【Mybatis】Mybatis-Generator-Postgresql(批量更新)方法插件
  8. 【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件
  9. 【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类

前言

Mysql批量新增插件,核心贴出来,有需要的看我的GITHUB,准备更新微服务BLOG

github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。

装备

  1. maven

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
    </dependencies>
    
  2. 这个插件提供给使用了序列自增ID的表

Core-Code

sql

SELECT T_HUI_TEST_TEST_SEQUENCE.nextval from dual')

mybatis-implement

<selectKey keyProperty="ID" order="BEFORE" resultType="java.math.BigDecimal">
      SELECT T_HUI_TEST_TEST_SEQUENCE.nextval from dual')
    </selectKey>

plugin-code

/**
 * <b><code>OracleReturnKeyPlugin</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2018/12/19 1:27.
 *
 * @author HuWeihui
 */
public class OracleReturnKeyPlugin  extends PluginAdapter {
    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * 新增SqlMapper.xml里面PostGreSql返回主键基础的insert方法
     * @param element
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        XmlElement selectKey = addSelectKey(introspectedTable);
        element.addElement(0, selectKey);
        return super.sqlMapInsertElementGenerated(element, introspectedTable);
    }

    /**
     * 新增SqlMapper.xml里面PostGreSql返回主键基础的insertSelective方法
     * @param element
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        XmlElement selectKey = addSelectKey(introspectedTable);
        element.addElement(0, selectKey);
        return super.sqlMapInsertSelectiveElementGenerated(element, introspectedTable);
    }


    /**
     * 返回主键方法   ==> 一般sql如下面
     * <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
     *       SELECT currval('t_hui_order_id_seq')
     * </selectKey>
     * @param introspectedTable
     * @return
     */
    private XmlElement addSelectKey(IntrospectedTable introspectedTable){
        String resultType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().getFullyQualifiedName();
        String keyProperty = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();
        String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
        String sql = "SELECT " + tableName + "_SEQUENCE.nextval from dual')";

        XmlElement selectKey = new XmlElement("selectKey");
        TextElement selectKeySQL = new TextElement(sql);
        selectKey.addAttribute(new Attribute("keyProperty",keyProperty));
        selectKey.addAttribute(new Attribute("order","BEFORE"));
        selectKey.addAttribute(new Attribute("resultType", resultType));
        selectKey.addElement(selectKeySQL);
        return selectKey;
    }
}

使用方法

generatorConfig.xml

<!--MySql批量更新-->
<plugin type="com.hui.mybatis.plugins.OracleReturnKeyPlugin"/>

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis.generator}</version>
                <configuration>
                    <!-- 配置文件 -->
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <!-- 允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!-- mybatis-generator-core -->
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>${mybatis.generator}</version>
                    </dependency>

                    <!--插件导入-->
                    <dependency>
                        <groupId>com.hui.mybatis.plugins</groupId>
                        <artifactId>hui-mybatis-plugins</artifactId>
                        <version>0.0.1-SNAPSHOT</version>
                    </dependency>

                    <!--Mysql-->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.44</version>
                    </dependency>

                    <!--ORACLE-->
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.1.0.7.0</version>
                    </dependency>

                    <!--PG SQL-->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.version}</version>
                    </dependency>

                </dependencies>
            </plugin>
        </plugins>
    </build>

Github

https://github.com/ithuhui/hui-mybatis-plugins/tree/master/src/main/java/com/hui/mybatis/plugins

作者

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢

猜你喜欢

转载自blog.csdn.net/HuHui_/article/details/85150631
今日推荐