--Display search results on the list search screen --For the search condition, use the entered item, ignore it in the case of input, and narrow down the where clause
--Add to inherit *** JpaSpecificationExecutor *** in the Repository interface to be searched
testRepository
@Repository
public interface TestRepository extends JpaRepository<TestEntity, Integer>, JpaSpecificationExecutor<TestEntity> {
}
-(Supplement) Repository is a mechanism for managing Entity in a list
--Create search conditions --This method will be used later as a search condition --Specification class plays the role of search condition used in where clause
testSpecifications
/**
*If the matter category exists in the search condition, add it to the where condition
* @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 + "%");
}
};
}
--This part creates a partial match search with the item anken_kbn --Prepare methods for the conditions used by chaining with and and or ――Here, there are others
--Search conditions can be set in the method chain --At this time, the repository mentioned earlier is required, so define this method where it can be injected, or in this case, take the repository object injected into the controller as the first argument and use it. ing --The second argument Form holds the value passed from the screen.
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("search results");
System.out.println(result);
return result;
}
--Pass the object to be the search condition to the parameter of findAll method provided by repository interface.
Specifications.where(spec.ankenKbnContains(form.getAnken_kbn()))
.and(spec.ankenNameContains(form.getAnken_name()))
.and(spec.reaseCampanyNameContains(form.getRease_campany_name()))
--As mentioned above, chain search conditions with and conditions, etc. --spec is an object of specifications and uses the ankenKbnContains method created in that class. --The next form shows the value received from the screen. It becomes the character string of the search condition
--In testListForm, put the search result as a list to be returned to the screen. --testDao is injected --testRepository is injected --Use Dao. I just needed an instance of the repository class in Dao, so I'm passing testRepository
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("search results");
System.out.println(tstEntityList);
mav.addObject("contractList", hdrEntityList);
mav.addObject("contractListF", new TestListForm());
return mav;
}
--Created a variable where clause by groping ――Is this a good way to write? There may be a better way to write that I don't have.
Recommended Posts