亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - 復(fù)雜的字段條件判斷解析
PHPz
PHPz 2017-04-18 10:52:25
0
3
618

應(yīng)用場景簡化描述

現(xiàn)在有個用戶Person,包含int age, BigDecimal money,java.util.Date accessTime等字段,對應(yīng)于用戶表person:

年齡(age : int) 資產(chǎn) (money : number) 入網(wǎng)日期 (accessTime : date)
20 100.00 2017-03-24
20 150.00 2016-05-07
21 300.00 2015-04-03
21 240.00 2015-07-15
22 300.00 2014-12-21
21 300.00 2014-12-21

另外,有一張條件表,condition:

字段名(fieldName : varchar) 運算符(oper : varchar) 閾值 (threshold : varchar )
age = 21
money > 280.05
accessTime > 2015-05-31

條件表condition用來配置過濾用戶person的條件,表示要過濾出

  • 年齡等于21歲

  • 資產(chǎn)大于280.05

  • 入網(wǎng)日期在 2015-06-01之后

的所有用戶。

其中,oper可取的值有 = , < , > , >= , <= , in , between

如果oper為inbetween, 則閾值為多個,用逗號隔開。

問題

現(xiàn)在,對于不同的字段,在條件表condition里都是varchar類型,而在person表中,卻有不同的類型。

而且,條件表里的條件是從web系統(tǒng)頁面上,由用戶配置的;也就是說,條件的個數(shù)不確定,配置的字段,運算符,閾值也都是不確定的。

問: 如何才能使用java代碼,先將所有用戶以及條件查出來,然后遍歷每個用戶,對于每個用戶,遍歷每個條件,怎樣才能正確的判斷該用戶通過所有的條件檢查,得出通過條件篩選的用戶列表?(上邊的場景是實際場景簡化后的,請不要給出拼接sql,通過sql過濾的答案)


是不是可以把person的記錄生成xml文件,條件生成xsd文件,用xsd去校驗xml ??

PHPz
PHPz

學(xué)習(xí)是最好的投資!

reply all(3)
小葫蘆

If you don’t use SQL, you can consider using the chain of responsibility model to get all the data and put it in the linkedlist.
Then write a filter to filter the content in the collection. Each condition can be equivalent to a filter

劉奇

How big is your user table? Find out all of it.

The formal method is to use sql to check. . .

A more advanced approach should be to customize the domain-specific language and then convert it to sql. It is impossible to use xml filtering anyway.

左手右手慢動作

I feel better about youcondition表沒什么用???僅僅是存儲數(shù)據(jù)?如果臨時的直接用json傳過來就好了,如果你要持久存儲的話,起碼要個admin_id什么的,來標(biāo)記下是誰的篩選條件。感覺效率不如直接扔到redis.

See you are using Java, but I am used to using PHP, so I will briefly describe my idea using PHP...

Write oneFilter類處理condition轉(zhuǎn)換為sql.

class Filter {
    public function doFilter() {
        //1. 獲取登錄的admin_id
        //2. 獲取篩選條件
        /**
         * 數(shù)據(jù)格式例子為: [
         *    'field' => 'age';, 
         *    'type'  => 'compare',
         *    'ext'   => '>',
         *    'value' => '2'
         * ]
         */
        foreach($conditions as $condition) {
            // 根據(jù)type獲取對應(yīng)的$conditionHandle
            $conditionHandle::parse($condition);
        }
    }
}

// Condition接口
interface Condition {
    // 解析數(shù)據(jù),返回sql片段和需要填充的數(shù)據(jù)(此處使用PDO預(yù)處理,減少SQL注入)
    static function parse($data);
}

// ConditionHandle
namespace Condition;
class Compare implements Condition {
    // 執(zhí)行流程
    public static function parse($data) {
        ...
        return [
            'sql' => 'age > ?',
            'params' => [ 2 ]
        ];
    }
}

Handwritten, please ignore grammatical errors...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template