1. 程式人生 > 實用技巧 >學習Mybatis-plus遇到的坑(彙總)

學習Mybatis-plus遇到的坑(彙總)

1、Could not autowire. No beans of 'UserMapper' type found.
解:對應的mapper介面寫@Repository

@Repository // 代表持久層
public interface UserMapper extends BaseMapper<User> {
    // 所有的CRUD操作都已經編寫完成了
    // 你不需要像以前的配置一大堆檔案了!
}

2、Error querying database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
解:mybatisplus的效能分析攔截器,用於輸出每條 SQL 語句及其執行時間,在MyBatisPlusConfig中修改引數:maxTime,它是 SQL 執行最大時長,超過自動停止執行

/**
     * SQL執行效率外掛
     */
    @Bean
    @Profile({"dev","test"})// 設定 dev test 環境開啟,保證我們的效率
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(800); //ms 設定sql執行的最大時間,如果超過了則不執行
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

3、Error attempting to get column 'create_time' from result set. Cause: java.sql.SQLException: Zero date value prohibited(0000-00-00 00:00:00)
解:在資料來源源配置(application.properties)中的spring.datasource.url末尾新增: &zeroDateTimeBehavior=convertToNull

# 資料庫連線配置
spring.datasource.username=root
spring.datasource.password=199829.
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

4、資料庫不能設計表的datetime欄位的預設值為CURRENT_TIMRSTAMP,可以考慮將datetime改為timestamp,並且可能需要在my.ini配置檔案中新增sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character-set-server=utf8

bind-address = 0.0.0.0

port = 3306

basedir=D:\MySQL\mysql-5.7.29-winx64

datadir=D:\MySQL\mysql-5.7.29-winx64\data

max_connections=2000

default-storage-engine=INNODB

[mysql]
default-character-set=utf8
[mysql.server]
default-character-set=utf8

[client]
default-character-set=utf8