添加依赖
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
编写mapper接口
编写sql映射文件
配置yml文件
server:
port: 9091
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/Bike
username: root
password: admin123
mybatis:
# 对实体类定义别名
type-aliases-package: com.example.demo.domain
# 配置映射文件
mapper-locations: classpath:mapper/*Mapper.xml
在启动类中扫描映射类
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//扫描mybatis所有业务mapper接口
@MapperScan("com.example.demo.mapper.UserMapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}