IntroductionWhen searching in presentation, we always need pass a lot values which can be null to business layer, and business layer use this values to create query statement. It's so boring, we need judge every value whether it's null, and doing different thing. In Polyforms query, you just need define query statement and search object, system will do others for you. DetailsFirst, we want to search user by name, email and created time (from to due), so we created a UserSO: public class UserSO {
private String name;
private String email;
private Date createdTimeFrom;
private Date createdTimeDue;
...
getter and setter
}Second, we add query statement as a bean: <bean id="User.search" class="cn.muthos.polyforms.dao.jpa.query.StatementHolder">
<constructor-arg>
<value>
<![CDATA[select u from User u
where 1=1
/~ and u.name like {%name%} ~/
/~ and u.email like {%email%} ~/
/~ and u.createdTime >= {createdTimeFrom} ~/
/~ and u.createdTime < {createdTimeDue} ~/
order by u.userName
]]>
</value>
</constructor-arg>
</bean>In the statement, parameter value use {...} as holder, all holder should be search object's property. If value maybe null, you need surround with /~ and ~/, so system will remove it if value is null. Last, we add search method to UserRepository: @Searcher
List<User> search(UserSO userSO); The rule for mapping method with Query Statement is [name of Query Statement] = [name of Entity].[name of method]. you can specify the name of Query Statement in @Finder as @Finder("search"). It's time to use this method.
|