这篇文章主要讲解了“Spring Boot整合Mybatis的配置方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring Boot整合Mybatis的配置方法”吧!
目录
配置文件形式
pom.xml
application.yml:
UserMapper.xml
UserMapper
配置springboot整合mybatis
在运行类上添加@MapperScan注解
测试类
效果
配置文件形式
pom.xml
<?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>2.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.keafmd</groupId> <artifactId>spring-boot-09-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-09-mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
配置数据源
在yml文件中配置数据源。
application.yml:
server: port: 80# 配置数据源spring: datasource: url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 18044229# 整合mybatismybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapperpublic interface UserMapper { List list();}
配置springboot整合mybatis
在application.yml中配置:
# 整合mybatismybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
在运行类上添加@MapperScan注解
SpringBoot09MybatisApplication:
package com.keafmd;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.keafmd.mapper")public class SpringBoot09MybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot09MybatisApplication.class, args); }}
测试类
UserMapperTest :
package com.keafmd.mapper;import com.keafmd.SpringBoot09MybatisApplication;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest(classes = SpringBoot09MybatisApplication.class)class UserMapperTest { @Autowired UserMapper userMapper; @Test void list(){ List list = userMapper.list(); for (Object o : list) { System.out.println(o); } }}
效果
感谢各位的阅读,以上就是“Spring Boot整合Mybatis的配置方法”的内容了,经过本文的学习后,相信大家对Spring Boot整合Mybatis的配置方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!