一、springboot-mybatis
1.1 坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web核心依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<!-- 扫描java下面的mapper文杰 -->
<!-- ......用于扫描 dao 文件下的mapper 文件................. start -->
<resources>
<!-- 编译 src/main/java 目录下的 mapper 文件 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- ......用于扫描 dao 文件下的mapper 文件................. end -->
</build>
1.2 配置文件
连接配置
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
# driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///demo?characterEncoding=utf-8&useSSL=false
# ?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
mybatis:
# 指定全局配置文件位置
config-location: classpath:mybatis-config.xml
# 指定sql映射文件位置
mapper-locations: classpath:mapper/*.xml
#showSql
logging:
level:
com:
three:
mapper : debug
第一种配置
resources + myabtis-config.xml
配置
mybatis:
config-location: classpath:mybatis-config.xml
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
第二种配置
resources + application.yml
mybatis:
# 斜杠和点号都可以
# mapper-locations: classpath*:com.example.mapper/*.xml
mapper-locations: classpath*:com/example/mapper/*.xml
type-aliases-package: com.example.bean
config-location: classpath:mybatis-config.xml
configuration:
map-underscore-to-camel-case: true
第三种配置
java + properties.yml
mybatis:
mapper-locations: classpath*:mapper/*.xml
# type-aliases-package: com.example.bean
# config-location: classpath:mybatis-config.xml
# configuration:
# map-underscore-to-camel-case: true
解决idea报错问题
@Component("userMapper")
@Mapper
public interface UserMapper {
List<User> findAll();
}