@Override public Result add(Article article) { List<Words> wordsList = wordsService.selectAllWords(); if (wordsList != null && wordsList.size() > 0){ for (Words d:wordsList) { if (article.getContent().contains(d.getWords())){ return ResultGenerator.genFailResult("留言包含敏感词,请注意您的言论。"); } } }给我讲讲代码
时间: 2024-04-29 20:19:56 浏览: 15
这段代码是一个Java方法,其名称为“add”,接收一个“Article”类型的参数,并返回一个“Result”类型的结果。在方法中,首先从数据库中查询所有的敏感词,并将其存储在“wordsList”列表中。接着判断“wordsList”是否为空且有元素,如果是,则遍历“wordsList”中的每个敏感词,并判断文章内容中是否包含这个敏感词。如果包含,则返回一个包含错误信息的失败结果,否则会继续执行后面的代码。这个方法的作用是在添加文章时检查文章内容是否包含敏感词,如果包含则禁止添加。
相关问题
@Override public List<User> list(User user) { List<User> list = new ArrayList<>(); LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Collect::getUserId,user.getId()); List<Collect> collects = collectMapper.selectList(wrapper); if (collects != null && collects.size() > 0){ for (Collect collect : collects) { Question question = questionService.getById(collect.getQuestionId()); collect.setQuestion(question); } } return null; }我的代码
好的,这里是修改后的代码,你可以参考一下:
```java
@Override
public List<Collect> list(User user) {
List<Collect> collects = new ArrayList<>(); // 修改:返回Collect对象的集合
LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Collect::getUserId, user.getId());
collects = collectMapper.selectList(wrapper); // 修改:将查询结果赋值给collects集合
if (collects != null && collects.size() > 0) {
for (Collect collect : collects) {
Question question = questionService.getById(collect.getQuestionId());
collect.setQuestion(question);
}
}
return collects; // 修改:返回封装好的Collect对象集合
}
```
主要修改点如下:
1. 将返回值类型从List<User>修改为List<Collect>,因为该方法的作用是查询指定用户收藏的所有问题,并将问题信息封装到Collect对象中返回;
2. 将返回值赋值为一个空的Collect对象集合;
3. 修改return语句,返回封装好的Collect对象集合。
这样修改后,该方法就可以正常查询指定用户收藏的所有问题并返回封装好的Collect对象集合了。
List<ProtocolActualExecutionInfo> protocolActualExecutionInfos = protocolActualExecutionInfoMapper.selectByExample(protocolActualExecutionInfoExample); List<ProtocolDetailInfo> protocolDetailInfos = protocolDetailInfoMapper.selectByExample(protocolDetailInfoExample);根据ProtocolActualExecutionInfo中的dlcgid+dlcgxyid与ProtocolDetailInfo中的dlcgid+dlcgxyid匹配合并成一个list
可以使用Java8的Stream API来实现这个需求,具体代码如下:
```
List<ProtocolActualExecutionInfo> protocolActualExecutionInfos = protocolActualExecutionInfoMapper.selectByExample(protocolActualExecutionInfoExample);
List<ProtocolDetailInfo> protocolDetailInfos = protocolDetailInfoMapper.selectByExample(protocolDetailInfoExample);
List<ProtocolInfo> protocolInfos = Stream.concat(
protocolActualExecutionInfos.stream().map(info -> new ProtocolInfo(info.getDlcgid(), info.getDlcgxyid())),
protocolDetailInfos.stream().map(info -> new ProtocolInfo(info.getDlcgid(), info.getDlcgxyid()))
)
.distinct()
.collect(Collectors.toList());
```
其中,`ProtocolInfo`是一个简单的POJO类,表示`dlcgid`和`dlcgxyid`的组合:
```
public class ProtocolInfo {
private String dlcgid;
private String dlcgxyid;
public ProtocolInfo(String dlcgid, String dlcgxyid) {
this.dlcgid = dlcgid;
this.dlcgxyid = dlcgxyid;
}
// 省略getter和setter方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProtocolInfo that = (ProtocolInfo) o;
return Objects.equals(dlcgid, that.dlcgid) &&
Objects.equals(dlcgxyid, that.dlcgxyid);
}
@Override
public int hashCode() {
return Objects.hash(dlcgid, dlcgxyid);
}
}
```
这里使用了`Stream.concat()`方法将两个Stream合并起来,然后使用`distinct()`方法去重,最后使用`Collectors.toList()`方法将结果收集到一个List中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)