Mybatis文件的生成参考上篇mybatis-generator的介绍https://blog.csdn.net/d20062056/article/details/106750447
一、引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
二、application.yaml文件配置数据库连接信息
spring:
datasource:
url: jdbc:mysql://localhost:3306/java2020?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:/mapper/*.xml
注意事项:
1、driver如果用com.mysql.jdbc.Driver启动时会有如下提醒:
2、未配置serverTimeZone访问时会报错(可正常启动):
3、小插曲:mybatis:mapper-locations这里漏掉“s”,导致访问时报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),排查好久才发现。
三、SpringBoot的启动类加上@MapperScan("com.everest.springboot.mapper")
如果忘记加会启动失败,有如下提醒:
四、输出执行的SQL(方便调试)
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl