首页 > 代码库 > liferay 高级查询 (1)

liferay 高级查询 (1)

pointExchangeLocalServiceImpl.java 加入的代码

 /**

     * 积分管理中的查询方法

     * 

     * @param searchTerms

     * @param companyId

     * @param groupId

     * @param start

     * @param end

     * @return

     * @throws SystemException

     */




    //查询PointExchange表中所有记录

    @SuppressWarnings("unchecked")

    public List<PointExchange> findPointExchanges(PointSearchTerms searchTerms, long companyId, int start, int end) throws SystemException {

        DynamicQuery dynamicQuery = getPointExchangeDynamicQuery(searchTerms, companyId);

        return dynamicQuery(dynamicQuery, start, end);

    }


    /**

     * 积分管理中查询用户总数

     * 

     * @param searchTerms

     * @param companyId

     * @param groupId

     * @return

     * @throws SystemException

     */

    public int countPointExchange(PointSearchTerms searchTerms, long companyId) throws SystemException {

        DynamicQuery dynamicQuery = getPointExchangeDynamicQuery(searchTerms, companyId);

        return ((Long) dynamicQueryCount(dynamicQuery)).intValue();

    }


    private DynamicQuery getPointExchangeDynamicQuery(PointSearchTerms searchTerms, long companyId) {

        DynamicQuery dynamicQuery = pointExchangePersistence.createDynamicQuery();

        dynamicQuery.add(PropertyFactoryUtil.forName("companyId").eq(companyId));


        if (searchTerms.isAdvancedSearch()) {  //判断是否是高级查询

            //高级查询中的条件(按什么查询)

            String realNameLike = searchTerms.getRealNameLike();  

            Date submitDate = searchTerms.getSubmitDate();

            int confirmState = searchTerms.getConfirmState();

            Junction junction = null;

            //and运算

            if (searchTerms.isAndOperator()) {

                junction = RestrictionsFactoryUtil.conjunction();//连接运算,逻辑与

            } else {

                junction = RestrictionsFactoryUtil.disjunction();//逻辑或运算

            }

            if (Validator.isNotNull(realNameLike)) {

                String realNameLikeSql = DAOUtil.getLike(realNameLike); 

                junction.add(PropertyFactoryUtil.forName("realName").like(realNameLikeSql));//模糊查询

            }

            if (Validator.isNotNull(submitDate)) {

                Calendar cal = Calendar.getInstance();

                cal.setTime(submitDate);

                cal.add(Calendar.DATE, 1);

                Junction dateJunction = RestrictionsFactoryUtil.conjunction();

               //大于等于运算符ge,lt小于运算符,eq等于运算符

                dateJunction.add(PropertyFactoryUtil.forName("submitDate").ge(submitDate));

                dateJunction.add(PropertyFactoryUtil.forName("submitDate").lt(cal.getTime()));

                junction.add(dateJunction);

            }

            if (confirmState != -1) {

                junction.add(PropertyFactoryUtil.forName("confirmState").eq(confirmState));

            }

            dynamicQuery.add(junction);

        } else {

            String keywords = searchTerms.getKeywords(); //关键词搜索

            String realNameLike = keywords;

            if (Validator.isNotNull(realNameLike)) {

                String realNameLikeSql = DAOUtil.getLike(realNameLike);

                dynamicQuery.add(PropertyFactoryUtil.forName("realName").like(realNameLikeSql));

            }

        }


        return dynamicQuery;

    }


本文出自 “hexun.com” 博客,请务必保留此出处http://551735.blog.51cto.com/541735/1529975