Spring Boot整合MyBatis实战指南
Spring Boot整合MyBatis实战指南
Spring Boot与MyBatis的整合是Java后端开发中常见的技术栈组合。本文将详细介绍从项目创建到最终测试的完整整合流程,帮助开发者快速掌握这一技术组合的核心要点。
一、创建Spring Boot项目
在IDEA中选择Spring Initializr,配置项目的基本信息,包括项目名、存放位置、编程语言、项目管理框架、组织名、包名、JDK版本和打包方式。选择Maven构建项目,JDK版本为17,打包方式为Jar。
接下来选择项目依赖,由于这是一个Spring Boot整合MyBatis的Web项目,需要勾选Spring Web、MySQL数据库驱动和MyBatis框架。最后点击Finish完成创建。
二、修改POM文件
在POM文件中主要添加MyBatis-Spring框架,并调整依赖版本以避免版本不匹配导致的启动失败。以下是修改后的POM文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo</name>
<description>springboot_demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<!--springbootweb依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis的springboot依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中重点讲解下mybatis-spring和mybatis-spring-boot-starter这两个依赖。
mybatis-spring
mybatis-spring是spring整合mybatis的基础框架,是mybatis项目针对spring提供的接口专门开发的,只要是使用spring framework框架不管还使用了别的什么技术(springboot也好springMVC也罢)都是需要选择这这个框架的,这是springboot整合mybatis的基础。即使不用springboot,我们用SSM技术开发时也是要选这个的。mybatis-spring-boot-starter
这是mybatis基于mybatis-spring开发的针对springboot整合mybatis的,他是依赖于mybatis-spring运行的。他的智能之处就在于当你在Mapper接口上使用了@Mapper注解之后它可以自动扫描到Mapper接口完成mapper注册,省去了之前繁琐的在配置文件中注册mapper的操作。
三、编写application.yml(application.properties)
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: Wsz031227#
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
四、编写三层架构
Mapper.xml 文件统一放在resources目录下方便管理,mapper接口写在java包下
一定要注意:必须在启动类上加@MapperScan注解和在mapper接口上加@Mapper注解这两个中间选一个,否则spring将无法扫描到mapper接口,个人建议两个都加上避免遗漏。
@SpringBootApplication
//在启动类上加,参数为mapper接口在的文件夹
@MapperScan("com.example.springboot_demo.dao")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
//在mapper接口上加,直接一个@Mapper注解就完事
@Mapper
public interface StudentDao {
int insert(Student student);
}
五、测试
完成上述步骤后就可以连接数据库尽心测试了,这里就略过了。