搭建一个mybatis,出现配置文件错误

错误提示:

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中的配置是否正确,查看配置没有问题
  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配置文件的的配置路径中一个字母写错了

猜你喜欢

转载自blog.csdn.net/wuchao_codingforever/article/details/79727690
今日推荐