您好,登錄后才能下訂單哦!
這篇文章主要講解了“Spring Boot整合Mybatis的配置方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring Boot整合Mybatis的配置方法”吧!
配置文件形式
pom.xml
application.yml:
UserMapper.xml
UserMapper
配置springboot整合mybatis
在運行類上添加@MapperScan注解
測試類
效果
<?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文件中配置數據源。
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 # 整合mybatis mybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
這里注意!!!:一定是和UserMapper相同的目錄,是個三級目錄,創建時仿照這樣創建com/keafm/mapper(正確的) 別這樣com.keafam.mapper(錯誤的),這樣錯誤的創建的話,是個一級目錄,不是三級的,后面運行的時候可能會提示找不到Mapper。
<?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.keafmd.mapper.UserMapper"> <select id="list" resultType="map"> select * from user </select> </mapper>
package com.keafmd.mapper; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * Keafmd * * @ClassName: UserMapper * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 16:09 * @Blog: https://keafmd.blog.csdn.net/ */ public interface UserMapper { List list(); }
在application.yml中配置:
# 整合mybatis mybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
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的配置方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。