springboot整合mybatis,对于mapper文件扫描,和xml扫描详解
使用idea创建springboot项目(这里不做主要讲解)
1.准备连接数据库(用自己的配置:192.168.10.* 3306 xl root 123456
2.导数据库相关包:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
3.添加配置文件
spring.datasource.url = jdbc:mysql://192.168.10.*:3306/xl?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = congerfei
spring.datasource.driverClassName =com.mysql.cj.jdbc.Driver
4.写pojo (根据数据库的字段进行编写,并写入get/set/toString)
写dao 和对应的实现类(整合mybatis后使用mapper文件)
写service 和对应的实现类(要注入dao层,很多人这里都注入不成功,下面将会详细讲解这个的)
写controller (要注入service,使用@Service这里相信大家都能注入成功)
5.先保证编译环境一致
比如
java.version>1.8</java.version>
和idea中的setting中的编译jdk版本
6.我这里还碰到了版本不兼容问题,推荐springboot使用2.1.6.RELEASE,mybatis-spring使用1.3.2
(亲测和springboot 2.2.2有冲突,具体原因没细查)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--mybatis启动器,@Mapper注解需要使用到-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
7.扫描userDao,也就是我们的所说的各种mapper.java文件,此时注解是@Repository,和下面的一致
启动类上添加 或者只mapper文件上加入@Mapper即可
@MapperScan(basePackages = "com.xl.springboot.user.dao",annotationClass= Repository.class)
8.最重点的是扫描mapper.xml文件,
定位mapper.xml文件
mybatis.mapper-locations=classpath*:mappers/*.xml
为啥这个能扫描到呢?因为默认有这个
<resource><!--默认的-->
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
同理可以添加去扫描java包下的xml文件,
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
```
java包下的定位mapper.xml文件:
mybatis.mapper-locations=classpath*:com/xl/springboot/**/*.xml
总结以及报错分析:
定位mapper.java文件:
尝试去掉annotationClass= Repository.class ,发明是没有问题的也就是说只要扫描包就行,需要具体定位可加注解
尝试去掉mapper.java文件中的@Repository 发现也是能够扫描到的,但是需要此注解交给spring进行管理,使@Autowired有效
尝试启动类不扫描包:换成mapper.java文件中的@Mapper注解,也是能正常获取到的
个人见解:@mapper 将接口生成对应的实现类,@Repository将实现类交给spring管理,具体如何实现需要匹配到xml文件
报错问题:
1.扫描mapper文件,如果没有报错:
o.s.b.d.LoggingFailureAnalysisReporter :
Field userDao in com.xl.springboot.user.service.impl.LoginServiceImpl required a bean of type 'com.xl.springboot.user.dao.UserDao' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
2.交给spring管理,@Repository即可解决,如果没有@Autowired则编译不通过
3.建立xml与mapper文件的关联,如果没有会报错 Invalid bound statement (not found): com.xl.springboot.user.dao.UserDao.queryUserById
本文详细介绍了如何在SpringBoot项目中整合Mybatis,包括设置数据库连接、导入相关依赖、配置文件、编写POJO、DAO、Service及Controller。特别强调了Mapper文件的扫描,包括使用@Repository注解和@Mapper注解的方式,以及如何通过配置扫描XML文件,确保SpringBoot 2.1.6.RELEASE与mybatis-spring 1.3.2的版本兼容。

1436

被折叠的 条评论
为什么被折叠?



