@GetMapping("/selectAllId/${id}") 使用mybatisplus根据id查询数据
时间: 2024-03-05 22:51:09 浏览: 173
假设你的实体类为 `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` 变量上。同时,路径中的变量应该用 `/` 区分开,不应该使用 `$` 符号。
阅读全文
相关推荐


















