<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
- 配置文件中配置好mybatis及数据库连接配置
mybatis:
mapper-locations: classpath:mappers/*xml
# 此配置用于提取xml中select标签中resultType类型中的包名
type-aliases-package: com.example.demo.mybatis.entity
configuration:
# 数据库字段下划线间隔变为JAVA小驼峰的对应方式
map-underscore-to-camel-case: true
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC
username: xxx
password: xxx
- 写Mapper接口及实体类
- 写Mapper.xml文件
- 在Service中使用@ Autowired注入Mapper即可