Python 官方文档:入门教程 => 点击学习
mybatis中insert 方法总是返回一个int值 ,这个值代表的是插入所影响的行数。 如果id采用自增长策略,自动生成的键值在 insert 方法执行完后可以被设置到传入的参数
mybatis中insert 方法总是返回一个int值 ,这个值代表的是插入所影响的行数。 如果id采用自增长策略,自动生成的键值在 insert 方法执行完后可以被设置到传入的参数对象中。那么我们可以在service中通过传入的对象来获得插入的id值。
mapper.xml文件
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.woniuxy.SpringBootmybatis.entity.User" useGeneratedKeys="true">
insert into user
( id,user_name,tel
,passWord,age,create_date
,head_img,dept_id)
values (#{id,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{tel,jdbcType=VARCHAR}
,#{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER},#{createDate,jdbcType=TIMESTAMP}
,#{headImg,jdbcType=VARCHAR},#{deptId,jdbcType=INTEGER})
</insert>
service代码
@Override
public int insertSelective(User record) {
int result = userMapper.insertSelective(record);
log.info("当前行数据的ID为{}",record.getId());
return result;
}
日志文件为:
2023-04-06 15:45:09.813 INFO 15952 --- [NIO-8080-exec-1] c.w.s.service.impl.UserServiceImpl : 当前行数据的ID为107
非自增长的主键
<insert id="add" parameterType="user">
<selecTKEy keyProperty="id" order="BEFORE" resultType="string">
select uuid()
</selectKey>
insert into questions (id,title) values(#{id},#{title})
</insert>
到此这篇关于MyBatis获取自动生成的(主)键值的方法的文章就介绍到这了,更多相关MyBatis获取自动生成键值内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: MyBatis获取自动生成的(主)键值的方法
本文链接: https://www.lsjlt.com/news/208523.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0