Ign*_*son 10 java spring jsp jpa spring-boot
我一直在使用 JPA 在 Spring Boot Web 中编写一个程序,并且我正在使用查询来访问带有“contains”和“ignorecase”过滤器的一些数据,我之前在其他程序中已经这样做过,并且运行良好,但现在我遇到了这个错误,我完全迷失了,因为我在谷歌中找不到任何东西,我真的很深入地寻找它为什么会发生,到目前为止我还没有看到我的代码中有任何不合适的地方,声明的变量类型似乎没问题,但正如我所说,我迷失了。值得一提的是,由于某种原因,当我第一次在我的网站上执行查询时,一切正常,我得到了正确的结果等等,但是当我回到家并尝试另一个查询(甚至相同的查询)时,我会得到正确的结果。 )我收到错误。代码如下:
模型
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Serie {
@Id
@Column(columnDefinition = "NUMERIC(19,0)")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String red;
@Column(columnDefinition = "NUMERIC(19,0)")
private double rating;
Run Code Online (Sandbox Code Playgroud)
存储库
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import cl.desafiolatam.imdb.modelo.Serie;
public interface SerieRepository extends JpaRepository<Serie, Integer> {
public List<Serie> findByTitleContainingIgnoreCase(String title);
}
Run Code Online (Sandbox Code Playgroud)
服务
import cl.desafiolatam.imdb.vo.SerieVO;
public interface SerieService {
public SerieVO findByTitleContainingIgnoreCase(String title);
}
Run Code Online (Sandbox Code Playgroud)
服务实施
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cl.desafiolatam.imdb.dao.SerieRepository;
import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;
@Service
public class SerieServiceImpl implements SerieService {
private static final Logger logger = LoggerFactory.getLogger(SerieServiceImpl.class);
@Autowired
SerieRepository dao;
SerieVO respuesta;
@Override
@Transactional(readOnly = true)
public SerieVO findByTitleContainingIgnoreCase(String title) {
respuesta = new SerieVO("Ha ocurrido un error!", "104", new ArrayList<Serie>());
try {
List<Serie> serie = dao.findByTitleContainingIgnoreCase(title);
System.out.println(serie);
if(serie.size() > 0) {
respuesta.setSeries(serie);
respuesta.setMensaje("Se ha encontrado el registro");
respuesta.setCodigo("0");
} else {
respuesta.setMensaje("No se ha encontrado el registro");
respuesta.setCodigo("104");
}
} catch (Exception e) {
logger.error("Error al buscar la serie", e);
}
return respuesta;
}
}
Run Code Online (Sandbox Code Playgroud)
视觉对象
import java.util.List;
import cl.desafiolatam.imdb.modelo.Serie;
public class SerieVO extends GenericVO {
List<Serie> series;
public SerieVO(String mensaje, String codigo, List<Serie> series) {
super(mensaje, codigo);
this.series = series;
}
public SerieVO() {
super();
}
Run Code Online (Sandbox Code Playgroud)
控制器
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;
@Controller
public class SerieController {
private final static Logger logger = LoggerFactory.getLogger(SerieController.class);
@Autowired
private SerieService svc;
@GetMapping("/buscarSerie")
public ModelAndView buscarSerie(Model model, @RequestParam String nombreSerie) {
SerieVO respuestaServicio = new SerieVO();
respuestaServicio.setMensaje("No se ha encontrado la serie");
try {
respuestaServicio = svc.findByTitleContainingIgnoreCase(nombreSerie);
model.addAttribute("listaSeries", respuestaServicio.getSeries());
return new ModelAndView("resultadoserie");
} catch (Exception e) {
logger.error("Error al buscar la serie", e);
}
return new ModelAndView("redirect:/user");
}
}
Run Code Online (Sandbox Code Playgroud)
搜索输入
<div class="d-flex justify-content-center pb-2">
<div class="container row">
<div class="col-md-4">
<div class="d-flex justify-content-center">
<h2>Buscar serie</h2>
</div>
</div>
<div class="col-md-8">
<form action="buscarSerie" method="get">
<div class="row g-2">
<div class="col-md">
<div class="form-floating">
<input type="text" class="form-control" id="floatingInputGrid"
value="" name="nombreSerie" required> <label
for="floatingInputGrid">Serie</label>
</div>
</div>
</div>
<div class="d-flex justify-content-center pt-4">
<input type="submit" class="btn m-2 btn-dark" value="Buscar" />
</div>
</form>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
正如我所说,我真的迷失了,到处研究,并检查了我上一个项目中的代码,我只是不明白为什么这个让我这么肮脏。一开始就不会失败,它给了我一线希望,当我想重试时,它又粉碎了那一点点希望。:)
我尝试删除我的代码并从我知道它按预期工作的项目中复制和粘贴,更改变量和参数。名称以使其适用于新程序,但不起作用。进行了并排比较,尝试使用 @Query 编写特定指令。寻找信息。仅使用“包含”过滤器,但没有任何效果。
| 归档时间: |
|
| 查看次数: |
13375 次 |
| 最近记录: |