ibatis一对多 执行一次sql语句(left join),报列名无效..求大神指点

月光之殇 2014-10-20 04:50:51
刚接触ibatis,想写一个一对多查询,看网上有种方法可以只查一次数据库就可以同时查出对应的子类.我试着仿照着写了个例子,报列名无效...代码如下:

sqlMapping配置:

<typeAlias alias="User" type="com.ailk.crms.entity.MqTestUser"/>
<typeAlias alias="UserScore" type="com.ailk.crms.entity.MqTestUserScore"/>
<resultMap class="User" id="_u" groupBy="id">
<result property="id" column="ID" />
<result property="name" column="NAME" />
<result property="gender" column="GENDER" />
<result property="age" column="AGE" />
<result property="userScoreList" resultMap="userDetail._us"/>
</resultMap>

<resultMap class="UserScore" id="_us">
<result property="scoreId" column="ID" />
<result property="userId" column="USER_ID" />
<result property="scoreName" column="NAME" />
<result property="totalScore" column="TOTAL_SCORE" />
<result property="objectiveScore" column="OBJECTIVE_SCORE" />
<result property="subjectiveScore" column="SUBJECTIVE_SCORE" />
</resultMap>
<select id="findUserByUsName" parameterClass="java.util.Map" resultMap="userDetail._u" >
select u.ID as id,
u.NAME as name,
u.GENDER as gender,
u.AGE as age,
us.ID as scoreId,
us.USER_ID as userId,
us.NAME as scoreName,
us.TOTAL_SCORE as totalScore,
us.OBJECTIVE_SCORE as objectiveScore,
us.SUBJECTIVE_SCORE as subjectiveScore
from MQ_TEST_USER u
left join MQ_TEST_USER_SCORE us
on u.ID = us.USER_ID
where us.NAME in
<iterate property="subjects" open="(" close=")" conjunction=",">
#subjects[]#
</iterate>
</select>


实体类:

public class MqTestUser {

private Long id;
private String name;
private String gender;
private int age;
private List<MqTestUserScore> userScoreList;
...........
}

public class MqTestUserScore {

private Long scoreId;
private String scoreName;
private Long userId;
private int totalScore;
private int objectiveScore;
private int subjectiveScore;
....................
}


DAO实现方法:

public class MqTestUserDaoImpl extends SqlMapClientDaoSupport implements MqTestUserDao {

@SuppressWarnings("unchecked")
public List<MqTestUser> findUserFetchUserScoreByUsName1(Map<String, Object> paramMap, int offset, int max) {
return getSqlMapClientTemplate().queryForList("findUserByUsName", paramMap, offset, max);
}
}


错误日志:
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in com/ailk/crms/config/entity/mapping/MqTestUserSqlMapping.xml.
--- The error occurred while applying a result map.
--- Check the userDetail._us.
--- Check the result mapping for the 'userId' property.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: Error getting nested result map values for 'userScoreList'. Cause: java.sql.SQLException: 列名无效; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/ailk/crms/config/entity/mapping/MqTestUserSqlMapping.xml.
--- The error occurred while applying a result map.
--- Check the userDetail._us.
--- Check the result mapping for the 'userId' property.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: Error getting nested result map values for 'userScoreList'. Cause: java.sql.SQLException: 列名无效

我是参照这个写的:
http://cuishen.iteye.com/blog/544207
http://yingzhuo.iteye.com/blog/964064
...全文
185 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
月光之殇 2014-11-02
  • 打赏
  • 举报
回复
问题解决了....resultMap中colum属性值应该和对应的 sql语句中as别名一致.

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧