Mybatis-Plus Bugs


Mybatis-Plus Bugs

實體類中屬性和數據庫字段對應異常

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'user_id' in 'field list'
原因:
	實體類中有該字段並且有值,但是數據庫不存在該字段。
	INSERT INTO user  ( user_id, name )  VALUES  ( ?, ? )
方案:
    1.字段聲明為靜態字段 private static Long userId;
    2.字段用transient修飾 private transient Long userId;
    3.@TableField注解的exist=false
        @TableField(exist = false)
        private Long userId;
### SQL: INSERT INTO user  ( name )  VALUES  ( ? )
### Cause: java.sql.SQLException: Field 'id' doesn't have a default value
Field 'id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id' doesn't have a default value
原因:
	mybatis-plus 如果實體的字段為null,則不會出現在插入sql語句中,反之則會出現。

新增記錄主鍵生成異常

org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.mozq.boot.sbmp01.pojo.Car' with value '1185494840728498178' Cause: java.lang.IllegalArgumentException: argument type mismatch
原因:
	mybatis-plus 默認的主鍵生成策略不是數據庫自增。而是IDWorker,生成Long值,並設置到實體中,主鍵有值了,則會出現在插入sql語句中,插入到數據庫。
	sql語句:
	INSERT INTO car ( id, company_id, car_license, brand, status ) VALUES ( ?, ?, ?,     ?, ? )
方案:
public class Car implements Serializable {
    @TableId(type = IdType.AUTO)//在主鍵上使用@TableId和type屬性,指定主鍵生成策略為數據庫自增。
    private Integer id;
}
參考:
	https://blog.csdn.net/u010514052/article/details/81775595 青花葬水

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1
原因:
	實體主鍵為默認的IDWorker生成Long,而數據庫字段的類型為int,超出范圍。
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
原因:
	沒有在yml配置文件中配置數據源。

日志工廠不存在

Caused by: java.lang.ClassNotFoundException: org.mybatis.logging.LoggerFactory
原因:
	mybatis-plus和mybatis包不能同時引入。
	當使用mybais-plus時,要將所有直接或間接引入的mybatis依賴排除掉。常見的有mybatis,pagehelper,還有其他使用mybatis的包。
<dependency>
    <groupId>com.ytkj</groupId>
    <artifactId>wechat_start_server</artifactId>
    <version>1.0.4</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Cause: java.sql.SQLSyntaxErrorException: Unknown column 'sys_role_list' in 'field list'
原因:SysUser額外的角色列表在數據庫中沒有對應。
@Data
public class SysUser implements Serializable {
    /**
     * 角色列表
     */
    @TableField(exist = false)
    List<SysRole> sysRoleList;
}
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 10
原因: selectOne()方法只能查詢1個結果或0個,查詢多個結果跑出異常。
SysUser sysUser = sysUserDao.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getCompanyId, 1));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM