1、添加依赖:

  1. <!-- mysql 依赖jar,只添加mysql依赖可以不配置数据源 -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.26</version>
  6. </dependency>
  7. <!-- mybatis 依赖jar,添加mybatis起步依赖,必须配置数据源 -->
  8. <dependency>
  9. <groupId>org.mybatis.spring.boot</groupId>
  10. <artifactId>mybatis-spring-boot-starter</artifactId>
  11. <version>2.2.0</version>
  12. </dependency>

2、修改配置文件application.yml

添加数据源配置和MyBatis配置

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=UTF-8
  5. # 使用mysql8的驱动 要加上&serverTimezone=Asia/Shanghai
  6. username: root
  7. password: root
  8. #mybatis配置
  9. mybatis:
  10. mapper-locations: classpath:mapper/*.xml
  11. # config-location: classpath:mybatis-config.xml
  12. type-aliases-package: edu.xja.entity
  13. configuration:
  14. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  15. #下划线转驼峰
  16. map-underscore-to-camel-case: true
  17. #mybatis配置
  18. #mybatis:
  19. #typeAliasesPackage: edu.xja.domain
  20. #mapperLocations: classpath:mapping/*.xml
  21. #configLocation: classpath:/mybatis-config.xml

代码解释:
Yml配置文件也支持驼峰模式,看上面的注释,理解一下
Mysql8 url 配置加上时区:serverTimezone=Asia/Shanghai

3、创建启动类进行测试

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class,args);
  5. }
  6. }

创建Dao层,Service层进行测试,暂不赘述。