zoukankan      html  css  js  c++  java
  • 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));
    
  • 相关阅读:
    无线网络的切换bat
    远程桌面远程服务器拷贝失灵 解决方法
    不能将 Null 值赋给类型为 (不可为 null 的值类型)的成员。解决方法
    应该改变面向对象的说法
    Windows 2012 装 Remote Desktop Organizer 无法连接到其他远程服务器
    JavaScript 运行时错误: 无法获取未定义或 null 一种解决方案
    ssd硬盘u盘装win7扩展文件时0x80070570错误
    swfit-小知识Demo
    ios-简单算法
    swfit-扩展语法
  • 原文地址:https://www.cnblogs.com/mozq/p/11767307.html
Copyright © 2011-2022 走看看