使用MyBatis-Plus时,getById()方法总是返回null,日志报“Warn: Could not find @TableId in Class: com.tp.entity.User.”

今天第一次尝试使用MyBatis-Plus时,发现在UserController中使用userService.getById(id)无法得到数据,查看控制台的日志,发现日志中有一条警告2020-09-04 17:36:41.802 WARN 16036 --- [ restartedMain] c.b.m.core.metadata.TableInfoHelper : Warn: Could not find @TableId in Class: com.tp.entity.User.

然后查看User实体类,发现MyBatis-Plus代码生成器生成的代码里面没有id属性:

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("m_user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
	// 数据库中是有id字段的,这里没有生成
    private String username;

    private String email;

    private String password;

}

我有点疑惑,找了一圈,在MyBatis-Plus的代码生成器CodeGenerator中发现了问题所在。

public class CodeGenerator {
		//省略。。。。。。
    
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
//        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
        strategy.setSuperEntityColumns("id");  /*这一行本来该注释掉的...>_< */
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("m_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

这里,示例代码说过了没有父类实体父类控制器的话不用设置SuperEntityClassSuperControllerClassSuperEntityColumns,我当时其实是看到这个的,所以把对应那两行代码注释了,但是我不知怎么的把strategy.setSuperEntityColumns("id");这行给漏了,没给注释掉,结果自动生成的User实体类里就没有id属性。

既然找到了问题,那么把这行代码注释掉,删掉之前生成的entity,service,mapper,重新跑一遍CodeGenerator,得到的实体类如下:

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("m_user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private String username;

    private String email;

    private String password;

}

这下User实体类就有id属性了,再重新跑userService.getById(id)就不是null了,日志中的warning也没了。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Using VFS adapter com.baomidou.mybatisplus.autoconfigure.SpringBootVFS Checking to see if class com.lxyk.pojo.Account matches criteria [is assignable to Object] Checking to see if class com.lxyk.pojo.Customer matches criteria [is assignable to Object] Checking to see if class com.lxyk.pojo.History matches criteria [is assignable to Object] Checking to see if class com.lxyk.pojo.MainMenu matches criteria [is assignable to Object] Checking to see if class com.lxyk.pojo.Salary matches criteria [is assignable to Object] Checking to see if class com.lxyk.pojo.WaterDetails matches criteria [is assignable to Object] Checking to see if class com.lxyk.pojo.Worker matches criteria [is assignable to Object] Scanned package: 'com.lxyk.pojo' for aliases Warn: Could not find @TableId in Class: com.lxyk.pojo.Customer. Parsed mapper file: 'file [E:\Java_Pro\Project\basic-code\Springboot_Pro\target\classes\mapper\CustomerMapper.xml]' Warn: Could not find @TableId in Class: com.lxyk.pojo.History. Parsed mapper file: 'file [E:\Java_Pro\Project\basic-code\Springboot_Pro\target\classes\mapper\HistoryMapper.xml]' Warn: Could not find @TableId in Class: com.lxyk.pojo.Salary. Parsed mapper file: 'file [E:\Java_Pro\Project\basic-code\Springboot_Pro\target\classes\mapper\SalaryMapper.xml]' Warn: Could not find @TableId in Class: com.lxyk.pojo.WaterDetails. Parsed mapper file: 'file [E:\Java_Pro\Project\basic-code\Springboot_Pro\target\classes\mapper\WaterDetailsMapper.xml]' Warn: Could not find @TableId in Class: com.lxyk.pojo.Worker. Parsed mapper file: 'file [E:\Java_Pro\Project\basic-code\Springboot_Pro\target\classes\mapper\WorkerMapper.xml]' Warn: Could not find @TableId in Class: com.lxyk.pojo.Account.
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值