博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以接口的方式编程 问题解析

Posted on 2016-02-18 14:06  code_Craftsman  阅读(168)  评论(0编辑  收藏  举报

异常:

Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.yihaomen.mybatis.inter.IUserOperation is not known to the MapperRegistry.
 at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:42)
 at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:655)
 at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:218)
 at com.yihaomen.test.Test.main(Test.java:33)

 

原因:User.xml 文件中 mapper中:错误如下红色字体为错误命名空间,接口编程中应对应接口所在路径 ,绿色为正确配置。

<--------?xml version="1.0" encoding="UTF-8" ?>
<--------!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<--------!--namespace="com.yihaomen.mybatis.inter.IUserOperation"  -->
<--------mapper namespace="com.yihaomen.mybatis.models.UserMapper" >
    <--------select id="selectUserByID" parameterType="int" resultType="User">
        select * from `user` where id = #{id}
    <--------/select>
<--------/mapper>

注意:增加,更改,删除的时候要调用session.commit(),这样才会真正对数据库进行操作,否则是没有提交的。