從“資料”到“大資料”,激發資料潛力,深耕智慧應用!
阿新 • • 發佈:2022-05-22
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