mybatis plus 查找个人用户的全部文章的controllo代码
时间: 2023-04-08 16:03:33 浏览: 61
以下是一个使用 MyBatis Plus 查找个人用户全部文章的控制器代码示例:
```java
@RestController
@RequestMapping("/articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping("/user/{userId}")
public List<Article> getUserArticles(@PathVariable Long userId) {
QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
return articleService.list(queryWrapper);
}
}
```
这个控制器会根据用户的 ID 查找该用户发布的所有文章。使用 MyBatis Plus 的 QueryWrapper 可以方便地构建查询条件。
相关问题
mybatis plus 查找个人用户的全部文章且分页的controllo代码
以下是一个示例的 Mybatis Plus 查找个人用户的全部文章且分页的控制器代码:
```java
@RestController
@RequestMapping("/articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping("/user/{userId}")
public Page<Article> getArticlesByUserId(@PathVariable Long userId,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
Page<Article> page = new Page<>(pageNum, pageSize);
QueryWrapper<Article> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
wrapper.orderByDesc("create_time");
return articleService.page(page, wrapper);
}
}
```
这段代码使用了 Mybatis Plus 提供的分页插件,通过传入用户 ID 和分页参数,查询该用户的所有文章并按照创建时间倒序排列返回分页结果。
mybatis plus 查找个人用户的全部文章的全部代码
以下是使用 Mybatis Plus 查找个人用户的全部文章的全部代码:
```java
public List<Article> findUserArticles(Long userId) {
QueryWrapper<Article> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
return articleMapper.selectList(wrapper);
}
```
其中,`Article` 是文章实体类,`articleMapper` 是 Mybatis Plus 自动生成的 Mapper 接口。以上代码会根据传入的 `userId` 查找该用户的所有文章并返回。