2
【笔记】MyBatis插入语句返回主键
source link: https://feiju12138.github.io/2022/09/28/MyBatis%E6%8F%92%E5%85%A5%E8%AF%AD%E5%8F%A5%E8%BF%94%E5%9B%9E%E4%B8%BB%E9%94%AE/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
MyBatis利用SQL语句插入数据后返回主键
插入数据后返回主键
创建实体类
src/main/java/com/pojo/User.java
class User {
Integer id;
String name;
}
xml中添加SQL语句
useGeneratedKeys="true"
:启用主键返回keyProperty=""
:配置主键
src/main/resources/com/mybatis.xml
<insert useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (id, name)
VALUES (null, #{name});
</insert>
java中执行sql语句
src/main/java/com/Main.java
# 封装对象
User user = new User();
user.name = "";
# 执行sql语句
...
# 获取返回的主键
int id = user.id;
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK