首頁 > 軟體

SpringBoot整合mybatis的方法詳解

2022-03-14 10:00:27

1 依賴設定

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>
<dependencies>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.18.22</version>
     </dependency>
	 <dependency>
	     <groupId>org.mybatis.spring.boot</groupId>
	     <artifactId>mybatis-spring-boot-starter</artifactId>
	     <version>1.3.2</version>
	 </dependency>
	 <!--     spring連線驅動時,如com.mysql.cj.jdbc.Driver使用   -->
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

找到mybatis-spring-boot-starter設定的依賴,即autotoconfigure包,SpringBoot的自動設定,會找到META-INF下的spring.factories,找到EnableAutoConfiguration對應的類:

可知自動設定類為MybatisAutoConfiguration:

檢視設定繫結類MybatisProperties,可知yml中字首設定為mybatis:

可知,mybatis字首的yml設定,可以設定屬性,比如:configLocationmapperLocationstypeAliasesPackage等等。

mybatis下,又具有@NestedConfigurationProperty成員變數,故而字首是mybatis.configuration,其中具有如下屬性:

其中有非常熟悉的屬性:mapUnderscoreToCamelCase,也就是資料庫欄位下劃線轉駝峰的設定。

然後,資料來源有如下設定:

資料來源的設定繫結是DataSourceProperties

可知,資料來源在yml中以spring.datasource為字首,可設定連線資料來源的driverClassNameurlusernamepassword等引數。

2 使用

2.1 SpringBoot設定整合mybatis:

建表:

實體類(mybatis本質上將資料庫表的資料和實體類對應,就是依靠的getter和setter,所以@Data是必須有的):

package com.xiaoxu.boot.dto;
import lombok.Data;
import java.util.Date;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.dto.PeopleDTO
 */
@Data
public class PeopleDTO {
    // 人的id編號
    long id;
    // 人的名字
    String myName;
    // 年齡
    int myAge;
    // 出生日期
    Date birthday;
}

新建Mapper介面:

注意mapper介面必須要有@Mapper註解:

package com.xiaoxu.boot.mapper;
import com.xiaoxu.boot.dto.PeopleDTO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PeopleMapper {
    List<PeopleDTO> queryPeopleByAge(int age);
}

在resources目錄下準備mybatis的組態檔,以及Mapper檔案:

mybatis-confi.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

<?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.xiaoxu.boot.mapper.PeopleMapper">
    <select id="queryPeopleByAge" resultType="com.xiaoxu.boot.dto.PeopleDTO">
        select * from my_people where my_age = #{age}
    </select>
</mapper>

在application.yml中設定如下:

spring:
  datasource:
    username: root
    password: ******
    url: jdbc:mysql://localhost:3306/xiaoxu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

#mybatis的相關設定
mybatis:
  #mapper組態檔
  mapper-locations: classpath:mapper/*.xml
#  #mybatis組態檔
#  config-location: classpath:mybatis-config.xml
#  config-location和configuration不能同時存在
  #開啟駝峰命名
  configuration:
    map-underscore-to-camel-case: true

插入測試資料:

service層實現:

package com.xiaoxu.service;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.boot.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.service.PeopleService
 */
@Service
public class PeopleService {
    @Autowired
    PeopleMapper peopleMapper;
    public List<PeopleDTO> getPeoples(int Age){
        return peopleMapper.queryPeopleByAge(Age);
    }
}

controller層實現:

package com.xiaoxu.boot.controller;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.controller.PeopleController
 */
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/people")
    public List<PeopleDTO> queryPeople(@RequestParam(value = "ag") int age){
        return peopleService.getPeoples(age);
    }
}

執行結果無誤:

2.2 SpringBoot註解整合mybatis:

修改Mapper介面檔案:

package com.xiaoxu.boot.mapper;
import com.xiaoxu.boot.dto.PeopleDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface PeopleMapper {
    List<PeopleDTO> queryPeopleByAge(int age);
    @Select("select * from my_people")
    List<PeopleDTO> queryAllPeople();
}

修改服務層:

package com.xiaoxu.service;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.boot.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.service.PeopleService
 */
@Service
public class PeopleService {
    @Autowired
    PeopleMapper peopleMapper;
    public List<PeopleDTO> getPeoples(int Age){
        return peopleMapper.queryPeopleByAge(Age);
    }
    public List<PeopleDTO> getAllPeople(){
        return peopleMapper.queryAllPeople();
    }
}

增加controller:

package com.xiaoxu.boot.controller;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.controller.PeopleController
 */
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/people")
    public List<PeopleDTO> queryPeople(@RequestParam(value = "ag") int age){
        return peopleService.getPeoples(age);
    }
    @GetMapping("/allPeople")
    public List<PeopleDTO> queryAllPeople(){
        return peopleService.getAllPeople();
    }
}

結果返回無誤:

2.3 在設定類上增加@MapperScan註解,掃描某個包下的全部Mapper檔案:

如果每個Mapper介面檔案上增加@Mapper比較麻煩,那麼可以在設定類,如主程式類上,增加@MapperScan註解,以及掃描路徑,效果和@Mapper一致:

主程式類增加@MapperScan註解:

重新執行效果一致:

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!  


IT145.com E-mail:sddin#qq.com