Spring Boot 整合 MyBatis有多種方式,本文使用的是starter
的方式,還可使用註解+bean配置
的方式等。此外本文使用的是xml配置SQL而不是用註解。主要是 SQL 和業務代碼應該隔離,方便和 DBA 校對 SQL,此外 XML 對較長的 SQL 比較清晰。java
使用mySQL數據庫,建立一張book
表:mysql
CREATE TABLE `book` ( `id` INT(6) NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NULL DEFAULT NULL, `author` VARCHAR(100) NULL DEFAULT NULL, `price` DOUBLE NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB AUTO_INCREMENT=3 ;
項目結構以下:git
pom.xml
文件中添加 Spring Boot 的 mybatis starter 依賴,因爲要使用 mysql 數據庫,還須要增長 mysql 的依賴<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
entity實體類(使用MyBatis逆向工程生成):github
package com.ddcx.springboot.demodatabase.entity; public class Book { private Integer id; private String name; private String author; private Double price; //get、set方法省略 }
dao:web
package com.ddcx.springboot.demodatabase.mapper; import com.ddcx.springboot.demodatabase.entity.Book; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookMapper { int insert(Book record); List<Book> selectAll(); Book getById(@Param(value = "id") Integer id); }
service:spring
package com.ddcx.springboot.demodatabase.service; import com.ddcx.springboot.demodatabase.entity.Book; import com.ddcx.springboot.demodatabase.mapper.BookMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by liaosi on 2017/9/26. */ @Service public class BookService { @Autowired private BookMapper bookMapper; public List<Book> getAllBooks() { return bookMapper.selectAll(); } public Book getById(Integer id) { return bookMapper.getById(id); } }
controller:sql
package com.ddcx.springboot.demodatabase.controller; import com.ddcx.springboot.demodatabase.service.BookService; import com.google.gson.Gson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by liaosi on 2017/9/26. */ @RestController public class DemoController { private static Gson gson = new Gson(); @Autowired private BookService bookService; @RequestMapping("/getBook/{id}") String bookInfo(@PathVariable("id") Integer id) { return gson.toJson(bookService.getById(id)); } }
<?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.ddcx.springboot.demodatabase.mapper.BookMapper"> <resultMap id="BaseResultMap" type="Book"> <result column="id" jdbcType="INTEGER" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="author" jdbcType="VARCHAR" property="author" /> <result column="price" jdbcType="DOUBLE" property="price" /> </resultMap> <insert id="insert" parameterType="Book"> insert into book (id, name, author, price) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{price,jdbcType=DOUBLE}) </insert> <select id="selectAll" resultMap="BaseResultMap"> select id, name, author, price from book </select> <select id="getById" resultMap="BaseResultMap"> select id, name, author, price from book WHERE id = #{id} </select> </mapper>
爲何要將SQL映射的xml文件的文件放在 resource 下?
由於在這個示例中使用的開發工具是IDEA,IDEA不會編譯src的java目錄下的xml文件,具體可參考:IDEA不編譯src的java目錄下的xml文件問題及解決數據庫
package com.ddcx.springboot.demodatabase; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //mapper 接口類包掃描 @MapperScan(basePackages = "com.ddcx.springboot.demodatabase.mapper") public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
@MapperScan註解的做用相似於 MapperScannerConfigurer ,能夠掃描該包下 MyBatis 的 mapper.java 接口類註冊到 IOC 容器中。
若是不使用@MapperScan註解,還能夠在每一個 mapper 接口類上加上 @Mapper 這個註解,可是這樣作比較麻煩,若是全部的mapper接口類都在一個包下,仍是使用@MapperScan註解更爲方便。apache
#SpringBoot整合MyBatis框架 #1.加載MyBatis配置文件 mybatis.mapper-locations=classpath:mapper/*.xml //掃描classpath下mapper目錄下的全部.xml文件 mybatis.type-aliases-package=com.ddcx.springboot.demodatabase.entity //實體類的包路徑 #2.數據庫配置 spring.datasource.url=jdbc:mysql://192.168.0.1:3306/java_test spring.datasource.username=root spring.datasource.password=******** spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations :用來指定sql映射的xml文件的位置。
mybatis.type-aliases-package :用來指定實體類的包路徑。有了這個配置,在sql映射文件中,將數據庫中的數據映射成某個類的對象時,不用設置該類的全類名了。例如原本在xml文件中須要這樣配置的segmentfault
若是配置了mybatis.type-aliases-package=com.ddcx.springboot.demodatabase.entity,則能夠寫成這樣:
能夠看出這個配置的做用有點相似於MyBatis配置文件中的別名處理器(typeAliases標籤)中的package配置。
還有其餘的一些MyBatis的配置也均可以配置到application.properties文件裏,此處再也不詳細研究。
啓動Spring Boot應用,在postman或瀏覽器中訪問便可。
本節示例代碼已上傳到github: https://github.com/liaosilzu2...