springboot整合mybatis遇到的几个问题

springboot整合mybatis流程

  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>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>test</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

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

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <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>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.1</version>
        </dependency>

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

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.8</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.restdocs</groupId>
                        <artifactId>spring-restdocs-asciidoctor</artifactId>
                        <version>${spring-restdocs.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

  1. 将自动生成的yaml配置文件修改为yml主流配置文件(可选择)。
    在这里插入图片描述

3.书写必要配置信息

spring:
#  profiles:
#    active: dev
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/book?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: 974930069rt


mybatis:
  mapper-locations: classpath:com/example/test/dao/*.xml
#  mapper-locations: com/example/test/dao/*.xml

  1. 创建Mapper||dao映射文件以及接口类,务必建立在同一个包内,路径与上方mapper-locations路径一致
    在这里插入图片描述

bookDao.java

package com.example.test.dao;

import com.example.test.domain.book;
import com.example.test.entity.TBook;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 * @author Administrator
 */
@Repository
public interface BookDao {

     TBook selectById(int id);

}

BookDao.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.test.dao.BookDao">
    <resultMap type="com.example.test.entity.TBook" id="BookMap">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="price" column="price" jdbcType="DOUBLE"/>
        <result property="author" column="author" jdbcType="VARCHAR"/>
        <result property="sales" column="sales" jdbcType="INTEGER"/>
        <result property="stock" column="stock" jdbcType="INTEGER"/>
        <result property="imgPath" column="imgPath" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="selectById" resultMap="BookMap">
        select * from book.t_book
        where id = #{id};
    </select>
</mapper>

  1. 建立service层用于测试。
    BookServiceImpl
package com.example.test.service.impl;

import com.example.test.dao.BookDao;
import com.example.test.entity.TBook;
import com.example.test.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service
public class BookServiceImpl implements BookService {
//    @Qualifier("bookDao")

    @Autowired
    private BookDao bookDao;

    @Override
    public TBook selectById(int id){
        return bookDao.selectById(id);
    }
}

BookService

package com.example.test.service;

import com.example.test.entity.TBook;
import org.springframework.stereotype.Service;


public interface BookService {
    TBook selectById(int id);
}

  1. 注解设置映射接口文件扫描路径(必要)
    在这里插入图片描述
    配置完成,测试成功。

问题总结


问题一

不书写MapperScan(“”)导致:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'bookDao': No qualifying bean of type 'com.example.test.dao.BookDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue

问题二

因为maven项目不解析xml文件所以要在pom文件中进行补充,诺不书写会导致无法找到接口绑定的语句:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.test.dao.BookDao.selectById

设定范围在java下的包中的文件都生效

 <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

其他原因可参考
链接: 点击跳转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z=>(200)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值