Springデータjpaと仕様を使用して、spring mvcでフィルタ/検索機能を実装する必要があります。バックエンドは、基本的にフロントエンドのフィルタを表すいくつかのフィールドを持つBeanであるオブジェクト(ReportTemplateBean)を受け取ります。SpringデータJPAの動的クエリ。リファクタリング
public class ReportTemplateBean implements Serializable {
private static final long serialVersionUID = -3915391620260021813L;
private Long id;
private String property;
private String city;
private String state;
private String zipCode;
private String propertyStatus;
private String realEstateRep;
//more code
私たちは、コントローラ
@RequestMapping(value = "/search", method = RequestMethod.GET)
@ResponseBody
public ReportBean search(@AuthenticationPrincipal ActiveUser activeUser,
@ModelAttribute("templateForm") ReportTemplateBean template,
Pageable pageable) throws GenericException {
LOGGER.info("Pulling report requested");
ReportBean report = reportService.searchProperties(template,
pageable.getPageNumber(), pageable.getPageSize());
return report;
}
サービス
@Override
@Transactional(readOnly = true, timeout = 20)
public ReportBean searchProperties(ReportTemplateBean template,
Integer pageNumber, Integer pageSize) throws GenericException,
TransactionTimedOutException {
LOGGER.info("searchProperties({})", template);
try {
// pageNumber = (pageNumber == null ? 0 : pageNumber);
// pageSize = (pageSize == null ? 10 : pageSize);
ReportTemplate t = reportTemplateMapper.beanToEntity(template);
List<PropertyBean> beans = new ArrayList<PropertyBean>();
PropertySpecification spec = new PropertySpecification(t);
Page<Property> properties = propertyRepository.findAll(spec,
new PageRequest(pageNumber, pageSize, Sort.Direction.ASC,
"name"));
を持っているし、それが動的にクエリを構築しますが、私はそれを好きではないIF長鎖を使用。これが仕様です。あなたが仕様に気づくことができたよう
@SuppressWarnings("unchecked")
@Override
public Predicate toPredicate(Root<Property> root, CriteriaQuery<?> query,
CriteriaBuilder cb) {
Path<String> propertyName = root.get(Property_.name);
Path<String> city = root.get(Property_.city);
Path<String> state = root.get(Property_.state);
Path<String> zipCode = root.get(Property_.zipCode);
final List<Predicate> orPredicates = new ArrayList<Predicate>();
final List<Predicate> andPredicates = new ArrayList<Predicate>();
if (template.getProperty() != null
&& template.getProperty().length() > 0) {
andPredicates.add(cb.equal(propertyName, template.getProperty()));
}
if (template.getCity() != null && template.getCity().length() > 0) {
andPredicates.add(cb.equal(city, template.getCity()));
}
if (template.getState() != null && template.getState().length() > 0) {
andPredicates.add(cb.equal(state, template.getState()));
}
if (template.getZipCode() != null && template.getZipCode().length() > 0) {
andPredicates.add(cb.equal(zipCode, template.getZipCode()));
}
if (template.getRealEstateRep() != null) {
Join<Property, User> pu = null;
if (query.getResultType().getName().equals("java.lang.Long")) {
pu = (Join<Property, User>) root.fetch(Property_.createdBy);
} else {
pu = root.join(Property_.createdBy);
}
Path<Long> userId = pu.get(User_.id);
andPredicates.add(cb.equal(userId, template.getRealEstateRep()));
}
if (template.getProjectType() != null
&& template.getProjectType().length() > 0) {
Join<Property, Project> pp = null;
if (query.getResultType().getName().equals("java.lang.Long")) {
pp = root.join(Property_.projects);
} else {
pp = (Join<Property, Project>) root.fetch(Property_.projects);
}
Path<String> projectType = pp.get(Project_.projectName);
andPredicates.add(cb.equal(projectType, template.getProjectType()));
}
//more IF's
return query.getRestriction();
}
は醜いようで、そのSONARのほかに(良い)このメソッドの循環的複雑度について不平を言います。
質問ですが、どうすれば仕様(IF)をより多くのOOコードにリファクタリングできますか。 ありがとうございます。 UPDATE - 私はSpring Data JPA(Query by Example)の新機能のようなものを使用/実装したいと思います。ExampleMatcherクラスは、Beanフィールドのnull値をほとんど無視しているようですを探しています。 null値と空の値は無視します。
仕様のみを使用してソリューションをお探しですか?私は同様の問題を抱えていましたが、私はリポジトリにオプションのパラメータを使って@Queryアノテーションを使ってクエリを書くことで解決しました – amicoderozer
仕様を使用していいですが、必須ではありません。 – TheProgrammer