MyBatis支持定制化SQL、存储过程以及高级映射。MyBatis几乎避免了所有的JDBC代码手动设置参数以及获取结果集。在传统的SSM框架整合中,使用MyBatis需要大量的XML配置,而在Spring Boot中,MyBatis官方提供了一套自动化配置方案,可以做到MyBatis开箱即用。
添加MyBatis依赖、数据库驱动依赖以及数据库连接池依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
application.yml配置:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 mybatis: mapper-locations: - classpath:mapper/*.xml
创建数据库访问层:
@Mapper public interface BookMapper { int addBook(Book book); int deleteBookById(Integer id); int updateBookById(Book book); Book getBookById(Integer id); List<Book> getAllBooks(); }
有两种方式指明该类是一个Mapper:
第一种如前面的代码所示,在BookMapper上添加@Mapper注解,表明该接口是一个MyBatis中的Mapper,这种方式需要在每一个Mapper上都添加注解;
还有一种简单的方式是在配置类上添加@MapperScan("org.sang.mapper")注解,表示扫描org.sang.mapper包下的所有接口作为Mapper,这样就不需要在每个接口上配置@Mapper注解了。
创建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="com.xc.xcspringboot.mapper.BookMapper"> <insert id="addBook" parameterType="com.xc.xcspringboot.model.Book"> INSERT INTO book(name, author) VALUES (#{name}, #{author}) </insert> <delete id="deleteBookById" parameterType="int"> DELETE FROM book WHERE id = #{id} </delete> <update id="updateBookById" parameterType="com.xc.xcspringboot.model.Book"> UPDATE book set name=#{name}, author=#{author} WHERE id = #{id} </update> <select id="getBookById" parameterType="int" resultType="com.xc.xcspringboot.model.Book"> SELECT * FROM book WHERE id = #{id} </select> <select id="getAllBooks" resultType="com.xc.xcspringboot.model.Book"> SELECT * FROM book </select> </mapper>
ookService与BookController,代码如下:
@Service public class BookService { @Autowired BookMapper bookMapper; public int addBook(Book book) { return bookMapper.addBook(book); } public int updateBook(Book book) { return bookMapper.updateBookById(book); } public int deleteBookById(Integer id) { return bookMapper.deleteBookById(id); } public Book getBookById(Integer id) { return bookMapper.getBookById(id); } public List<Book> getAllBooks() { return bookMapper.getAllBooks(); } }
@RestController public class BookController { @Autowired BookService bookService; @GetMapping("/bookOps") public void bookOps() { Book b1 = new Book(); b1.setName("西厢记"); b1.setAuthor("王实甫"); int i = bookService.addBook(b1); System.out.println("addBook>>>" + i); Book b2 = new Book(); b2.setId(1); b2.setName("朝花夕拾"); b2.setAuthor("鲁迅"); int updateBook = bookService.updateBook(b2); System.out.println("updateBook>>>" + updateBook); Book b3 = bookService.getBookById(1); System.out.println("getBookById>>>" + b3); int delete = bookService.deleteBookById(2); System.out.println("deleteBookById>>>" + delete); List<Book> allBooks = bookService.getAllBooks(); System.out.println("getAllBooks>>>" + allBooks); } }
参考来源:Spring Boot+Vue全栈开发实战 5.2 整合MyBatis