SpringBoot整合mybatis的方法詳解
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連接驅(qū)動時,如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對應(yīng)的類:


可知自動配置類為MybatisAutoConfiguration:


查看配置綁定類MybatisProperties,可知yml中前綴配置為mybatis:

可知,mybatis前綴的yml配置,可以配置屬性,比如:configLocation、mapperLocations、typeAliasesPackage等等。
mybatis下,又具有@NestedConfigurationProperty成員變量,故而前綴是mybatis.configuration,其中具有如下屬性:

其中有非常熟悉的屬性:mapUnderscoreToCamelCase,也就是數(shù)據(jù)庫字段下劃線轉(zhuǎn)駝峰的配置。
然后,數(shù)據(jù)源有如下配置:

數(shù)據(jù)源的配置綁定是DataSourceProperties:

可知,數(shù)據(jù)源在yml中以spring.datasource為前綴,可配置連接數(shù)據(jù)源的driverClassName、url、username、password等參數(shù)。

2 使用
2.1 SpringBoot配置整合mybatis:
建表:

實體類(mybatis本質(zhì)上將數(shù)據(jù)庫表的數(shù)據(jù)和實體類對應(yīng),就是依靠的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目錄下準(zhǔn)備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的相關(guān)配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
# #mybatis配置文件
# config-location: classpath:mybatis-config.xml
# config-location和configuration不能同時存在
#開啟駝峰命名
configuration:
map-underscore-to-camel-case: true
插入測試數(shù)據(jù):

service層實現(xiàn):
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層實現(xiàn):
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);
}
}
執(zhí)行結(jié)果無誤:

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();
}
修改服務(wù)層:
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();
}
}
結(jié)果返回?zé)o誤:

2.3 在配置類上增加@MapperScan注解,掃描某個包下的全部Mapper文件:
如果每個Mapper接口文件上增加@Mapper比較麻煩,那么可以在配置類,如主程序類上,增加@MapperScan注解,以及掃描路徑,效果和@Mapper一致:

主程序類增加@MapperScan注解:

重新執(zhí)行效果一致:

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題
這篇文章主要介紹了SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
MyBatis 動態(tài)SQL之where標(biāo)簽的使用
本文主要介紹了MyBatis 動態(tài)SQL之where標(biāo)簽,where 標(biāo)簽主要用來簡化 SQL 語句中的條件判斷,可以自動處理 AND/OR 條件,下面就來具體介紹一下2024-01-01
Java多線程實現(xiàn)簡易微信發(fā)紅包的方法實例
這篇文章主要給大家介紹了關(guān)于Java多線程實現(xiàn)簡易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringMVC中ModelAndView用法小結(jié)
本文主要介紹了SpringMVC中ModelAndView用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12

