【请教】ibatis 入门级错误 Invalid column name

-droidcoffee- 2010-04-24 09:54:55
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=test"/>
<property name="JDBC.Username" value="sa"/>
<property name="JDBC.Password" value="sa"/>
</dataSource>
</transactionManager>

<sqlMap resource="tao/ibatis/bean/User.hbm.xml"/>

</sqlMapConfig>



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="User">

<typeAlias alias="User" type="tao.ibatis.bean.User"/>

<resultMap id="UserResult" class="User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>

<select id="selectAllUsers" resultMap="UserResult">
select count(*) from "user"
</select>


</sqlMap>


package tao.ibatis.bean;

import java.io.Serializable;

public class User implements Serializable{
private long id;
private String username;
private String password;

public User(){

}

public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}




package tao.ibatis.test;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account;

import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;

public class UserTest {

private static SqlMapClient sqlMapper;

static {
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
}
}

public static List selectAllUsers () throws SQLException {
return sqlMapper.queryForList("selectAllUsers");
}


public static void main(String[] args) throws SQLException {
List<?> ls = selectAllUsers();
System.out.println(ls.size());
}
}



请问大家 我该怎么办呢?


Exception in thread "main" com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in tao/ibatis/bean/User.hbm.xml.
--- The error occurred while applying a result map.
--- Check the User.UserResult.
--- Check the result mapping for the 'id' property.
--- Cause: java.sql.SQLException: Invalid column name id.
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:201)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:122)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:98)
at tao.ibatis.test.UserTest.selectAllUsers(UserTest.java:28)
at tao.ibatis.test.UserTest.main(UserTest.java:48)
Caused by: java.sql.SQLException: Invalid column name id.







...全文
763 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
-droidcoffee- 2010-04-25
  • 打赏
  • 举报
回复


<resultMap id="UserResult" class="User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>

<select id="selectAllUsers" resultMap="UserResult">
select count(*) from "user"
</select>


问题解决了

我是没有搞明白 resultMap的含义
resultMap="UserResult" 中 指定了 返回类型

而我返回的count(*) 却 没有与其关联




<resultMap id="UserResult" class="User">
<result property="id" column="id"/>

</resultMap>

<select id="selectAllUsers" resultMap="UserResult">
select count(*) as id from "user"
</select>

这样子 搞定了


injuer 2010-04-24
  • 打赏
  • 举报
回复
楼主主键的类型一般不推介使用long型...换个其他类型....

long型在框架里转换还是很麻烦的....
justchenjie 2010-04-24
  • 打赏
  • 举报
回复
数据库表里有id吗?还有long改成int统一一下

67,515

社区成员

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

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