mybatis如何获取oracle新插入数据记录的主键?
发布网友
发布时间:2022-04-08 19:24
我来回答
共1个回答
热心网友
时间:2022-04-08 20:53
<insert
id="insertAndGetId"
useGeneratedKeys="true"
keyProperty="userId"
parameterType="com.chenzhou.mybatis.User">
insert
into
user(userName,password,comment)
values(#{userName},#{password},#{comment})
</insert>
User
user
=
new
User();
user.setUserName("chenzhou");
user.setPassword("xxxx");
user.setComment("测试插入数据返回主键功能");
System.out.println("插入前主键为:"+user.getUserId());
userDao.insertAndGetId(user);//插入操作
System.out.println("插入后主键为:"+user.getUserId());
User
user
=
new
User();
user.setUserName("chenzhou");
user.setPassword("xxxx");
user.setComment("测试插入数据返回主键功能");
System.out.println("插入前主键为:"+user.getUserId());
userDao.insertAndGetId(user);//插入操作
System.out.println("插入后主键为:"+user.getUserId());
输出:
插入前主键为:0
插入后主键为:15