Mybatis Generator insert useGeneratedKeys keyProperty
Mybatis自动生成代码,需要用到mybatis Generator,详见http://mybatis.github.io/generator/configreference/generatedKey.html
insert语句如果要返回自动生成的key值,一般会在insert里加入useGeneratedKeys属性,例如
<insert id="insert" parameterType="cn.ac.iscas.pebble.ufe.bean.Subtask"
useGeneratedKeys="true" keyProperty="sid">
insert into subtasks (SID, TID, RID,
START, INTERVALL, SCHEDULE,
RESULTS, STYLE)
values (#{sid,jdbcType=INTEGER}, #{tid,jdbcType=INTEGER}, #{rid,jdbcType=INTEGER},
#{start,jdbcType=TIMESTAMP}, #{interval,jdbcType=INTEGER}, #{schedule,jdbcType=INTEGER},
#{results,jdbcType=VARCHAR}, #{style,jdbcType=INTEGER})
</insert>
如果要让generator自动添加该功能,可以如下配置:
<table schema="cc" tableName="test" domainObjectName="TT"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<generatedKey column="taskid" sqlStatement="SELECT LAST_INSERT_ID()"/>
</table>
则通过generator自动生成的Mapper文件里的insert语句则类似如下:
<insert id="insert" parameterType="cn.ac.iscas.pebble.ufe.bean.TT" >
<selectKey resultType="java.lang.Integer" keyProperty="taskid" order="BEFORE" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into test (TASKID)
values (#{taskid,jdbcType=INTEGER})
</insert>
注意:最近才发现一个问题,通过这样generator出的id,如果在插入时已经写了id,则会出错,把已经自己设定的id,更新成上次插入的id。所以这种方法不适合于id不是自动生成而是手动添加的情况!!!
其中generateKey的sqlStatement属性见下表:
| Attribute | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| column | The column name of the generated column. | ||||||||||||||||||||
| sqlStatement | The SQL statement that will return the new value. If this is an identity column, then you can use one of the predefined special values, or substitute the proper statement for your database. The predefined special values are as follows:
|
关于mybatis的mapper文件的xml学习可以参考:http://my.oschina.net/zplswf/blog/63981
其中关于useGeneratedKeys和keyProperty参考下表:
| 属性 | 描述 |
|---|---|
| id | 在命名空间中唯一的标识符,可以被用来引用这条语句。 |
| parameterType | 将会传入这条语句的参数类的完全限定名或别名。 |
| parameterMap | 这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。 |
| flushCache | 将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被清空。默认值:false。 |
| timeout | 这个设置驱动程序等待数据库返回请求结果, 并抛出异常时间的最大等待值。默认不设置(驱动自行处理)。 |
| statementType | STA TEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBatis 使用选择使用 Statement,PreparedStatement 或 CallableStatement。默认值:PREPARED。 |
| useGeneratedKeys | ( 仅 对 insert 有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。 |
| keyProperty | (仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。 |
| keyColumn | (仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。 |
Mybatis Generator insert useGeneratedKeys keyProperty的更多相关文章
- Mybatis Generator自动生成的mapper只有insert方法
– Mybatis Generator 生成的mapper只有insert方法 – 首先检查generatorConfig.xml中table项中的属性 enableSelectByPrimaryKe ...
- mybatis generator自动生成代码时 只生成了insert 而没有其他的
mybatis框架提供了非常好用的逆向工程插件,但是在使用过程中会有很多问题. 我在使用中就遇到了只生成insert和insertSeletive方法,而不生成其他根据primary key查询更新删 ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- mybatis generator使用总结
一.mybatis项目的体系结构 百度mybaits,可以进入mybatis的github:https://github.com/mybatis. mybatis是一个大大的体系,它不是孤立的,它可以 ...
- mybatis generator配置文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...
- 【转】Mybatis Generator最完整配置详解
本文转简书:http://www.jianshu.com/p/e09d2370b796 --> --> <!-- 自动识别数据库关键字,默认false,如果设置为true,根据Sql ...
- Mybatis Generator最完整配置详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- idea + mybatis generator + maven 插件使用
idea + mybatis generator + maven 插件使用 采用的是 generator 的 maven 插件的方式 ~ 1 pom.xml mybatis其它配置一样,下面是配置my ...
- Java逆向工程SpringBoot + Mybatis Generator + MySQL
Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...
随机推荐
- MyEclipse破解(MEGen.java)
步骤: 1.将MEGen.java粘贴到任意web项目下,运行结果如下: 2.输入注册名:如sun,得到注册码: 3.Window >> Preference >> S ...
- logstash json和rubydebug 第次重启logstash都会把所有的日志读完 而不是只读入新输入的内容
查看一下agent端的shipper的配置: # cat logstash_test2.shipper.conf input { file { path => ["/apps/logs ...
- openfire 服务器名称:后面的黄色叹号
然后点击重新获取证书,然后重新启动服务,问题解决!
- jquery学习笔记----ajax使用
一.load() 加载页面数据 load(url,[data],[callback]) url:加载的页面地址,[data]传送的数据,[callback]加载完成时回调函数. 设计一个load.ht ...
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)
1.IP简述使用//获取本地主机ip地址对象.InetAddress ip = InetAddress.getLocalHost();//获取其他主机的ip地址对象.ip = InetAddress. ...
- Eclipse中怎么设置Add cast to Clazz 快捷键
方法如下:window => preferences => 搜索keys => 然后点击进去,搜索add cast => 看到如图所示Quick Fix , 点击进去 => ...
- HDU5115 Dire Wolf(区间DP)
渐渐认识到区域赛更侧重的是思维及基本算法的灵活运用,而不是算法的量(仅个人见解),接下来要更多侧重思维训练了. 区间DP,dp[i][j]表示从i到j最终剩余第i 与第j只的最小伤害值,设置0与n+1 ...
- golang heap container balance request
package mainimport ( "container/heap" "fmt" "log" "math/rand" ...
- phpcms-v9系统搭建wap网站及单页面
如需要绑定域名为wap.domain.com,作下如操作: 一.把wap.domain.com域名绑定到你的这个网站主机上. 二.在网站后台——模块——手机门户域名里面填写“http://wap.do ...