ibatis简单例子求助。
以前没学过ibatis的,虽然知道这东西将要被淘汰,但是现在的工作环境下用的就是ibatis。所以想想还是学学吧。以前只是调用人家写好的现成的dao层的。
今天试了一个例子,报错
空指针,求助。
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD sql Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="test/test_user">
<typeAlias alias="user" type="test.entity.User" />
<!-- Use primitive wrapper type (e.g. Integer) as parameter and allow results
to be auto-mapped results to Person object (Java Bean) properties -->
<select id="getUser" parameterClass="int" resultClass="user">
SELECT id,name,descs,address
FROM test_user
WHERE id = #value#
</select>
</sqlMap>
sql数据库:
执行代码:
public static void main(String[] args) {
try {
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
String resource = "sqlmap.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
// 显示一条记录
//getNameSpace() + "." + sqlName
//第一种方式,
List<User> list = (List<User>) sqlMap.queryForList("test/test_user.getUser","10");
System.out.println(list.get(0).getName());
//第二种方式
// User user = (User) sqlMap.queryForObject("test/test_user.getUser","10");
// System.out.println(user.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
最后报错:
java.lang.NullPointerException
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.endTransaction(SqlMapExecutorDelegate.java:734)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.endTransaction(SqlMapSessionImpl.java:176)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.endTransaction(SqlMapClientImpl.java:153)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.autoEndTransaction(SqlMapExecutorDelegate.java:835)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:574)
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.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:94)
at test.IbatisTest.main(IbatisTest.java:21)
求助!
------解决方案--------------------<select id="getUser" parameterClass="int" resultClass="user">
SELECT id,name,descs,address
FROM test_user
WHERE id = #value#
</select>
WHERE id = #value# 调查询方法的时候没有传入value 的入参哦!
------解决方案--------------------不好意思,没看清楚,貌似 这里的入参
List<User> list = (List<User>) sqlMap.queryForList("test/test_user.getUser","10");
要和
<select id="getUser" parameterClass="int" resultClass="user">
SELECT id,name,descs,address