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

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

一、要求:

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

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

二、源代码:

1、User.java

 1 package cn.com.zfc.model;
 2 
 3 public class User {
 4 
 5     private Integer id;
 6     private String name;
 7     private String password;
 8 
 9     public Integer getId() {
10         return id;
11     }
12 
13     public void setId(Integer id) {
14         this.id = id;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public String getPassword() {
26         return password;
27     }
28 
29     public void setPassword(String password) {
30         this.password = password;
31     }
32 
33     @Override
34     public String toString() {
35         return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
36     }
37 
38 }

2、UserMapper.xml

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

3、mybatis-config.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <environments default="development">
 7         <environment id="development">
 8             <transactionManager type="JDBC" />
 9             <!-- 配置数据源 -->
10             <dataSource type="POOLED">
11                 <property name="driver" value="com.mysql.jdbc.Driver" />
12                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis_07" />
13                 <property name="username" value="root" />
14                 <property name="password" value="root" />
15             </dataSource>
16         </environment>
17     </environments>
18     <!-- 注册映射文件 -->
19     <mappers>
20         <!-- 注册映射文件,xml:resource -->
21         <mapper resource="cn/com/zfc/model/UserMapper.xml" />
22     </mappers>
23 </configuration>

4、MyBatisUtil.java

 1 package cn.com.zfc.util;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 
10 public class MyBatisUtil {
11 
12     private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder = null;
13     private static SqlSessionFactory sessionFactory = null;
14     private static InputStream inputStream = null;
15 
16     static {
17         try {
18             // 加载mybatis主配置文件
19             inputStream = Resources.getResourceAsStream("mybatis-config.xml");
20             sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
21             // 获取SqlSessionFactory对象
22             sessionFactory = sqlSessionFactoryBuilder.build(inputStream);
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26     }
27 
28     /**
29      * 获取SqlSessionFactory对象
30      * 
31      * @return
32      */
33     public static SqlSessionFactory getSqlSessionFactory() {
34         return sessionFactory;
35     }
36 }

5、MyBatisTest.java

 1 package cn.com.zfc.test;
 2 
 3 import org.apache.ibatis.session.SqlSession;
 4 import org.apache.ibatis.session.SqlSessionFactory;
 5 
 6 import cn.com.zfc.model.User;
 7 import cn.com.zfc.util.MyBatisUtil;
 8 
 9 /**
10  * 获取刚插入数据库表中记录的自增长主键
11  * 
12  * @title MyBatisTest
13  * @describe
14  * @author 张富昌
15  * @date 2017年8月22日下午10:56:58
16  */
17 public class MyBatisTest {
18     public static void main(String[] args) {
19 
20         // 1、获取SqlSessionFactory
21         SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
22         // 2、获取SqlSession
23         SqlSession sqlSession = sqlSessionFactory.openSession();
24         String statement = "cn.com.zfc.model.UserMapper.insert";
25         User user = new User();
26         user.setName("张富昌2");
27         user.setPassword("123456");
28         // 执行添加操作之后返回的结果
29         int result = sqlSession.insert(statement, user);
30         // 提交
31         sqlSession.commit();
32         // 关闭
33         sqlSession.close();
34         System.out.println("result:" + result);
35         // 获取刚刚添加进去的自增长主键id
36         System.out.println("id:" + user.getId());
37     }
38 }

 三、运行效果

 

posted @ 2017-08-22 23:02  勇闯天涯zfc  阅读(440)  评论(0编辑  收藏  举报