kooyee ‘s blog

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

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

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

          把第二個parameter帶入query中,SQL為
          SELECT * FROM TABLE WHERE cloumn in ($P!{param})  //注意 $P后要加!
          這樣就可以建立動態(tài)query, 數(shù)據(jù)庫運行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.

          主站蜘蛛池模板: 南乐县| 顺义区| 兰溪市| 玛曲县| 石柱| 新闻| 永川市| 平山县| 宜阳县| 江达县| 高雄市| 南丰县| 绥滨县| 磐石市| 孝义市| 临城县| 永登县| 凤庆县| 开平市| 鄢陵县| 新绛县| 苗栗县| 黑山县| 军事| 双桥区| 邯郸县| 华蓥市| 介休市| 婺源县| 红河县| 平邑县| 伊通| 陈巴尔虎旗| 临安市| 四会市| 鹤庆县| 嘉黎县| 敦煌市| 海安县| 朝阳市| 扎鲁特旗|