--Afficher les résultats de la recherche sur l'écran de recherche de liste
--Ajouter pour hériter *** JpaSpecificationExecutor *** dans l'interface du référentiel à rechercher
testRepository
@Repository
public interface TestRepository extends JpaRepository<TestEntity, Integer>, JpaSpecificationExecutor<TestEntity> {
}
--Créer des conditions de recherche
testSpecifications
/**
	 *Si la catégorie de sujet existe dans la condition de recherche, ajoutez-la à la condition where
	 * @param anken_kbn
	 * @return
	 */
	public Specification<TestEntity> ankenKbnContains(String anken_kbn) {
		if (Util.stringIsEmpty(anken_kbn))
			return null;
		else
			return new Specification<TestEntity>() {
				@Override
				public Predicate toPredicate(Root<TestEntity> root, CriteriaQuery<?> query,
						CriteriaBuilder cb) {
					return cb.like(root.get("anken_kbn"), "%" + anken_kbn + "%");
				}
			};
	}
--Cette partie crée une recherche de correspondance partielle avec l'élément anken_kbn
testDao
@Override
	public List<TestEntity> findBySearchItem(TestRepository testRepository,TestForm form) {
		TestSpecifications spec = new TestSpecifications();
		List<TestEntity> result = TestRepository.findAll(Specifications.where(spec.ankenKbnContains(form.getAnken_kbn()))
				.and(spec.ankenNameContains(form.getAnken_name()))
				.and(spec.reaseCampanyNameContains(form.getRease_campany_name()))
				);
		System.out.println("Résultats de recherche");
		System.out.println(result);
		return result;
	}
Specifications.where(spec.ankenKbnContains(form.getAnken_kbn()))
				.and(spec.ankenNameContains(form.getAnken_name()))
				.and(spec.reaseCampanyNameContains(form.getRease_campany_name()))
--Comme mentionné ci-dessus, les conditions de recherche en chaîne avec et conditions, etc.
--Dans testListForm, placez le résultat de la recherche sous forme de liste à renvoyer à l'écran --testDao est injecté --testRepository est injecté
testControleller
	@RequestMapping("/search")
	public ModelAndView search(@ModelAttribute("testListForm") TestListForm contractListForm,
			ModelAndView mav) {
		mav.setViewName("index");
		Iterable<TestEntity> testEntityList = testDao.findBySearchItem(testRepository, testListForm);
		
		System.out.println("Résultats de recherche");
		System.out.println(tstEntityList);
		
		mav.addObject("contractList", hdrEntityList);
		mav.addObject("contractListF", new TestListForm());
		return mav;
	}
--Création d'une clause where variable en tâtonnant «Est-ce une bonne façon d'écrire? Il y a peut-être une meilleure façon d'écrire que je n'ai pas.
Recommended Posts