1. 程式人生 > >搭建一個mybatis,出現配置檔案錯誤

搭建一個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配置檔案的的配置路徑中一個字母寫錯了