find方法(条件查询)

find
public org.joget.apps.form.model.FormRowSet find(org.joget.apps.form.model.Form form, final java.lang.String condition, final java.lang.Object[] params, final java.lang.String sort, final java.lang.Boolean desc, final java.lang.Integer start, final java.lang.Integer rows)
find
public org.joget.apps.form.model.FormRowSet find(java.lang.String formDefId, java.lang.String tableName, final java.lang.String condition, final java.lang.Object[] params, final java.lang.String sort, final java.lang.Boolean desc, final java.lang.Integer start, final java.lang.Integer rows)

参数详解:

condition:可用于写where条件,系统会将 condition 拼接在 sql中

params: condition中可以用 ?代替参数,params中按顺序写入实际参数

sort: 排序字段

desc: 布尔类型,true表示 DESC排序

start: 分页,setMaxResults(start) ,不填默认为 0

rows: 分页, setMaxResults(rows)       query.setFirstResult(0),query.setMaxResults(4);相当于MySQL中的limit 0, 4;

源码查看

	protected FormRowSet internalFind(final String entityName, final String tableName, final String condition, final Object[] params, final String sort, final Boolean desc, final Integer start, final Integer rows) {
        // get hibernate template
        Session session = getHibernateSession(tableName, tableName, null, ACTION_TYPE_LOAD);

        try {
            String query = "SELECT e FROM " + tableName + " e ";
            if (condition != null) {
                query += condition;
            }

            if ((sort != null && !sort.trim().isEmpty()) && !query.toLowerCase().contains("order by")) {
                String sortProperty = sort;
                if (!FormUtil.PROPERTY_ID.equals(sortProperty) && !FormUtil.PROPERTY_DATE_CREATED.equals(sortProperty) && !FormUtil.PROPERTY_DATE_MODIFIED.equals(sortProperty)
                         && !FormUtil.PROPERTY_CREATED_BY.equals(sortProperty)  && !FormUtil.PROPERTY_CREATED_BY_NAME.equals(sortProperty)
                         && !FormUtil.PROPERTY_MODIFIED_BY.equals(sortProperty)  && !FormUtil.PROPERTY_MODIFIED_BY_NAME.equals(sortProperty)) {
                    Collection<String> columnNames = getFormDefinitionColumnNames(tableName);
                    if (columnNames.contains(sort)) {
                        sortProperty = FormUtil.PROPERTY_CUSTOM_PROPERTIES + "." + sort;
                    }
                }
                query += " ORDER BY cast(e." + sortProperty + " as string)";

                if (desc) {
                    query += " DESC";
                }
            }
            Query q = session.createQuery(processQuery(query));

            int s = (start == null) ? 0 : start;
            q.setFirstResult(s);

            if (rows != null && rows > 0) {
                q.setMaxResults(rows);
            }

            if (params != null) {
                int i = 0;
                for (Object param : params) {
                    q.setParameter(i, param);
                    i++;
                }
            }

            Collection result = q.list();

            FormRowSet rowSet = new FormRowSet();
            rowSet.addAll(result);
            return rowSet;
        } finally {
            closeSession(session);
        }
    }
  • No labels