SpringBoot 整合 Mybatis
开发工具:Spring Tool Suite (简称STS)
数据库环境:mysql5.7
JDK版本:1.8
1.添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
2.配置数据源
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/库名
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
# 实体所在包,起别名
mybatis.typeAliasesPackage=实体类路径 #例:com.sh.entity
# 映射文件所在路径
mybatis.mapperLocations=classpath:mapper/*.xml
3.扫描接口包 @MapperScan(“包名”)
package com.sh;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@MapperScan("com.sh.dao") //扫描mapper
@SpringBootApplication
public class SpringBootMybatis02Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatis02Application.class, args);
}
}