ibatis查询结果映射的问题

问题描述:
我使用的ibatis框架,现在有两个类,一个User一个Template,多对一的关系,即一个用户只能选择一个模板,一个模板可以被多个用户选择。

User.class
[code="java"]
public class User {
private Long userId;
private String email;
private String username;
private String password;
private Template template;
//getters and setters...
}
[/code]

Template.class
[code="java"]

public class Template {
private Long templateId;
private String name;
//getters and setters...
}
[/code]

resultMap
[code="xml"]














<select id="loginByUsername" parameterClass="User" resultMap="base">
    <![CDATA[
        select
            user_id, template_id, email, username, password
        from
            user as u
        where
            u.password = #password# and
            u.username = #username#
    ]]>
</select>


[/code]

如果是这样设置,运行loginByUsername会有以下异常:

[code="java"]
Exception in thread "main" com.ibatis.common.jdbc.exception.NestedSQLException:

--- The error occurred in net/ing100/microblog/po/mapping/Template.xml.

--- The error occurred while applying a result map.

--- Check the Template.base.

--- Check the result mapping for the 'name' property.

--- Cause: com.ibatis.sqlmap.client.SqlMapException: Error getting nested result map values for 'template'. Cause: java.sql.SQLException: Column 'name' not found.
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:204)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForObject(MappedStatement.java:120)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:518)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:493)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:106)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForObject(SqlMapClientImpl.java:82)
at net.ing100.microblog.dao.impl.UserDAOImpl.loginByEmail(UserDAOImpl.java:12)
at net.ing100.microblog.test.dao.TestUserDAO.main(TestUserDAO.java:32)
Caused by: com.ibatis.sqlmap.client.SqlMapException: Error getting nested result map values for 'template'. Cause: java.sql.SQLException: Column 'name' not found.
at com.ibatis.sqlmap.engine.mapping.result.ResultMap.setNestedResultMappingValue(ResultMap.java:467)
at com.ibatis.sqlmap.engine.mapping.result.ResultMap.applyNestedResultMap(ResultMap.java:395)
at com.ibatis.sqlmap.engine.mapping.result.ResultMap.setResultObjectValues(ResultMap.java:380)
at com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:385)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:300)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:189)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteQuery(MappedStatement.java:221)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:189)
... 7 more
Caused by: java.sql.SQLException: Column 'name' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1093)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at com.mchange.v2.c3p0.impl.NewProxyResultSet.getString(NewProxyResultSet.java:3342)
at com.ibatis.sqlmap.engine.type.StringTypeHandler.getResult(StringTypeHandler.java:35)
at com.ibatis.sqlmap.engine.mapping.result.ResultMap.getPrimitiveResultMappingValue(ResultMap.java:619)
at com.ibatis.sqlmap.engine.mapping.result.ResultMap.getResults(ResultMap.java:345)
at com.ibatis.sqlmap.engine.mapping.result.ResultMap.setNestedResultMappingValue(ResultMap.java:454)
... 15 more
[/code]

请问像这种查询,template_id如何装配到template.templateId中去,并且不要出什么column'name'的异常?

1个回答

呵呵。。。我觉得你的配置和我的不一样,我研究的也不深,有点模糊,错了请见谅。
我的是只有一个,对应而的几个实体放在里面,查询的语句放在里面,通过存储过程去执行的。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问