Windchill 高級查詢怎麼寫1=1

Windchill 高級查詢之查詢條件動態變化寫法

本文主要陳述在高級搜索業務場景,定義後臺查詢方法時需要考慮:
- 查詢條件支持動態擴展
- 定義高級查詢時需要明確定義是否需要查出該對象的子類
- 查詢方法是否需要忽略權限
- 查詢方法儘量支持批量查詢,減少數據庫與服務器的交互
- 查詢條件能命中索引的寫在最後面,篩選結果集的放在where裏面
- 儘量返回對象,調用的地方通過對象獲取相關屬性

舉個栗子
代碼示例


public static List<WTPart> queryItemByNumberAndState(String ipNumber, String state)
                            throws WTPropertyVetoException, WTException {
                        List<WTPart> itemResult = new ArrayList<WTPart>();
                        if (!RemoteMethodServer.ServerFlag) {
                            try {
                                Class[] argType = { String.class, String.class };
                                Object[] arg = { ipNumber, state };
                                return (List) RemoteMethodServer.getDefault().invoke("queryItemByNumberAndState",
                                        GPPtoSAPInfoHelper.class.getName(), null, argType, arg);
                            } catch (Exception e) {
                                //TODO
                            }
                        } else {
                            boolean isAccess = SessionServerHelper.manager.isAccessEnforced();
                            try {
                                if (isAccess) {
                                    SessionServerHelper.manager.setAccessEnforced(false);
                                }
                                QuerySpec qs = new QuerySpec(WTPart.class);
                                qs.setAdvancedQueryEnabled(true);
                                qs.getFromClause().setAliasPrefix("A");

                                // 高級查詢動態拼接SQL,第一句先寫where 1=1 方便後面動態擴展寫其他條件
                                qs.appendWhere(new SearchCondition(new ConstantExpression("1"), SearchCondition.EQUAL,
                                        new ConstantExpression("1")));
                                if (StringUtils.isNotBlank(ipNumber)) {
                                    qs.appendAnd();
                                    qs.appendWhere(new SearchCondition(WTPart.class, WTPart.NUMBER, SearchCondition.EQUAL, ipNumber));
                                }
                                if (StringUtils.isNotBlank(state)) {
                                    qs.appendAnd();
                                    qs.appendWhere(new SearchCondition(WTPart.class, WTPart.STATE, SearchCondition.EQUAL,
                                            state.toUpperCase().trim()));
                                } else {
                                    qs.appendAnd();
                                    qs.appendWhere(new SearchCondition(WTPart.class, WTPart.STATE, SearchCondition.EQUAL, "RELEASED"));
                                }
                                QueryResult qr = PersistenceHelper.manager.find(qs);
                                while (qr.hasMoreElements()) {
                                    WTPart itemInfo = (WTPart) qr.nextElement();
                                    itemResult.add(itemInfo);
                                }
                            } catch (WTException e) {
                                logger.error(e);
                            } finally {
                                SessionServerHelper.manager.setAccessEnforced(isAccess);
                            }
                    }
                    return itemResult;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章