【问题标题】:liferay-6.1 - Implement own serviceliferay-6.1 - 实现自己的服务
【发布时间】:2012-11-18 21:37:30
【问题描述】:

嘿,我已经和学生一起创建了自己的 service.xml。现在我想为学生添加我自己的 searchByName 方法。你能解释一下在 StudentLocalServiceImpl 中写什么吗?

 public class StudentLocalServiceImpl extends StudentLocalServiceBaseImpl {
/*
 * NOTE FOR DEVELOPERS:
  *
 */

public List<Student> getAll() throws SystemException {
    return studentPersistence.findAll();
}

public Student getStudentByName(String name) {
    return studentPersistence.
}

// 我创建了一种方法 getAll。
我需要另一种方法的帮助。
提前致谢。

【问题讨论】:

    标签: liferay-6


    【解决方案1】:

    您将首先在您定义的实体内的service.xml 中将其声明为“finder”元素。

    例如

    <finder name="Name" return-type="Student">
        <finder-column name="name" />
    </finder>
    

    return-type 也可以是 Collection,如果想要 List&lt;Student&gt; 作为返回类型,如果名称不是唯一的。

    <finder name="Name" return-type="Collection">
        <finder-column name="name" />
    </finder>
    

    您还可以为列声明比较运算符:

    <finder name="NotName" return-type="Collection">
        <finder-column name="name" comparator="!=" />
    </finder>
    

    通过在查找器上指定unique="true" 属性,查找器实际上也可以声明要在此关系上生成的唯一索引(将应用于数据库表):

    <finder name="Name" return-type="Student" unique="true">
        <finder-column name="name" />
    </finder>
    

    有了这个定义并在重新运行ant build-service 之后,studentPersistence 将包含新方法,这些方法使用在 xml 元素中找到的查找器的名称附加前缀:countBy、findBy、fetchBy、removeBy 等。

    最后,您的服务方法只需要包含以下内容(基于上述内容):

    public Student getStudentByName(String name) throws SystemException {
        return studentPersistence.findByName(name);
    }
    

    HTH

    【讨论】:

    • 非常感谢 Ray 它帮了我很多.. :)
    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多