读书人

初学者的Mybatis报错有关问题

发布时间: 2014-01-17 00:06:00 作者: rapoo

菜鸟的Mybatis报错问题
是这样的,我在映射文件 UserInfMapper.xml文件里面写了


<!-- 对应的java文件定义:public UserInf selectPublicName(String userName); -->
<select id="obtainedPublicName" resultType="zxw.model.UserInf" parameterType="java.lang.String">
select * from userInf
where user_name = #(userName,jdbcType=VARCHER)
</select>

然后在UserInfMapper.java里面写到:

//对应的包package zxw.dao
public interface UserInfMapper {
/**
* 根据名字查询数据
*/
UserInf obtainedPublicName(String userName);
}

之后,我在service包下面写:
//对应的包package zxw.service;
public interface UserInfServiceI {
/**
* 根据用户姓名查找用户是否存在
* @param name:用户姓名
* @return:用户信息
*/
public UserInf findByName(String userName);
}

--------------这些都没报错--------------
接下来:
我实现UserInfServiceI,写到:

//对应的包package zxw.service.impl;
@Service(value="userInfService")
public class UserInfServiceImpl implements UserInfServiceI {
@Autowired
private UserInfMapper userInfMapper;//spring autowired注入了
public UserInf findByName(String userName) {
return userInfMapper.obtainedPublicName(userName);
}
}

此时,报错,说:The method obtainedPublicName(String) is undefined for the type UserInfMapper

我不知道为什么会错,希望有朋友能指点下,帮我解决下问题
[解决办法]
select * from userInf where user_name = #(userName) 试试
[解决办法]
报错是 类型匹配的问题。。clean,重启试试。
[解决办法]
还出什么问题。
[解决办法]

<select id="obtainedPublicName" parameterType="String" resultType="zxw.model.UserInf">
select * from user where userName=#{userName}
</select>

你确定 * 和 zxw.model.UserInf 里面的属性对应??

你 select * from user where userName=#{userName} 这个先改查 select userName from user where userName=#{userName} 看看
[解决办法]
@Autowired
private UserInfMapper userInfMapper;//spring autowired注入了

UserInfMapper 的spring配置是什么?
[解决办法]
你原文写的是 #(userName,jdbcType=VARCHER),不知是你复制过来的还有手打的,VARCHER应为VARCHAR
[解决办法]
parameterType传给此语句的参数的完整类名或别名
[解决办法]
mapper中的配置有没有<mapper namespace="zxw.dao.userInfMapper"></mapper>这样把mapper中的方法文件和你userInfMapper接口中的方法对应起来就好了。
或者你协议个继承userInfMapper的java方法继承ibatisDao。

public class UserInfMapperImpl extends IbatisBaseDao implements UserInfMapper{
@Override
public UserInf obtainedPublicName(String userName);{
return this.getSqlSession().selectList("zxw.dao.userInfMapper.obtainedPublicName", userName);
}

[解决办法]
参数设置 为题。 userName
[解决办法]
引用:
Quote: 引用:

mapper中的配置有没有<mapper namespace="zxw.dao.userInfMapper"></mapper>这样把mapper中的方法文件和你userInfMapper接口中的方法对应起来就好了。
或者你协议个继承userInfMapper的java方法继承ibatisDao。

public class UserInfMapperImpl extends IbatisBaseDao implements UserInfMapper{
@Override
public UserInf obtainedPublicName(String userName);{
return this.getSqlSession().selectList("zxw.dao.userInfMapper.obtainedPublicName", userName);
}

1、存在你说的东西
<mapper namespace="zxw.dao.UserInfMapper" >
<!-- 自动生成的代码 -->
<!--

[解决办法]
以下些,都是我自己洗的

------解决方案--------------------


@Author:weidu23
-->
<!-- public int selectMaxId(); -->
<select id="obtainedMaxId" resultType="java.lang.Integer">
select max(user_id) from userInf
</select>
<!-- public UserInf selectPublicName(String userName); -->
<select id="obtainedPublicName" resultType="zxw.model.UserInf" >
select * from userInf
where user_name = #(userName,jdbcType=VARCHAR)
</select>
<!-- 以上些都是@weidu23的 -->
</mapper>



2、我清理了下缓存问题,发现,原来的问题没了,出了新问题,如下:

严重: Servlet.service() for servlet [springMvc] in context with path [/DemoThree] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
### The error may exist in file [D:\apache-tomcat-7.0.50\webapps\DemoThree\WEB-INF\classes\zxw\mapping\UserInfMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from userInf where user_name = #(userName,jdbcType=VARCHAR)
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

但是我自己不知道我的Mapper错在哪里,求赐教
现在报的是sql语句错误了,说明能识别了。你把语句改成这样试一试:

select * from userInf
where user_name = #{userName}

看好,是大括号

读书人网 >J2EE开发

热点推荐