SpringBoot整合mybatis

SpringBoot整合mybatis

一. 创建一个SpringBoot项目

二. 项目结构

image-20211110151308320

注意springboot的启动类的位置

三. 配置文件

(1) 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.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--  web核心依赖  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!--  lombok 插件 简化代码      -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>


        <!--mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

        </plugins>


        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        
    </build>


</project>

pom.xml 配置吃过的亏:

a. mybatis-plus 依赖

报错:
java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.mybatis.spring.annotation.MapperScan.factoryBean()
    at java.lang.reflect.Method.getDefaultValue(Method.java:612) ~[na:1.8.0_92]

SpringMVC的引入方式:

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.0</version>
        </dependency>

SpringBoot的引入方式:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

还有一点要注意的是:

​ 引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。

b. 配置resource

​ 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。

       
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
  注:<resources> 要放在 <build> 里面

(2) application.yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123
    url: jdbc:mysql:///myschool
    driver-class-name: com.mysql.cj.jdbc.Driver

#mybatis-plus配置
mybatis-plus:
  mapper-locations: classpath:mapping/*.xml

#showSql
logging:
  level:
    com:
      example:
        mapper : debug

mybatis-plus的扫描路径也可以在pom.xml里面写:

<resource>
    <directory>src/main/java</directory>
    <includes>
        <include>**/*.properties</include>
        <include>com.lv.mybatis.dao.xml/*.xml</include>
    </includes>
    <filtering>false</filtering>
</resource>

这一段没有起到作用,我把 UserMapper.xml 放在 resources/mapping下面的话,就是不知道为什么和 UserMapper 映射不了

![image-20211110151006809](C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20211110151006809.png)这样不行														   ![image-20211110151108842](C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20211110151108842.png)‘最后还是改成这样才可以

四. 使用 mybatis-plus

  1. pom.xml 添加依赖

  2. application.yml 设置数据库连接 和 配置mybatis

  3. 写 po 实体类

    import lombok.Data;
    
    @Data  
    public class Users {
    
      private long id;
      private String username;
      private String password;
    }
    
  4. 写 mapper 接口类

    //@Mapper 
    @Repository
    public interface UserMapper extends BaseMapper<Users> {
        /**
         *  BaseMapper 里封装了一些常用的 CRUD
         *  直接继承 BaseMapper 可以省去xml的编写
         */
    
    
        /**
         * 当然也可以通过 xml 自定义 sql 查询语句
         */
        // 该方法使用User实体类
        public List<Users> getAllUser();
    }
    

    a. 继承 BaseMapper

    b. 写自己的操作数据的方法

    c. 可选注解

    ​ @Mapper

    ​ @Repository:需要在启动类加上

    @MapperScan("com.example.demo2.mapper")//指向你的mapper接口类
    
  5. 写 mapper.xml

<?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.example.demo2.mapper.UserMapper">

    <select id="getAllUser" resultType="com.example.demo2.po.Users">
        SELECT * FROM users
    </select>

</mapper>

注意 namespace id resultType

  1. mapper接口和mapper.xml文件的映射(很重要)

    https://blog.csdn.net/weixin_44695793/article/details/107752054

  2. 参考文档
    https://www.cnblogs.com/l-y-h/p/12859477.html

posted @ 2021-11-22 15:38  吹梦西洲  阅读(866)  评论(0编辑  收藏  举报