通过Liferay Service Builder在service.xml中生成实体时,通常会创建类似下面代码段的 finder 列:
<entity name="Person" local-service="true" >
<column name="personId" type="long" primary="true" />
<column name="firstName" type="String" />
<column name="lastName" type="String" />
<finder name="AnyName" return-type="Collection" >
<finder-column name="firstName" />
<finder-column name="lastName" />
</finder>
<entity>
这会创建一个finder方法,可以找到与 firstName 和 lastName 相匹配的实体。
如何创建一个可选的匹配任何列的finder。我需要生成类似于以下内容的SQL的东西:来自person的人,其中firstName =“firstname” OR lastName =“lastname”。
这可能吗?
答案 0 :(得分:0)
您可能希望使用DynamicQuery来使用“OR”语句。然后我能想到的另一种方式是
将其添加到service.xml
<finder name="FirstName" return-type="Collection" >
<finder-column name="firstName" />
</finder>
<finder name="LastName" return-type="Collection" >
<finder-column name="lastName" />
</finder>
这就是你PersonLocalServiceImpl
public List < Person > getPersonByAnyName(String firstName, String lastName) {
if (firstName != null && (lastName == null || lastName.equals("") )) {
return getPersonByFirstName(firstName);
}
if (lastName != null && (firstName == null || firstName.equals("") )) {
return getPersonByLastName(lastName);
}
return personPeristence.findByAnyName(firstName, lastName);
}
public List < Person > getPersonByFirstName(String firstName) {
// return the Collection from personPersistence.findByFirstName ...
}
public List < Person > getPersonByLastName(String lastName) {
// return the Collection from personPersistence.findByLastName ...
}
编辑:oops,这段代码不能用作OR语句,如果你的意思是使用其中一个Sting参数为null,我就可以工作