一起入門mybatis-plus之增刪改查

前情回顧

一起入門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是否正確
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容