MyBatis 插入时返回刚插入记录的主键值

时间:2023-02-24 09:31:42

MyBatis 插入时返回刚插入记录的主键值

一、要求:

1、数据库表中的主键是自增长的,如:id;

2、获取刚刚插入的记录的id值;

MyBatis 插入时返回刚插入记录的主键值

二、源代码:

1、User.java

 package cn.com.zfc.model;

 public class User {

     private Integer id;
private String name;
private String password; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
} }

2、UserMapper.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.zfc.model.UserMapper"> <!-- 插入一条记录开始,返回主键插入记录的id -->
<insert id="insert" parameterType="cn.com.zfc.model.User">
insert into user
(name,password)
values
(#{name},#{password})
<!--
selectKey中order属性的值是不同的,BEFORE先选择主键,设置keyProperty的值然后执行插入语句。
AFTER是先执行插入语句,然后执行selectKey。
-->
<selectKey keyColumn="id" keyProperty="id" order="AFTER"
resultType="Integer">
SELECT LAST_INSERT_ID() AS id
</selectKey>
</insert>
<!-- 插入一条记录结束 -->
</mapper>

3、mybatis-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_07" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 注册映射文件 -->
<mappers>
<!-- 注册映射文件,xml:resource -->
<mapper resource="cn/com/zfc/model/UserMapper.xml" />
</mappers>
</configuration>

4、MyBatisUtil.java

 package cn.com.zfc.util;

 import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder = null;
private static SqlSessionFactory sessionFactory = null;
private static InputStream inputStream = null; static {
try {
// 加载mybatis主配置文件
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 获取SqlSessionFactory对象
sessionFactory = sqlSessionFactoryBuilder.build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 获取SqlSessionFactory对象
*
* @return
*/
public static SqlSessionFactory getSqlSessionFactory() {
return sessionFactory;
}
}

5、MyBatisTest.java

 package cn.com.zfc.test;

 import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import cn.com.zfc.model.User;
import cn.com.zfc.util.MyBatisUtil; /**
* 获取刚插入数据库表中记录的自增长主键
*
* @title MyBatisTest
* @describe
* @author 张富昌
* @date 2017年8月22日下午10:56:58
*/
public class MyBatisTest {
public static void main(String[] args) { // 1、获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
// 2、获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "cn.com.zfc.model.UserMapper.insert";
User user = new User();
user.setName("张富昌2");
user.setPassword("123456");
// 执行添加操作之后返回的结果
int result = sqlSession.insert(statement, user);
// 提交
sqlSession.commit();
// 关闭
sqlSession.close();
System.out.println("result:" + result);
// 获取刚刚添加进去的自增长主键id
System.out.println("id:" + user.getId());
}
}

 三、运行效果

MyBatis 插入时返回刚插入记录的主键值

MyBatis 插入时返回刚插入记录的主键值

MyBatis 插入时返回刚插入记录的主键值

MyBatis 插入时返回刚插入记录的主键值