[英]How to filter based on something other than ID
我正在尝试使用 mysql 数据库构建一个非常小的 REST API。 我正在使用 Spring boot 和 Tomcat 在本地部署它。 我正在处理的一项服务是从数据库中检索消息。 我能够获取从数据库创建的所有消息,并且能够基于 ID 进行过滤,但我也希望能够基于其他参数进行过滤。 其他参数称为“messageCreated”和“messageSubscribed”,它们都是 int 参数。
这是我的控制器类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.NoSuchElementException;
@RestController
@RequestMapping("/messages")
public class MessageController {
@Autowired
MessageService messageService;
@GetMapping("")
public List<Message> list() {
return messageService.listAllMessage();
}
@GetMapping(value ={"/id"})
public ResponseEntity<Message> get(@PathVariable Integer id) {
try {
Message message = messageService.getMessage(id);
return new ResponseEntity<Message>(message, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<Message>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/")
public void add(@RequestBody Message message) {
messageService.saveMessage(message);
}
@PutMapping("/{id}")
public ResponseEntity<?> update(@RequestBody Message message, @PathVariable Integer id) {
try {
Message existMessage = messageService.getMessage(id);
message.setId(id);
messageService.saveMessage(message);
return new ResponseEntity<>(HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Integer id) {
messageService.deleteMessage(id);
}
}
如果您使用的是 Spring 数据,则使用 @Query 过滤。 更多示例可在以下网址中找到。 https://www.baeldung.com/spring-data-jpa-query
listAllMessage()
的结果,我们说buildMsgsFromWanted
List<Message> buildMsgsFromWanted(List<Message> allResults){
// there should be some codes to check the validation of allResults, like ifEmpty() or something like this. then
List<Message> createdMsgs = allResult.stream().filter((item)->item.messageCreated()).collect(Collectors.toList());
return createdMsgs;
}
selectAllMessages()
应该是这样的:List<Message> selectAllMessages(Message filter);
在映射器 xml 文件中,sql 应该是这样的:
<select id="selectAllMessages" parameterType="XXX.XXX.XXX.Message" resultMap="messageMap">
select m.fieldA, m.fieldB, m.filedC, ...., m.is_created, m.is_subscribed from message m
<where>
<if test="messageCreated!=null"> and m.is_created=#{messageCreated}</if>
<if test="messageSubscribed!=null"> and m.is_subscribed=#{messageSubscribed}</if>
</where>
</select>
因此,您传递给selectAllMessage
的参数message
中的messageCreated
和messageSubscribed
是否为 null 决定了结果,或者实际上是执行的 sql。 只需调用 select 方法,如:
Message queryObj = new Message();
queryObj.setCreated(null);
queryObj.setSubscribed(1);
mapper.selectAllMessage(queryObj);
那么您将获得订阅的消息,因为 xml 文件中的参数if test="messageSubscribed!=null"
为 true 并且where m.is_subscribed=1
将附加到查询 sql 中。缺点是您必须维护所有参数作为过滤器可能基于的Message
类中的字段。
在其他的dao框架中也是一样的,只是控制最后传给sql执行的2个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.