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ū)動(dòng)時(shí),如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的自動(dòng)配置,會(huì)找到META-INF下的spring.factories,找到EnableAutoConfiguration對(duì)應(yīng)的類:


可知自動(dòng)配置類為MybatisAutoConfiguration:


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

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

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

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

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

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

實(shí)體類(mybatis本質(zhì)上將數(shù)據(jù)庫(kù)表的數(shù)據(jù)和實(shí)體類對(duì)應(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編號(hào)
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不能同時(shí)存在
#開啟駝峰命名
configuration:
map-underscore-to-camel-case: true
插入測(cè)試數(shù)據(jù):

service層實(shí)現(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層實(shí)現(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é)果無(wú)誤:

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注解,掃描某個(gè)包下的全部Mapper文件:
如果每個(gè)Mapper接口文件上增加@Mapper比較麻煩,那么可以在配置類,如主程序類上,增加@MapperScan注解,以及掃描路徑,效果和@Mapper一致:

主程序類增加@MapperScan注解:

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

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java 實(shí)戰(zhàn)項(xiàng)目之在線點(diǎn)餐系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)在線點(diǎn)餐系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
java?HttpURLConnection類的disconnect方法與http長(zhǎng)連接詳解
這篇文章主要介紹了java?HttpURLConnection類的disconnect方法與http長(zhǎng)連接,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
對(duì)SpringMVC的@RequestParam的解釋
下面小編就為大家?guī)?lái)一篇對(duì)SpringMVC的@RequestParam的解釋。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

