通用mapper的selectByPrimaryKey返回null的bug

实体类

@Data
@Table(name = "tb_user")
public class User {

    @Id
    @KeySql(useGeneratedKeys = true)
    private int id;
    
    private String username;
    
    private String password;
}

dao层

public interface UserMapper extends Mapper<User> {
}

service层查询

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public User queryById(int id){
        return userMapper.selectByPrimaryKey(id);
    }
}

结果一直返回null,首先看一下通用mapper执行的语句。

在这里插入图片描述


可以看到并没有按照我们想象的那样,根据id进行查询。
罪魁祸首就是我们在定义实体类的时候,id的类型为int,他根本不能识别基本类型
改成Integer即可。int->Integer
注意:有时候直接改成Integer,还是失效。
那么我们就要实行曲线救国。
先改成Long,再改成Integer

你可能感兴趣的:(Mybatis)