Spring Boot整合Mybatis【超详细】

我不是女神ヾ 2022-11-17 05:26 213阅读 0赞

pring Boot整合Mybatis

  • 配置文件形式
    • pom.xml
    • 配置数据源
    • UserMapper.xml
    • UserMapper
    • 配置springboot整合mybatis
    • 在运行类上添加@MapperScan注解
    • 测试类
    • 效果

配置文件形式

在这里插入图片描述

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.4.4</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>com.keafmd</groupId>
  11. <artifactId>spring-boot-09-mybatis</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>spring-boot-09-mybatis</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-jdbc</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.mybatis.spring.boot</groupId>
  29. <artifactId>mybatis-spring-boot-starter</artifactId>
  30. <version>1.3.2</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>mysql</groupId>
  34. <artifactId>mysql-connector-java</artifactId>
  35. <scope>runtime</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

配置数据源

在yml文件中配置数据源。

application.yml:

  1. server:
  2. port: 80
  3. # 配置数据源
  4. spring:
  5. datasource:
  6. url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false
  7. driver-class-name: com.mysql.cj.jdbc.Driver
  8. username: root
  9. password: 18044229
  10. # 整合mybatis
  11. mybatis:
  12. # typeAliasesPackage: com.neuedu.entity
  13. mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml

UserMapper.xml

这里注意!!!:一定是和UserMapper相同的目录,是个三级目录,创建时仿照这样创建com/keafm/mapper(正确的) 别这样com.keafam.mapper(错误的),这样错误的创建的话,是个一级目录,不是三级的,后面运行的时候可能会提示找不到Mapper。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.keafmd.mapper.UserMapper">
  4. <select id="list" resultType="map">
  5. select * from user
  6. </select>
  7. </mapper>

UserMapper

  1. package com.keafmd.mapper;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import java.util.List;
  4. /** * Keafmd * * @ClassName: UserMapper * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 16:09 * @Blog: https://keafmd.blog.csdn.net/ */
  5. public interface UserMapper {
  6. List list();
  7. }

配置springboot整合mybatis

在application.yml中配置:

  1. # 整合mybatis
  2. mybatis:
  3. # typeAliasesPackage: com.neuedu.entity
  4. mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml

在运行类上添加@MapperScan注解

SpringBoot09MybatisApplication:

  1. package com.keafmd;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan("com.keafmd.mapper")
  7. public class SpringBoot09MybatisApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringBoot09MybatisApplication.class, args);
  10. }
  11. }

测试类

UserMapperTest :

  1. package com.keafmd.mapper;
  2. import com.keafmd.SpringBoot09MybatisApplication;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import java.util.List;
  7. import static org.junit.jupiter.api.Assertions.*;
  8. @SpringBootTest(classes = SpringBoot09MybatisApplication.class)
  9. class UserMapperTest {
  10. @Autowired
  11. UserMapper userMapper;
  12. @Test
  13. void list(){
  14. List list = userMapper.list();
  15. for (Object o : list) {
  16. System.out.println(o);
  17. }
  18. }
  19. }

效果

在这里插入图片描述

以上就是Spring Boot整合Mybatis【超详细】的全部内容。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring boot整合Mybatis

    时隔两个月的再来写博客的感觉怎么样呢,只能用“棒”来形容了。闲话少说,直接入正题,之前的博客中有说过,将spring与mybatis整个后开发会更爽,基于现在springboo