通用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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章