[英]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.