mybatis insert 插入数据成功后返回值为 null
2018-03-18 19:30
309 查看
第一种方法:
<!-- ***************重点掌握插入语句返回结果的做法**************** -->
<insert id="insertUser" parameterType="cn.com.gjw.pojo.User">
<!-- 将插入数据的主键返回,由SELECT LAST_INSERT_ID()得到,只适用于自增主键
keyProperty:指定将查询到的结果返回到parameterType所指的对象的对应属性中
order:指的是执行顺序,即为AFTER时,则在insert语句执行之后执行。
-->
<selectKey keyProperty="id" order="AFTER" resultType="int">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(userName, sex, address, birthday) values(#{userName}, #{sex}, #{address}, #{birthday})
</insert>
第二种方法:
<insert id="insertUser" parameterType="cn.com.gjw.pojo.User" useGeneratedKeys="true" keyProperty="id">
insert into user(userName, sex, address, birthday) values(#{userName}, #{sex}, #{address}, #{birthday})
</insert>
第三种方法:如果id不是自增的,当表中没有记录式,设置id值为1,否则取id的最大值加2作为新的主键,设置方式为:
<insert id="insertUser" parameterType="cn.com.gjw.pojo.User" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="id" order="BEFORE" resultType="int">
select if(max(id) is null, 1, max(id) + 2) as newId from user
</selectKey>
insert into user(userName, sex, address, birthday) values(#{userName}, #{sex}, #{address}, #{birthday})
</insert>
代码中,执行完插入操作后可以打印测试是否取到了刚插入的记录的id:
System.out.println(user.getId());
版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/qq_25816185/article/details/58071982
<!-- ***************重点掌握插入语句返回结果的做法**************** -->
<insert id="insertUser" parameterType="cn.com.gjw.pojo.User">
<!-- 将插入数据的主键返回,由SELECT LAST_INSERT_ID()得到,只适用于自增主键
keyProperty:指定将查询到的结果返回到parameterType所指的对象的对应属性中
order:指的是执行顺序,即为AFTER时,则在insert语句执行之后执行。
-->
<selectKey keyProperty="id" order="AFTER" resultType="int">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(userName, sex, address, birthday) values(#{userName}, #{sex}, #{address}, #{birthday})
</insert>
第二种方法:
<insert id="insertUser" parameterType="cn.com.gjw.pojo.User" useGeneratedKeys="true" keyProperty="id">
insert into user(userName, sex, address, birthday) values(#{userName}, #{sex}, #{address}, #{birthday})
</insert>
第三种方法:如果id不是自增的,当表中没有记录式,设置id值为1,否则取id的最大值加2作为新的主键,设置方式为:
<insert id="insertUser" parameterType="cn.com.gjw.pojo.User" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="id" order="BEFORE" resultType="int">
select if(max(id) is null, 1, max(id) + 2) as newId from user
</selectKey>
insert into user(userName, sex, address, birthday) values(#{userName}, #{sex}, #{address}, #{birthday})
</insert>
代码中,执行完插入操作后可以打印测试是否取到了刚插入的记录的id:
System.out.println(user.getId());
版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/qq_25816185/article/details/58071982
相关文章推荐
- mybatis insert 插入数据成功后返回值为 null
- ADO.net 更新和插入数据 遇到null 执行不成功
- 关于ibatis insert 返回值为 null的研究----得到插入数据的逐渐值
- hive导入数据成功,但是查询显示NULL
- Python 的 MySQLdb 模块插入数据没有成功与 autocommit(自动提交)的关系
- oracle11g数据恢复提示不能插入NULL至非空字段
- Python 的 MySQLdb 模块插入数据没有成功与 autocommit(自动提交)的关系
- mysql 查找没有插入数据的项不能使用 ***=Null 得用is null
- oracle11g数据恢复提示不能插入NULL至非空字段
- php监测数据是否成功插入到Mysql数据库的方法
- 使用JMeter往Mysql中插入数据,数据时成功插入了,但是JDBC请求为什么显示的是报错状态
- sql 语句 插入数据 返回值问题
- C++ map插入(insert)数据返回值
- 程序返回插入数据库成功,但是数据库内却没有数据
- MySql数据库新建表后,插入数据不成功的解决办法
- Text数据类型,参数化查询但未提供该参数(将null插入数据库)
- mysql 控制台环境下查询中文数据乱码,插入、更新中文数据不成功
- ibatis用oracle插入 更新数据,返回sequence号判定是否成功
- MyBatis插入数据返回值为负数-2147482646
- 程序返回插入数据库成功,但是数据库内却没有数据