MyBatis-Spring的整合,配置Mapper的过程中遇到的异常
项目中用的是spring3.0.5,MyBatis3.0.5,整合mybatis,spring用的是mybatis-spring-.1.0.1。在这个过程中,遇到了一个配置上的问题,就是配置Mapper时的错误,在运行时抛出如下异常:
Exception in thread "main" java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.xxxx.mail.webmail.dao.UserMapper.getUserById一看就知是Mapper配置出错了。仔细检查过了,1、我是用:<bean value="cn.xxxx.mail.webmail.dao.mapper"/>??? </bean>让spring自动加载mapper配置的,这个没错。
2、UserMapper.java接口和UserMapper.xml同名,且在同一路径下,这个没错。???? 我的UserMapper.java有个方法是:public User getUserById(String id);
问题就出在这里了,我在UserMapper.xml中配成了这样:<select id="getUser" resultType="cn.xxxx.mail.webmail.model.User" parameterType="java.lang.String">
? ??? select * from users where id=#{value}
? </select>在配置文件中的select 的id写错了,写成getUser,没和UserMapper.java的方法同名
经过这次,得出如下结论:1、mapper的java文件和mapper的xml配置文件要同名,同路径
2、mapper的java文件的方法名称和mapper的xml配置文件的id要同名称
3、mapper配置文件的namespace要全名,不能只写包名???? 如:cn.xxxx.mail.webmail.dao.mapper.UserMapper,这时,namespace不能写 成:cn.xxxx.mail.webmail.dao.mapper
1 楼 liumin1939 2011-10-20 你好能不能说下spring+mybatis的事物是怎么弄的? 2 楼 enki_ding 2011-10-24 liumin1939 写道你好能不能说下spring+mybatis的事物是怎么弄的?
我的事务处理是交由spring aop处理的。