(转)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获取主键 >>>>>>>>>>>>>> ...
随机推荐
- 【python问题】UnicodeEncodeError: 'ascii' codec can't encode characters in position 306-309: ordinal not in range(128)
今天在写python爬虫的时候,遇到一个问题 UnicodeEncodeError: 'ascii' codec can't encode characters in position 306-309 ...
- Find–atime –ctime –mtime的用法与区别总结
转自 周五有同事问起find命令中-mtime n.-mtime –n以及-mtime +n的用法区别,当时虽然记得这里n是n个24个小时的意思,也是对所有这几个属性详细的用法却一知半解,索性周末仔细 ...
- python之SQLAlchemy ORM
前言: 这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.有兴趣可看下python之数据库(mysql)操作.下篇博客整理写篇关于Web框架和django基础~~ 一.OR ...
- ubuntu apt 代理设置
可以通过三种方法为apt-get设置http代理方法一这是一种临时的手段,如果您仅仅是暂时需要通过http代理使用apt-get,您可以使用这种方式.在使用apt-get之前,在终端中输入以下命令(根 ...
- 浏览器 User Agent字符串列表
http://www.73207.com/useragent/ http://www.73207.com/useragent/pages/internet-2520explorer/index.htm ...
- sencha touch list tpl 监听组件插件(2013-9-15)
插件代码 /* *list tpl模版加入按钮监控 *<div class="x-button-normal x-button x-iconalign-center x-layout- ...
- VUE单独页面body css设置
使用created周期用JS来处理BODY的样式 export default { beforeCreate: function () { document.getElementsByTagName( ...
- jenkins之另辟蹊径实现根据svn项目实现智能选择
项目要求,根据svn选择的trunk或branches及tags里的各分支,动态选择参数.一开始认为很简单,直接用jenkins中的List Subversion tags插件及active choi ...
- python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...
- python selenium操作表格式元素实例
很多时候,网页上的布局都是表格形式的,如出下面这样的 这种网页类型在自动化中比较头痛,需要很多判断,下面就举个例子,这里以深圳出入境网页为例,http://yysl.sz3e.com/wsyysq/s ...