springboot整合mybatis流程
- 创建项目,添加必要的驱动。
自动生成的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>
- 将自动生成的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
- 创建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>
- 建立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);
}
- 注解设置映射接口文件扫描路径(必要)
配置完成,测试成功。
问题总结
问题一
不书写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>
其他原因可参考
链接: 点击跳转