kooyee ‘s blog

          開源軟件, 眾人努力的結(jié)晶, 全人類的共同財(cái)富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          因?yàn)閖asper server中Multi-Select控件得到的值不是String,,而是Collection。這些值可以顯示在report中或者打印出來。但是當(dāng)Multi-Select中的值用到sql query中必須是String,所以需要把它轉(zhuǎn)換成String list.

          先建立一個(gè)parameter,類型為java.util.Collection。可以設(shè)定 default value 為
          Arrays.asList(new Integer[] {Integer.valueOf(1), Integer.valueOf(2) } )
          或者
                Arrays.asList(new String[] {"entry1", "entry2" })

          然后建立另一個(gè)parameter,類型為String。defaultValueExpression為下面其中的一個(gè)
          $P{MultiTaskInput}.toString().replaceAll("[\\[\\]]""")//for integer
          或者
          $P
          {MultiTaskInput}.toString().replaceAll("[\\[\\]]""'").replaceAll("""', '")//for String(char in database) 把 , 換成 ' , '
          **因?yàn)閏ollection的String格式為 [1],[2],[3]。所以到把方括號(hào)替換掉。

          把第二個(gè)parameter帶入query中,SQL為
          SELECT * FROM TABLE WHERE cloumn in ($P!{param})  //注意 $P后要加!
          這樣就可以建立動(dòng)態(tài)query, 數(shù)據(jù)庫運(yùn)行select基于parameter中l(wèi)ist的值。 類似于
          SELECT * FROM table WHERE column IN ('1','3','5')




          關(guān)于$P!{}的原文解釋:

          The $P{} syntax is used for introducing parameter values just like you use the ? in a JDBC prepared statement. They are placeholders for prepared statement parameters.

          The $P!{} simply expands the value of the parameter within the query text. It is used for dynamically building queries or for providing the whole query text as a parameter.

          The $X{} syntax is a new addition in JR 1.3.2 and is something in between the previous two. It is a way to dynamically build a part of the query, but also introduce parameter placeholders.
          There are two $X{} functions supported by the SQL query executer in JasperReports:

          $X{IN, colName, paramName}

          and

          $X{NOTIN, colName, paramName}

          paramName should be the name of a report parameter that is either of type Collection or array.

          This syntax is to be used in order to produce these SQL sequences in the WHERE clause:

          colName IN {val1, val2, ...}

          or

          colName NOT IN {val1, val2, ...}

          where val1, val2, ... are values in the collection or array parameter mentioned.

          Check the supplied /demo/sample/query for a working example of this.

          主站蜘蛛池模板: 滦平县| 大理市| 杨浦区| 封丘县| 桦南县| 台东县| 澄江县| 南陵县| 中牟县| 宜昌市| 灵台县| 牙克石市| 成武县| 射洪县| 大厂| 泊头市| 花莲县| 合作市| 日喀则市| 灵川县| 晋城| 三河市| 昆明市| 南丰县| 吐鲁番市| 观塘区| 定陶县| 武威市| 文山县| 沽源县| 叙永县| 民乐县| 汾阳市| 阳谷县| 涟水县| 阜宁县| 武隆县| 彰化市| 宣化县| 肥乡县| 苗栗县|