在以往Oracle数据库的操作中,假如我们插入一本图书,想立即获取该书的ID,需要先执行序列,再将序列生成的ID作为返回值返回,而使用了Mybatis的Mysql则不同,用它获取新插入的记录ID非常方便,我们准备一下接下来将要使用到的数据。
和《MyBatis:简单使用》 一文的所做的操作一样,唯一的不同是需要为Book的实体类添加三个构造方法:
package io.zhangjia.entity; public class Book { /*建议严格遵守驼峰命名法*/ private Integer bookId; private String name; private String author; private Double price; public Book() { } public Book(String name, String author, Double price) { this.name = name; this.author = author; this.price = price; } public Book(Integer bookId, String name, String author, Double price) { this.bookId = bookId; this.name = name; this.author = author; this.price = price; } public Integer getBookId() { return bookId; } public void setBookId(Integer bookId) { this.bookId = bookId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Book{" + "bookId=" + bookId + ", name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}'; } }
接下来,将BookMapper.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="io.zhangjia.mapper.BookMapper"> <insert id="doInsert" parameterType="book"> INSERT INTO BOOK (NAME, AUTHOR, PRICE) VALUE (#{name}, #{author}, #{price}) </insert> </mapper>
然后配置mybatis-config.xml,配置内容可以参照一《Mybatis:增删改查》一文中的第四节。
最后,编写对应的测试类即可:
package io.zhangjia.util; import io.zhangjia.mapper.BookMapper; import io.zhangjia.entity.Book; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class Main { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = build.openSession(true); BookMapper mapper = sqlSession.getMapper(BookMapper.class); Book book = new Book(); book.setAuthor("张甲"); book.setName("无名"); book.setPrice(250.0); System.out.println("book = " + book); int i = mapper.doInsert(book); System.out.println("book = " + book); sqlSession.close(); inputStream.close(); } } 输出: book = Book{bookId=null, name='无名', author='张甲', price=250.0} book = Book{bookId=null, name='无名', author='张甲', price=250.0}
可以看到,按照之前的操作方式,book对象在插入前和插入后的ID都是null,并没有获取到我们插入的book对象的ID,那么我们应该如何获取呢?其实获取的方式也非常简单,只需要在将BookMapper.xml的insert中添加以下内容即可:
-
useGeneratedKeys:设置为true,代表需要去使用自动生成的主键
-
keyProperty:将自动生成的主键保存在入参的哪个属性中
<insert id="doInsert" parameterType="book" useGeneratedKeys="true" keyProperty="bookId"> INSERT INTO BOOK (NAME, AUTHOR, PRICE) VALUE (#{name}, #{author}, #{price}) </insert>
此时再次执行测试类,输出:
book = Book{bookId=null, name='无名', author='张甲', price=250.0} book = Book{bookId=28, name='无名', author='张甲', price=250.0}
请登录之后再进行评论