目录
1依赖配置2使用2.1SpringBoot配置整合mybatis:2.2SpringBoot注解整合mybatis:2.3在配置类上增加@MapperScan注解,扫描某个包下的全部Mapper文件:总结1 依赖配置
org.springframework.boot spring-boot-starter-parent 2.5.4 org.springframework.boot spring-boot-starter-web org.projectlombok lombok 1.18.22 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-configuration-processor true
找到mybatis-spring-boot-starter配置的依赖,即autotoconfigure包,SpringBoot的自动配置,会找到META-INF下的spring.factories,找到EnableAutoConfiguration对应的类:
可知自动配置类为MybatisAutoConfiguration:
查看配置绑定类MybatisProperties,可知yml中前缀配置为mybatis:
可知,mybatis前缀的yml配置,可以配置属性,比如:configLocation、mapperLocations、typeAliasesPackage等等。
mybatis下,又具有@NestedConfigurationProperty成员变量,故而前缀是mybatis.configuration,其中具有如下属性:
其中有非常熟悉的属性:mapUnderscoreToCamelCase,也就是数据库字段下划线转驼峰的配置。
然后,数据源有如下配置:
数据源的配置绑定是DataSourceProperties:
可知,数据源在yml中以spring.datasource为前缀,可配置连接数据源的driverClassName、url、username、password等参数。
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 queryPeopleByAge(int age);
}
在resources目录下准备mybatis的配置文件,以及Mapper文件:
mybatis-confi.xml:
在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 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 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 queryPeopleByAge(int age);
@Select("select * from my_people")
List 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 getPeoples(int Age){
return peopleMapper.queryPeopleByAge(Age);
}
public List 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 queryPeople(@RequestParam(value = "ag") int age){
return peopleService.getPeoples(age);
}
@GetMapping("/allPeople")
public List queryAllPeople(){
return peopleService.getAllPeople();
}
}
结果返回无误:
2.3 在配置类上增加@MapperScan注解,扫描某个包下的全部Mapper文件:
如果每个Mapper接口文件上增加@Mapper比较麻烦,那么可以在配置类,如主程序类上,增加@MapperScan注解,以及扫描路径,效果和@Mapper一致:
主程序类增加@MapperScan注解:
重新执行效果一致:
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?

