版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85150712
文章目录
系列
- 【Mybatis】Mybatis-Generator-Postgresql返回主键插件
- 【Mybatis】Mybatis-Generator-Oracle返回主键插件
- 【Mybatis】Mybatis-Generator-batchDelete(批量删除)方法插件
- 【Mybatis】Mybatis-Generator-Postgresql&Mysql(批量新增)方法插件
- 【Mybatis】Mybatis-Generator-Oracle(批量新增)方法插件
- 【Mybatis】Mybatis-Generator-Mysql(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Postgresql(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类
前言
没什么好说的。PG真的用的很不习惯。。。
Mybatis也不支持返回主键
受不了。。。 写个主键返回插件吧,反正也简单
github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。
装备
-
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>
-
postgre建表时候主键使用了 SERIAL 的序列自增方式
Core-Code
sql
SELECT currval('tablename_id_seq')
mybatis-implement
<selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER">
SELECT currval('tablename_id_seq')
</selectKey>
plugin-code
/**
* <b><code>PostGreSQLReturnKeyPlugin</code></b>
* <p/>
* Description: PG返回主键插件(万恶的PG。。。)
* <p/>
* <b>Creation Time:</b> 2018/12/7 0:00.
*
* @author HuWeihui
*/
public class PostGreSQLReturnKeyPlugin 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 currval('" + tableName + "_id_seq')";
XmlElement selectKey = new XmlElement("selectKey");
TextElement selectKeySQL = new TextElement(sql);
selectKey.addAttribute(new Attribute("keyProperty",keyProperty));
selectKey.addAttribute(new Attribute("order","AFTER"));
selectKey.addAttribute(new Attribute("resultType", resultType));
selectKey.addElement(selectKeySQL);
return selectKey;
}
}
使用方法
generatorConfig.xml
<!--返回postgresql主键-->
<plugin type="com.hui.mybatis.plugins.PostGreSQLReturnKeyPlugin"/>
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和大数据问题,转载请注明作者和原文链接,感谢