2015-05-22 39 views
2

我使用Java 1.7, IBatis and postgreSQL如何在IBatis的選擇查詢中發送列表?

我有一個StudentVO

import java.util.List; 
    public class StudentVO { 

     private Long studentId; 
     private String studentName; 
     private List<Long> studentFriendNums; 

     //getters and setters 
    } 

Function在PostgreSQL是get_party_details(VARIADIC bigint[])

CREATE OR REPLACE FUNCTION get_party_details(VARIADIC bigint[]) 
    RETURNS TABLE(studentid bigint,studentName character varying, amtPaid numeric) AS 
$BODY$ 
    DECLARE 
     SQL VARCHAR; 
BEGIN 
    RETURN QUERY 
     SELECT 
      STU.student_id as "Student ID" 
      ,STU.student_name as "Student Name" 
      ,STU.amt_paid  as "Amount Paid" 

     FROM STUDENT  STU 

      WHERE STU.STUDENT_ID IN (SELECT $1[I] FROM GENERATE_SUBSCRIPTS($1, 1) G(I)); 
END; 

$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100 
    ROWS 1000; 

我的函數返回以下結果:

SELECT * FROMget_party_details(101,102,103,104);

StudentId Student Name  Amt Paid 
101   JOHN   428000.00 
102   SMITH   275405.00 
103   JACKSON  109250.00 
104   LOVELESS  63200.00 

queries.xml

<select id="get_party_details" parameterType="StudentVO" 
     statementType="PREPARED" resultMap="partyMap"> 
      select * from get_party_details(#{studentFriendNums}) 
</select> 

    <resultMap type="StudentVO" id="partyMap"> 
     <result property="studentId"   column="studentid" /> 
     <result property="studentName"   column="studentname" /> 
     <result property="amtPaid"    column="amtpaid" /> 
    </resultMap> 

如果我使用上述Select語句我收到以下異常:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'studentFriendNums'. It was either not specified and/or could not be found for the javaType/jdbcType combination specified. 
### Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'studentFriendNums'. It was either not specified and/or could not be found for the javaType/jdbcType combination specified. 

任何幫助/建議?

編輯1:我試過了一個foreach循環,這並沒有讓我擺脫這個問題。

<select id="get_party_details" parameterType="StudentVO" statementType="PREPARED" resultMap="partyMap"> 
     select * from get_party_details(
     <foreach item="friends" index="index" collection="studentFriendNums" 
      open="(" separator="," close=")"> 
      #{friends} 
     </foreach> 
     ) 
    </select> 

異常與<foreach/>

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: function get_party_details(record) does not exist 
    Hint: No function matches the given name and argument types. You might need to add explicit type casts. 
    Position: 15 
### The error may exist in com/myblog/queries.xml 
### The error may involve com.myblog.StudentDAO.get_party_details-Inline 
### The error occurred while setting parameters 
### SQL: select * from epc.get_party_details( (  ? ,  ? ,  ? ,  ? ) ) 
### Cause: org.postgresql.util.PSQLException: ERROR: function get_party_details(record) does not exist 
    Hint: No function matches the given name and argument types. You might need to add explicit type casts. 
    Position: 15 

編輯2:我ibatis的版本是3.2.3

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper 
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
    <mapper namespace="com.myblog.StudentDTO"> 

    <select id="get_party_details" parameterType="StudentVO" 
     statementType="PREPARED" resultMap="partyMap"> 
     select * from get_party_details 
    <iterate property="studentFriendNums" conjunction="," open = "(" close = ")"> 
      #studentFriendNums[]# 
    </iterate> 
</select> 

    <resultMap type="StudentVO" id="partyMap"> 
     <result property="studentId"   column="studentid" /> 
     <result property="studentName"   column="studentname" /> 
     <result property="amtPaid"    column="amtpaid" /> 
    </resultMap> 

</mapper> 

回答

0

您可以使用foreach:

<select id="get_party_details" parameterType="StudentVO" 
      statementType="PREPARED" resultMap="partyMap"> 
       select * from get_party_details 
       <foreach item="item" index="index" collection="studentFriendNums" 
        open="(" separator="," close=")"> 
       #{item} 
       </foreach> 
    </select> 

只看見自己的第一個編輯,你不需要之前和之後的foreach打開和關閉支架。參數打開和關閉爲你做。

+0

它是我的壞!我只是看着它!謝謝 :) – 09Q71AO534

0

你可以嘗試迭代標籤:

<select id="get_party_details" parameterType="StudentVO" 
     statementType="PREPARED" resultMap="partyMap"> 
     select * from get_party_details 
    <iterate property="studentFriendNums" conjunction="," open = "(" close = ")"> 
      #studentFriendNums[]# 
    </iterate> 
</select> 

<resultMap type="StudentVO" id="partyMap"> 
    <result property="studentId"   column="studentid" /> 
    <result property="studentName"   column="studentname" /> 
    <result property="amtPaid"    column="amtpaid" /> 
</resultMap> 
+0

使用迭代給我一個警告作爲「元素類型的內容」選擇「必須匹配」(包括| trim | where | set | foreach | choose | if | bind)「# – 09Q71AO534

+0

ibatis是你的哪個版本使用?你能發送完整的sqlMap文件嗎? – arnabkaycee

+0

' \t org.mybatis \t MyBatis的 \t 3.2.3 ' – 09Q71AO534