搭建一个mybatis,出现session.selectOne方法错误

错误提示:

org.apache.ibatis.exceptions.PersistenceException: 

### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.helloword.mapper.UserMapper.xml

### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.helloword.mapper.UserMapper.xml

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:71)

at test.Test.main(Test.java:43)

Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.helloword.mapper.UserMapper.xml

at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:888)

at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:721)

at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:714)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)

... 4 more

解决办法:

  1.  看到报错信息,猜测可能是因为配置文件中的配置有问题,先看看mybatis-config.xml中的<mapper>配置是否正确,查看<mapper>配置没有问题
  2. 查看UserMapper.xml中namespace的路径是否正确,检查没有问题
  3. UerMapper.xml在com.helloword.mapper的包里面,但是此包显示的为空,将UserMapper.xml映射文件移到实体类的包里面,并且修改一与二中的配置。测试之后,还是报上述错误。

---------------------------------------------一万只草泥马----------------------------------------------------------------------

新的报错信息

org.apache.ibatis.exceptions.PersistenceException: 

### Error building SqlSession.

### The error may exist in com/helloworld/model/UserMapper.xml

### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/helloworld/model/UserMapper.xml

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52)

at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36)

at test.Test.<clinit>(Test.java:30)

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/helloworld/model/UserMapper.xml

at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)

at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)

at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50)

... 2 more

Caused by: java.io.IOException: Could not find resource com/helloworld/model/UserMapper.xml

at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)

at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)

at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:371)

at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)

... 4 more

Exception in thread "main" java.lang.NullPointerException

at test.Test.main(Test.java:41)

  1. 错误显示是:创建SqlSession的问题,并报出主方法的空指针错误。
  2. 解决办法:粗心,在mybatis配置文件的<mapper>的配置路径中一个字母写错 了
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值