前情回顧
一起入門mybatis-plus之搭建逆向工程
沒有引入并配置mybaits-plus的同學(xué)可以參考上文,以及如何搭建逆向工程,專注于java代碼編寫。不再編寫繁瑣的數(shù)據(jù)庫表對應(yīng)的實(shí)體類和mapper等。
環(huán)境說明
- springboot版本: 2.3.0
- java版本:8
- mybatis-plus版本:3.3.1
- mysql版本:8.0
springboot整合mybatis-plus
添加依賴:
<!--springboot整合mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
配置數(shù)據(jù)源
application.yml
server:
port: 8080
#databse config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/數(shù)據(jù)庫?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT
username: root用戶名
password: root用戶密碼
type: com.zaxxer.hikari.HikariDataSource
- driver-class-name 驅(qū)動(dòng)使用com.mysql.cj.jdbc.Driver,com.mysql.jdbc.Driver已經(jīng)過期
- type 數(shù)據(jù)連接池推薦使用HikariDataSource,比druid性能更好,也是推薦的版本。性能方面 hikari>druid>tomcat-jdbc>dbcp>c3p0 。hikari可以最大限度的避免鎖競爭。
mapper注入
@MapperScan("com.hugo.talk.ssm.repository.mapper")
@SpringBootApplication
@MapperScan("com.hugo.talk.ssm.repository.mapper")
public class MyBatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisPlusApplication.class, args);
}
}
- 注意整合mybatis時(shí),對mapper的注入不要忘記用@MapperScan掃描。
增刪改查
增加日志
application.yml增加mybatis-plus配置:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
問題記錄
1.com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '?й???????' is unrecognized or represents more than one time zone.
解決方法:說明你使用的是mysql8.0版本,在配置數(shù)據(jù)源的額時(shí)候,需要再加上參數(shù),指定時(shí)區(qū):serverTimezone=GMT
url: jdbc:mysql://localhost:3306/數(shù)據(jù)庫?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT
2.org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
- 查看mybatis-plus相關(guān)依賴是否缺少
- 查看@MapperScan掃描路徑是否正確
- 查看xxxMapper.xml文件中,namespace是否正確