[英]ArrayList Mapping in IBatis
How should I map multiple columns with List in IBATIS ? 我应该如何在IBATIS中使用List映射多个列?
I have a Bean, say : 我有一个豆,说:
public class AttendanceBean {
private String user_id;
private String user_name;
private List daysArray;
// setter/getter methods
}
Ibatis (select clause in sqlMap): Ibatis(sqlMap中的select子句):
<select id="someName" parameter="param">
select user_id,user_name,
day_1,
day_2,
day_3,
...
...
day_31
from table1,table2
where table1.userid=table2.userid
<select>
My question is how I am going to Map AttendanceBean's daysArray with columns day_1
, day_2
, ... day_31
in ResultMap
even though I can simply write JDBC code for this as follows: 我的问题是我将如何使用day_1
, day_2
,... day_31
ResultMap
AttendanceBean的daysArray,即使我可以简单地为此编写JDBC代码,如下所示:
List alluser = new ArrayList();
while (rs.next()) {
AttendanceBean ab = new AttendanceBean();
ab.setUser_id(rs.getString("USER_ID"));
ab.setUser_name(rs.getString("USER_NAME"));
List tempArray = new ArrayList();
for (int i=1;i<=noOfDaysinMonth;i++) {
tempArray.add(rs.getString("DAY_"+i));
}
ab.setDayArray(tempArray);
alluser.add(ab);
}
Faraz I take the daysArray
is the number of days a particular user attended? 法拉兹我把daysArray
特定用户参加的天数? It would be helpful to add some details of your schema. 添加架构的一些细节会很有帮助。 The table that has the number of days also has the user Id as well? 具有天数的表也具有用户ID? Assuming you have a similar case you can do the following (this example is based on iBatis version 2.3.4). 假设您有类似的情况,您可以执行以下操作(此示例基于iBatis版本2.3.4)。
<select id="retrieveAllUserAttendences" resultMap="exampleUserAttendences">
SELECT
table1.user_id,
table1.user_name,
table2.day
FROM table1
INNER JOIN table2 ON table2.user_id = table1.user_id
</select>
<resultMap id="exampleUserAttendences" class="AttendanceBean" groupBy="user_id">
<result property="user_id" column="user_id" jdbcType="VARCHAR" javaType="java.lang.String"/>
<result property="user_name" column="user_name" jdbcType="VARCHAR" javaType="java.lang.String"/>
<result property="daysArray" resultMap="sqlMapNamespace.exampleDaysResultMap" />
</resultMap>
<resultMap id="exampleDaysResultMap" class="java.lang.String">
<result property="daysArray" column="day_column_name" jdbcType="VARCHAR" javaType="java.lang.String"/>
</resultMap>
Again I must stress this is an example and depends on how you store your data/schema is, however hope this example helps. 我再次强调这是一个例子,取决于你如何存储你的数据/模式,但希望这个例子有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.