您的位置:首页 > 编程语言 > Java开发

Springboot整合Mybatis及注意事项

2020-07-21 04:14 1561 查看

Mybatis文件的生成参考上篇mybatis-generator的介绍https://blog.csdn.net/d20062056/article/details/106750447

一、引入依赖

[code]        <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文件配置数据库连接信息

[code]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(方便调试)

[code]mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: