MyBatis+MySQL 返回插入的主键ID
需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。
方法:在mapper中指定keyProperty属性,示例如下:
<insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User">
insert into user(userName,password,comment)
values(#{userName},#{password},#{comment})
</insert>
useGeneratedKeys:
取值范围true|false
默认值是:false。
含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。MySQL和SQLServer执行auto-generated key field,因此当数据库设置好自增长主键后,可通过JDBC的getGeneratedKeys方法获取。但像Oralce等不支持auto-generated key field的数据库就不能用这种方法获取主键了。
keyProperty:
(仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。
所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。
public class User {
private int userId;
private String userName;
private String password;
private String comment; //setter and getter
}
public interface UserDao { public int insertAndGetId(User user); }
测试:
User user = new User();
user.setUserName("chenzhou");
user.setPassword("xxxx");
user.setComment("测试插入数据返回主键功能"); System.out.println("插入前主键为:"+user.getUserId());
userDao.insertAndGetId(user);//插入操作
System.out.println("插入后主键为:"+user.getUserId());
输出:
插入前主键为:0
插入后主键为:15
查询数据库:
如上所示,刚刚插入的记录主键id为15
MyBatis+MySQL 返回插入的主键ID的更多相关文章
- (转)MyBatis+MySQL 返回插入的主键ID
MyBatis+MySQL 返回插入的主键ID 需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法:在mapper中指定keyProperty属性,示例如 ...
- mybatis MySQL返回插入的主键ID,oracle不行
<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...
- MyBatis + MySQL返回插入的主键id
这是最近在实现perfect-ssm中的一个功能时碰到的一个小问题,觉得需要记录一下,向MySQL数据库中插入一条记录后,需要获取此条记录的id值,以生成对应的key值存入到redis中,id为自增i ...
- mybatis+mysql返回插入的主键,参数只是提供部分参数
mybatis+mysql返回插入的主键,参数只是提供部分参数 <insert id="insertByChannelIdOpenid" useGeneratedKeys=& ...
- MyBatis返回插入的主键ID(Mysql数据库)
1.Java代码: 1.1 entity类: User.java public class User { private int userId; private String userName; pr ...
- 插入数据返回插入的主键Id
ADO.Net中Sql语句: insert into RoomType(TypeName,Price,AddBed,BedPrice,Remark)output inserted.ID values( ...
- mybatis插入数据后返回自增主键ID详解
1.场景介绍: 开发过程中我们经常性的会用到许多的中间表,用于数据之间的对应和关联.这个时候我们关联最多的就是ID,我们在一张表中插入数据后级联增加到关联表中.我们熟知的mybatis在插入数据后 ...
- Mybatis 返回插入的主键
业务中,会遇到这样的问题,就是感觉返回的主键,插入作为其他表的外键. 那么问题来了,如何去实现,其实方法比较简单,这里就是重点记录下,会出现的误区. 用自动生成sql工具的话,加上下面这句话 < ...
- Mysql EF 触发器生成主键id 存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。刷新 ObjectStateManager 项 ;System.Data.Entity.Infrastructure.DbUpdateConcurrencyException
http://stackoverflow.com/questions/24725261/how-to-use-a-custom-identity-column-in-sql-with-entity-f ...
随机推荐
- “CoreCLR is now Open Source”阅读笔记
英文原文:CoreCLR is now Open Source 阅读笔记如下: CoreCLR是.NET Core的执行引擎,功能包括GC(Garbage Collection), JIT(将CIL代 ...
- TPL实现Task.WhileAll扩展方法
文章翻译整理自 Nikola Malovic 两篇博文: Task.WhileAll Awaitable task progress reporting 当 Task.WhenAll 遇见 Task. ...
- Silverlight动态载入调用XAML资源
以多语言为例子: ResourceDictionary resourceDictionary = new ResourceDictionary(); //新建资源集合 var culture = ...
- [BTS] Error biztalk arguments null exception string reference not set to an instance of a string. parameter name
biztalk arguments null exception string reference not set to an instance of a string. parameter name ...
- 点击div外面该div消失
<head> <meta charset="UTF-8"> <title></title> <script type=&quo ...
- Cacti学习笔记一:基本安装和配置
1.安装依赖包 yum -y install net-snmp-devel mysql mysql-devel openssl-devel libtool 2.安装RRDTool yum -y ins ...
- django with mysql (part-4)
step01: write the ( views.py ) again .. vim views.py step02: configure your (urls.py) step03: check ...
- Javascript设置广告和时间表和数组的学习
<html> <head> <meta charset="utf-8"> <title></title> </he ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- BarEditItem ContentTemplate
<dxb:BarEditItem Name="txtSearch" > <dxb:BarEditItem.ContentTemplate> <Data ...