如何在IBATIS中使用List映射多列?
我有一个豆子,说:
public class AttendanceBean {
private String user_id;
private String user_name;
private List daysArray;
// setter/getter methods
}
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>
我的问题是我将如何在ResultMap中使用列day_1,day_2,… day_31映射到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我用daysArray是特定用户参加的天数?添加一些架构细节将很有帮助.有天数的表也有用户ID?假设您遇到类似的情况,则可以执行以下操作(此示例基于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>
同样,我必须强调,这是一个示例,取决于您存储数据/模式的方式,但是希望该示例对您有所帮助.