一篇超详细的Spring Boot整合Mybatis文章
程序员文章站
2022-03-10 20:51:38
目录usermapper.xmlusermapper在运行类上添加@mapperscan注解配置文件形式pom.xml
配置文件形式
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 # 整合mybatis mybatis: # typealiasespackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
usermapper.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>
usermapper
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(); }
配置springboot整合mybatis
在application.yml中配置:
# 整合mybatis mybatis: # 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 + Kotlin整合MyBatis的方法教程
-
Spring Boot+Mybatis的整合过程
-
spring boot整合mybatis+mybatis-plus的示例代码
-
Spring boot Mybatis整合构建Rest服务(超细版)
-
spring boot整合mybatis利用Mysql实现主键UUID的方法
-
Spring boot整合Mybatis-plus分页插件的使用
-
接之前的文章,VS2017中使用Spring.NET配置以及使用方法(framework4.6.1超详细)
-
SpringBoot 源码解析 (九)----- Spring Boot的核心能力 - 整合Mybatis
-
一篇超详细的SpringBoot整合MybatisPlus的文章
-
Spring Boot 整合mybatis 使用多数据源的实现方法