JAVA全系列 教程
3762个小节阅读:7089k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
持久层接口方法
xxxxxxxxxx
int findCount();
映射文件
xxxxxxxxxx
<select id="findCount" resultType="int">
select count(id) from user
</select>
测试类
xxxxxxxxxx
@Test
public void testFindCount(){
System.out.println(userMapper.findCount());
}
有时我们需要获取新插入数据的主键值。如果数据库中主键是自增的,这时我们就需要使用MyBatis的主键回填功能。
持久层接口方法
xxxxxxxxxx
void add(User user);
映射文件
xxxxxxxxxx
<insert id="add" parameterType="com.itbaizhan.user.User">
<!-- keyProperty:主键属性名,keyColumn:主键列名,resultType:主键类型,order:执行时机 -->
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID();
</selectKey>
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
</insert>
SELECT LAST_INSERT_ID():查询刚刚插入的记录的主键值,只适用于自增主键,且必须和insert语句一起执行。
测试类
xxxxxxxxxx
@Test
public void testAdd(){
User user = new User("尚学堂", new Date(), "男", "北京");
userMapper.add(user);
session.commit();
System.out.println(user.getId());
}
1. 在MyBatis主键回填中,<selectKey>
中表示主键列名的属性为
A keyColumn
B keyProperty
C resultType
D order
2. 关于SELECT LAST_INSERT_ID()
,以下说法错误的是:
A 查询刚刚插入的记录的主键值
B 查询第一条插入的记录的主键值
C 只适用于查询自增主键
D 必须和insert语句一起执行
1=>A 2=>B