(转)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获取主键 >>>>>>>>>>>>>> ...
随机推荐
- 【Mybatis】Mybatis基本构成
SqlSessionFactoryBuilder(构造器):它会根据配置信息或者代码来生成SqlSessionFactory(工厂接口) SqlSessionFactory:依靠工厂来生成SqlSes ...
- 【linux系列】Centos下安装mysql数据库
前言 为了测试方便,通常我们会自己安装数据库,以下是在Centos上安装Mysql的操作. 一.检查自己是否安装了MySQL数据库 [root@s201 /home/mysql]#rpm -qa |g ...
- css方法 - 移动端h5在iphonex的适配
@media only screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-rati ...
- 百度编辑器(UEditor)自定义工具栏
百度编辑器(UEditor)自定义工具栏的自定义 百度编辑器默认功能比较齐全,但是不一定是我们所需要的,有的功能可以去掉,用自己想要的就可以了,可以参考百度官方文档! 百度编辑器默认配置展示界面 如何 ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- Centos 安装yum,安装ansible
今天使用centos安装ansible,发现域名默认安装是未注册的.提示: This system is not registered to Red Hat Subscription Manageme ...
- 【转】python中json.loads与eval的区别
JSON有两种结构: “名称/值”对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),纪录(record),结构(struct) ...
- 微信小程序---示例DEMO
转:CSDN的文章: https://blog.csdn.net/rolan1993/article/details/73467867 不错的DEMO: https://github.com/skyv ...
- 转Python SciPy库——拟合与插值
1.最小二乘拟合 实例1 import numpy as np import matplotlib.pyplot as plt from scipy.optimize import leastsq p ...
- java虚拟机的符号引用和直接引用
在java中,一个java类将会编译成一个class文件.在编译时,java类并不知道引用类的实际内存地址,因此只能使用符号引用来代替.比如org.simple.People类引用org.simple ...