SpringBoot整合mybatis的方法详解
作者:小徐也要努力鸭 发布时间:2023-09-02 06:23:57
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配置,可以配置属性,比如: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<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注解:
重新执行效果一致:
来源:https://blog.csdn.net/a232884c/article/details/123258404
猜你喜欢
- 在实际开发中,我们经常会需要在页面跳转的时候携带路由参数,典型的例子就是从列表到详情页的时候,需要携带详情的 id,以便详情页获取对应的数据
- maven3 安装:安装 Maven 之前要求先确定你的 JDK 已经安装配置完成。Maven是 Apache 下的一个项目,目前最新版本是
- 早期的项目比较简单,多是用JSP 、Servlet + JDBC 直接搞定,后来使用 Struts1(Struts2)+Spring+Hib
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters
- SpringBoot集成Mybatis时mybatis.mapper-locations和@MapperScan的作用1、mybatis.m
- 一.WebSocket简单介绍WebSocket protocol 是HTML5一种新的协议。它实现了浏览器与服务器全双工通信(full-d
- 先上图下拉刷新跟原生开发一样,下拉刷新在flutter里提供的有组件实现 RefreshIndicator一直不明白为啥组件中都提供下拉刷新
- 如下所示:String beginDate="1328007600000";SimpleDateFormat sdf=n
- 本文实例讲述了Android编程之消息机制。分享给大家供大家参考,具体如下:一、角色描述1.Looper: 一个线程可以产生一个Looper
- 在使用JDBC的时候,数据库据连接是非常宝贵的资源。为了复用这些资源,可以将连接保存在一个队列中。当需要的时候可以从队列中取出未使用的连接。
- cookie和session的区别和联系cookie是本地客户端用来存储少量数据信息的,保存在客户端,用户能够很容易的获取,安全性不高,存储
- JDK 1.5开始提供ScheduledThreadPoolExecutor类,ScheduledThreadPoolExecutor类继承
- java 请求跨域问题解决方法实例详解新建Util类,在Util中添加下面方法: /* * response请求跨域公共设置
- 最近在做项目的时候有用到对两个集合中的元素进行对比求其交集的情况,因为涉及到的数据量比较大,所以在进行求两个集合中元素交集的时候,就应该考虑
- 在之前项目中有用到关于获取手机联系人的部分,闲置就想和大家分享一下,话不多说,上代码:java部分:package com.example.
- BitArray的基础可以看菜鸟编程BitArray 类管理一个紧凑型的位值数组,它使用布尔值来表示,其中 true 表示位是开启的(1),
- 本文实例讲述了Android中SeekBar和RatingBar用法。分享给大家供大家参考,具体如下:什么是SeekBar?可以拖动的进度条
- 您已经看到很多包含视频内容的应用程序,比如带有视频教程的食谱应用程序、电影应用程序和体育相关的应用程序。您是否想知道如何将视频内容添加到您的
- Android植物大战僵尸小游戏全部内容如下:相关下载:Android植物大战僵尸小游戏具体代码如下所示:package com.examp
- Servlet3.0的出现是servlet史上最大的变革,其中的许多新特性大大的简化了web应用的开发,为广大劳苦的程序员减轻了压力,提高了