MyBatis 基于接口编程学习笔记

异常:

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(),这样才会真正对数据库进行操作,否则是没有提交的。

多对一 遇到的异常 :在xml文件中 多方的

中type 不能直接写Article 而要写全 其路径:com.yihaomen.mybatis.model.Article

org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/yihaomen/mybatis/model/User.xml
### The error occurred while processing mapper_resultMap[resultUserArticleList]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Article'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Article
 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
 at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
 at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35)
 at com.yihaomen.test.Test.(Test.java:22)……
 ... 13 more
Exception in thread "main" java.lang.NullPointerException
 at com.yihaomen.test.Test.getUserArticles(Test.java:142)
 at com.yihaomen.test.Test.main(Test.java:35)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值