如何使用sqlMap从数据库获取对象?
我想使用 sqlMap 从数据库插入和获取对象。插入用户工作正常,但是当我调用 getUser 时,它会抛出异常“请求处理失败;嵌套异常是 java.lang.NullPointerException”。
SqlUserDaoImpl
public void insertUser(User user)
{
try
{
getSqlMapClientTemplate().insert("User.insert", user);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public User getUser(String email) {
User user=null;
try
{
user = (User) getSqlMapClientTemplate().queryForObject("User.getUser", email);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
用户.xml
<sqlMap namespace="User">
<select id="getUser" resultClass="MyProject.domain.User">
select userName as name, password as password, email as email from users where email = #value#
</select>
<insert id="insert" parameterClass="MyProject.domain.User">
insert into users (userName,password,email) values
(#name#,#password#,#email#)
</insert>
</sqlMap>
I want to insert and get object from database using sqlMap. insert user is working fine but when i call getUser it throw an exception "Request processing failed; nested exception is java.lang.NullPointerException".
SqlUserDaoImpl
public void insertUser(User user)
{
try
{
getSqlMapClientTemplate().insert("User.insert", user);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public User getUser(String email) {
User user=null;
try
{
user = (User) getSqlMapClientTemplate().queryForObject("User.getUser", email);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
User.xml
<sqlMap namespace="User">
<select id="getUser" resultClass="MyProject.domain.User">
select userName as name, password as password, email as email from users where email = #value#
</select>
<insert id="insert" parameterClass="MyProject.domain.User">
insert into users (userName,password,email) values
(#name#,#password#,#email#)
</insert>
</sqlMap>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 sqlmap 中,您没有为 getUser 语句指定parameterClass; 添加:
尝试在您的声明中
In you sqlmap you are not specifying the parameterClass for the getUser statement; try adding:
to your statement.