将多个和多个类型的参数传递给iBATIS

时间:2014-08-26 12:16:54

标签: java ibatis

我在这里处境很复杂。我需要将三个参数传递给iBATIS select,其中两个是String,1是List

列表看起来像:

List<User> userList=new List<User>();

public class User
{
   private String name;
   private String location;
   //and other fields and setter/getter 
}

查询将如下所示:

Select * FROM myTable Where 
fromDate>= 'date1' and toDate<='date2' and name In ('nm1','nm2','nm3',......) and
location in('loc1','loc1','loc1',.....);

我的进步:

我尝试过分离查询

第1部分(使用Map传递多个参数):

Select * FROM myTable Where 
fromDate>= 'date1' and toDate<='date2' 

地图:

Map<String,String> paramMap=new HashMap<String, String>();
    paramMap.put("fromDate", fromDate);
    paramMap.put("toDate", toDate);

iBATIS查询:

<select id="" parameterClass="map" resultMap="">
    Select * FROM myTable Where fromDate>= #fromDate# and toDate<=#toDate#
</select>

第2部分(只有一个部分名称/位置。传递列表并对其进行迭代):

Select * FROM myTable Where 
name In ('nm1','nm2','nm3,......)

我的列表(只是一个字符串列表):

List<String> nameList=new ArrayList<String>();
    nameList.add("nm1");
    nameList.add("nm2");
    nameList.add("nm3");

iBATIS查询:

<select id="" parameterClass="list" resultMap="">
SELECT * from myTable WHERE name IN 
    <iterate open="(" close=")" conjunction=","  >
    #[]#
    </iterate>

这些都没关系。

我需要将这三个参数fromDatetoDateuserList传递给iBATIS并访问它们 构建一个像我提到的第一个查询一样的查询。

首先是可能的吗? 如果是,那么即使是一点提示(技术/流程/链接)也会拯救我。

如果没有,那么其他一些建议将不胜感激

请仅考虑iBATIS而非MyBatis

1 个答案:

答案 0 :(得分:1)

仍然无法使用ListMap找到直接传递参数的方法。但是我通过方式得到了我想要的东西:

我添加了另一个类Inof,它将把参数带到iBATIS。

Public Class Inof
{
    private String fromDate;
    private String fromDate;
    private List<User> list;

}

<select id="" parameterClass="myPackage.Inof" resultMap="">
    SELECT * from myTable WHERE  fromDate>= #fromDate# and toDate<=#toDate# and name IN 
     <iterate open="(" close=")" conjunction="," property="list"  >
     #list[].name#
    </iterate>
     and location  IN 
     <iterate open="(" close=")" conjunction="," property="list"  >
     #list[].location#
    </iterate>
</select>

这给了我想要的东西,但我并不完全满意。如果还有其他一些方法,这是好的做法或有效请分享。