springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value

Intelligence Idea 中逆向生成实体类时,数据库表字段为 int 类型 且not null,属性默认转换为 long

但是在执行insert方法时,一直报错:Field 'user_id' doesn't have a default value,跟踪检查过多遍,一直是赋值了的。

 

看控制台输出的sql 为  insert into user_favor(create_time) values(?)

user_id根本就没有。

索性,跟了下原码:

 

点击

1、点击 userFavorMapper.insert(userFavor) 的insert方法,进入InsertMapper

 

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第1张图片

2、点击进入BaseInsertProvider

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第2张图片

 

sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass)));
sql.append(SqlHelper.insertColumns(entityClass, false, false, false));

这两行代码用来生成insert 语句,insertIntoTable是拼接表名,insertColumns是拼接列名

 

3、点击 SqlHelper.insertColumns 方法,进入 SqlHelper

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第3张图片

4、点击进入 EntityHelper.getColumns方法

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第4张图片

5、点击进入  getEntityTable 方法,看到是从 entityTableMap中获取数据

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第5张图片

 

6、找到下面  entityTableMap 初始化的方法initEntityNameMap

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第6张图片

7、点击进入  DefaultEntityResolve 的 resolveEntity 方法

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第7张图片

最关键的代码在这里 !SimpleTypeUtil.isSimpleType(field.getJavaType())

点击进入isSimpleType方法,可以看到,

SIMPLE_TYPE_SET中存放的都是包装类 : Long,Integer,Short,Byte ……

springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value_第8张图片

至此,找到了原因,把long 修改为Long,问题得以解决。

你可能感兴趣的:(springboot+mybatis 逆向生成的实体,调用insert方法时报错Field 'user_id' doesn't have a default value)