[英]How to search by other thing that not is Id in Spring Boot with MongoDB Repository
我正在開發一個應用程序,該應用程序要按名稱搜索城市。從中提取信息的數據庫是MongoDB數據庫,其屬性為ID,城市,位置,人口和州。 我有以下實體:
public class Cities {
@Id
private String _id;
private String city;
private String[] loc;
private String pop;
private String state;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
String getLocation() {
return loc[0] + " " + loc[1];
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String[] getLoc() {
return loc;
}
public void setLoc(String[] loc) {
this.loc = loc;
}
public String getPop() {
return pop;
}
public void setPop(String pop) {
this.pop = pop;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "Cities{" +
"_id='" + _id + '\'' +
", city='" + city + '\'' +
", loc=" + Arrays.toString(loc) +
", pop='" + pop + '\'' +
", state='" + state + '\'' +
'}';
}
}
我的控制器如下所示:
import com.sbmongo.starbucks.Constant.ViewConstant;
import com.sbmongo.starbucks.Entity.Cities;
import com.sbmongo.starbucks.Repository.CitiesRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/starbucks")
public class CitiesController {
@Autowired
@Qualifier("citiesRepository")
private CitiesRepository citiesRepository;
@GetMapping("/cities")
public ModelAndView listCities(){
ModelAndView mav = new ModelAndView(ViewConstant.CITIES);
mav.addObject("cities", citiesRepository.findAll());
return mav;
}
@GetMapping("/searchCity")
public ModelAndView searchCity(@RequestParam(name = "city", required = false)String city){
ModelAndView mav = new ModelAndView(ViewConstant.CITIES);
mav.addObject("cities", citiesRepository.findByCity(city));
return mav;
}
}
我的資料庫:
import java.util.List;
@Repository("citiesRepository")
public interface CitiesRepository extends MongoRepository<Cities, String> {
public Cities findBy_id(ObjectId _id);
public Cities findByCity(String city);
}
以及我使用Thymeleaf的HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
<title>Starbucks</title>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>City</th>
<th>Location</th>
<th>Population</th>
<th>State</th>
</tr>
</thead>
<tbody>
<tr th:each="city : ${cities}">
<td th:text="${city._id}"></td>
<td th:text="${city.city}"></td>
<td th:text="${#strings.arrayJoin(city.loc, ' ')}"></td>
<td th:text="${city.pop}"></td>
<td th:text="${city.state}"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="../js/jquery-3.3.1.min.js"></script>
<script src="../js/popper.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
</body>
</html>
我想按城市名稱搜索城市,我從視圖中發送城市名稱,然后在Controller中接收它,但看起來我的存儲庫中的findByCity()方法不起作用。 我怎么解決這個問題?
findByCity()應該返回一個列表try:
public List<Cities> findByCity(String city);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.