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>

猜你喜欢

转载自www.cnblogs.com/mozq/p/11767307.html