1、添加依赖:
<!-- mysql 依赖jar,只添加mysql依赖可以不配置数据源 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- mybatis 依赖jar,添加mybatis起步依赖,必须配置数据源 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2、修改配置文件application.yml
添加数据源配置和MyBatis配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=UTF-8
# 使用mysql8的驱动 要加上&serverTimezone=Asia/Shanghai
username: root
password: root
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
# config-location: classpath:mybatis-config.xml
type-aliases-package: edu.xja.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#下划线转驼峰
map-underscore-to-camel-case: true
#mybatis配置
#mybatis:
#typeAliasesPackage: edu.xja.domain
#mapperLocations: classpath:mapping/*.xml
#configLocation: classpath:/mybatis-config.xml
代码解释:
Yml配置文件也支持驼峰模式,看上面的注释,理解一下
Mysql8 url 配置加上时区:serverTimezone=Asia/Shanghai
3、创建启动类进行测试
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
创建Dao层,Service层进行测试,暂不赘述。