MybatisPlus中selectById查询不到数据
时间: 2024-06-12 10:11:27 浏览: 215
MybatisPlus中selectById查询不到数据可能有以下两种情况:
1. 实体类中的id和数据库中的表的id不相对应,需要加上 @TableId(value = “数据库中的id”, type = IdType.ASSIGN_ID)注解。
2. 其他字段,比如逻辑删除isDeleted在建表的时候没有设置成非空默认值为0,加上@TableLogic注解后,也会出现数据查询不到的结果。
相关问题
mybatisplus selectByid
MyBatis-Plus是一个基于MyBatis的增强工具,可以帮助我们更方便地使用MyBatis进行数据库操作。它提供了一些常用的方法,如selectById方法。该方法用于根据主键id查询数据库表中的一条记录。它是由MyBatis-Plus库自带的,无需我们手动编写sql语句。通过调用selectById方法,MyBatis-Plus会自动生成对应的sql语句,执行查询,并返回查询结果。
需要注意的是,有时候我们可能会遇到查询不到数据的情况。这可能是由于MyBatis-Plus自动生成的sql语句中,where后面拼接的是"where null = ?"这样的条件导致的。在这种情况下,我们需要检查数据库中是否存在对应的数据,或者检查是否正确传入了查询条件。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Mybatis-Plus](https://blog.csdn.net/weixin_53089304/article/details/126173782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [关于使用Mybatisplus自带的selectById和insert方法时的一些问题](https://download.csdn.net/download/weixin_38631960/12821048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Mybatis Plus使用selectById](https://blog.csdn.net/qq_47905231/article/details/123592863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
@GetMapping("/selectAllId/${id}") 使用mybatisplus根据id查询数据
假设你的实体类为 `User`,你可以按照以下步骤使用 Mybatis-Plus 进行根据 id 查询数据:
1. 在 `UserMapper` 接口中声明一个方法,使用 `@Select` 注解进行 SQL 语句的编写:
```java
@Select("SELECT * FROM user WHERE id = #{id}")
User selectById(@Param("id") Long id);
```
2. 在 `UserMapper` 接口中继承 `BaseMapper` 接口,使得 `selectById` 方法可以被 Mybatis-Plus 自动实现:
```java
public interface UserMapper extends BaseMapper<User> {
// selectById 方法会被自动实现
}
```
3. 在 `UserService` 中调用 `UserMapper` 中的 `selectById` 方法:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Long id) {
return userMapper.selectById(id);
}
}
```
4. 在 Controller 中使用 `@GetMapping` 注解进行请求映射,调用 `UserService` 中的 `getUserById` 方法并返回结果:
```java
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/selectAllId/{id}")
public User getUserById(@PathVariable("id") Long id) {
return userService.getUserById(id);
}
}
```
注意,在 `@GetMapping` 中,`{id}` 表示路径中的参数,使用 `@PathVariable` 注解将其绑定到 `id` 变量上。同时,路径中的变量应该用 `/` 区分开,不应该使用 `$` 符号。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)