步骤:
1、创建springboot项目,勾选相关依赖
2、修改pom文件
3、编写application.yml(application.properties)
4、编写三层架构
5、测试
以上是springboot整合mybatis的主要步骤,其中最重要的是第三步———编写application.yml文件,这是springboot的配置文件mybatis的配置也写在里面。
一、创建springboot项目
直接在IDEA里选择Spring Initializr,在右边的界面里修改项目名(Name),存放位置(Location),编程语言(Language),项目管理框架(Type),组织名(Group),包名(Package name),jdk,打包方式(Packaging)。
这里我们选择maven构建项目,jdk选择17,打包方式Jar
接下来选择项目依赖,这个项目是springboot整合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这两个依赖。
1、mybatis-spring
mybatis-spring是spring整合mybatis的基础框架,是mybatis项目针对spring提供的接口专门开发的,只要是使用spring framework框架不管还使用了别的什么技术(springboot也好springMVC也罢)都是需要选择这这个框架的,这是springboot整合mybatis的基础。即使不用springboot,我们用SSM技术开发时也是要选这个的。
2、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);
}
五、测试
完成上述步骤后就可以连接数据库尽心测试了,这里就略过了。