(转)Mybatis insert后返回主键给实体对象(Mysql数据库)
<insert id="insert" parameterType="com.zqgame.game.website.models.Team">
<selectKey resultType="java.lang.Integer" keyProperty="teamId" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO kz_team
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="teamId != null">
`team_id`,
</if>
<if test="teamName != null">
`team_name`,
</if>
<if test="regionId != null">
`region_id`,
</if>
<if test="provinceId != null">
`province_id`,
</if>
<if test="cityId != null">
`city_id`,
</if>
<if test="address != null">
`address`,
</if>
<if test="idImg != null">
`id_img`,
</if>
<if test="teamLogo != null">
`team_logo`,
</if>
<if test="introduce != null">
`introduce`,
</if>
<if test="accountId != null">
`account_id`,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="teamId != null">
#{teamId,jdbcType=INTEGER},
</if>
<if test="teamName != null">
#{teamName,jdbcType=VARCHAR},
</if>
<if test="regionId != null">
#{regionId,jdbcType=INTEGER},
</if>
<if test="provinceId != null">
#{provinceId,jdbcType=INTEGER},
</if>
<if test="cityId != null">
#{cityId,jdbcType=INTEGER},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
<if test="idImg != null">
#{idImg,jdbcType=VARCHAR},
</if>
<if test="teamLogo != null">
#{teamLogo,jdbcType=VARCHAR},
</if>
<if test="introduce != null">
#{introduce,jdbcType=VARCHAR},
</if>
<if test="accountId != null">
#{accountId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<selectKey resultType="Java.lang.Integer" keyProperty="teamId" order="AFTER"> //teamId实体类主键
SELECT LAST_INSERT_ID()
</selectKey>
或者
给<insert id="xx" useGeneratedKeys="true" keyProperty="teamId"> 加入2个属性就可以省略上面那句<selectKey>xxxxxx</selectKey>
在mybatis中标红的那句话 能将插入的主键返回给实体对象
if (StringUtils.isEmpty(team.getTeamLogo())) {
team.setTeamLogo(defaultTeamLogo);
}
team.setAccountId(accountId);
insert(team);
System.out.println(team.getTeamId());
对于业务中需要取得插入后的主键id值得童鞋来说很方便
不过那个函数貌似是MySQL提供的 需要其他数据库的另外寻找方法
(转)Mybatis insert后返回主键给实体对象(Mysql数据库)的更多相关文章
- mybatis insert后返回主键ID
需求: mybatis 在添加记录时需要获取到记录主键id id=0 无法获取主键id的值 在插入方法中添加如下属性和相应的值 <insert useGeneratedKeys="t ...
- MyBatis返回主键,MyBatis Insert操作返回主键
MyBatis返回主键,MyBatis Insert操作返回主键 >>>>>>>>>>>>>>>>> ...
- Mybatis之MySql批量insert后返回主键
需求:使用批量插入后,需要insert之后的每一条记录的ID 注意:Mybatis3.3.1的版本以后支持批量插入后返回主键ID 示例: domin.java: public class User { ...
- PostgreSQL使用MyBatis,insert时返回主键
MyBatis中普通的insert语句是这样的: <insert id="insert" parameterType="com.xxx.xxx.xxDo" ...
- MyBatis insert操作返回主键
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数: Dao.java @Override public int insert(T record) { f ...
- 160613、MyBatis insert操作返回主键
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能,针对Sequence主键而言,在执行 ...
- mybatis insert oracle 返回主键
mybtis返回oracle主键 只需要加一点代码(红色处的代码)就可以了 <!-- 添加记录到临时表 --> <insert id="insertPlaneStateme ...
- mybatis insert 如何返回主键
在使用ibatis插入数据进数据库的时候,会用到一些sequence的数据,有些情况下,在插入完成之后还需要将sequence的值返回,然后才能进行下一步的操作. 使用ibatis的sel ...
- MyBatis自动获取主键,MyBatis使用Oracle返回主键,Oracle获取主键
MyBatis自动获取主键,MyBatis使用Oracle返回主键,Oracle获取主键 >>>>>>>>>>>>>> ...
随机推荐
- 【大数据系列】FileSystem Shell官方文档翻译
appendToFile cat checksum chgrp chmod chown copyFromLocal copyToLocal count cp createSnapshot delete ...
- 【大数据系列】hadoop单节点安装官方文档翻译
Hadoop: Setting up a Single Node Cluster. HADOOP:建立单节点集群 Purpose Prerequisites Supported Platforms R ...
- OAuth网络协议(转)
一.应用场景 为了理解OAuth的适用场合,让我举一个假设的例子. 有一个"云冲印"的网站,可以将用户储存在Google的照片,冲印出来.用户为了使用该服务,必须让"云冲 ...
- Git 创建两个“本地分支”协同工作
一 代码拉下来后,首先创建两个本地分之 $repo start master . //仅仅用于同步服务器的修改(此处master名字可以随意定,但是建议定成这样,好记忆) $repo start wo ...
- 题目1008:最短路径问题(最短路径问题dijkstra算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1008 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- sencha touch tpl 实现按钮功能
js如下: Ext.define('app.view.message.Info', { alternateClassName: 'messageInfo', extend: 'Ext.Containe ...
- [转]13 Hours: The Secret Soldiers of Benghazi
转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...
- locate命令的使用
使用locate命令,遇到了这样的情况:当前目录下有一个文件,而使用这个命令时却查找不到这个文件,上网查了一下,找到了原因,就在下面. 1. find find是最常见和最强大的查找命令,你可以用它找 ...
- dhroid - NetJSONAdapter 网络化的adapter
关于adapter 我想对于大家来说已经不陌生了,基本应用都会用的很多,不知道现在你是不是还是按一定的套路写很多代码去实现adapter我想大多数人还是写个adapter继承自baseadapter ...
- MONGOOSE – 让NODE.JS高效操作MONGODB(转载)
Mongoose库简而言之就是在node环境中操作MongoDB数据库的一种便捷的封装,一种对象模型工具,类似ORM,Mongoose将数据库中的数据转换为JavaScript对象以供你在应用中使用. ...