两种实现方式+配置依赖
第一种方式:使用@Mapper+pom.xml资源过滤
使用@Mapper注解,并且在pom.xml里加上资源过滤
<!--配置指定扫描的web文件目录-->
<resources>
<resource><!--扫描指定xml目录-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource><!--扫描指定web目录-->
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
<resource><!--扫描指定resource资源目录-->
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
第二种方式:@MapperScan(包名)+ .yaml文件指定路径
注意:@MapperScan注解是在springboot启动类里,并不是mapper接口类
在application.yaml里设置好扫描路径
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: root
password: admin
# 指定mybatis映射文件的路径
mybatis:
mapper-locations: classpath:mapper/*.xml
# 自定义端口号 + 上下文根
server:
port: 8080
servlet:
context-path: /springboot
整合所需要的依赖驱动(maven)
<?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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.czh.springboot</groupId>
<artifactId>springboot06</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot06</name>
<properties>
<java.version>1.8</java.version>
</properties>
<!--springboot整合mybatis起步依赖-->
<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>
</dependency>
<!--配置springboot内置的tomcat中间件-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--mybatis 代码自动生成插件(如果不使用GeneratorMapper.xml,则不需要该依赖)-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
<!--配置指定扫描的web文件目录-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
错误:
1,每个对象都需要加入到spring容器进行管理,使用springIOC注解
2,@SpringBootApplication(exclude = DataSourceAutoConfiguration.class),这个是设置,启动不配置数据库