org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bireportSqlSessionFactory' defined in URL


報錯如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bireportSqlSessionFactory' defined in URL [file:/D:/devcode/apstart/bireport/bireport-center/target/bireport-center/WEB-INF/classes/spring/spring-bireport-db.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\devcode\apstart\bireport\bireport-center\target\bireport-center\WEB-INF\classes\com\apstar\bireport\mapper\demo.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Mapper's namespace cannot be empty

1.剛開始的時候,懷疑是mybatis的bean有問題,更換了名稱,不行。

2.然后查看資料,嘗試在pom里面放入依賴包,但工程發現已經下載有了mybatis的兩個包,所以排除。

3.檢查我的mybatis-config.xml配置文件,也沒問題,排除。

4.結果發現:我使用了p:mapperLocations 所以,mybatis會掃描我的mapper的xml文件,如果沒有它,會報bean錯誤

在我的mapper層下的xml文件,必須要建有,並且是有mybatis的頭引用,並且<mapper...><.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" >
<mapper namespace="com.apstar.bireport.dao.DemoDao">

<select id="selectDemo" parameterType="com.apstar.bireport.po.DemoPo"
resultType="com.apstar.bireport.po.DemoPo"></select>
</mapper>

 

我的mybatis配置如下 :

 <!-- mybatis配置 -->
    <!-- mybatis文件配置,掃描所有mapper文件 -->
    <bean id="bireportSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
          p:dataSource-ref="bireportDataSource" p:configLocation="classpath:mybatis/mybatis-config.xml"
          p:mapperLocations="classpath*:com/apstar/bireport/mapper/*.xml"
          p:typeAliasesPackage="com.apstar.bireport.po"/>
<!-- mybatisDAO配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.apstar.bireport.dao"/> <property name="sqlSessionFactoryBeanName" value="bireportSqlSessionFactory"/> </bean>

  

 

參考了:https://blog.csdn.net/ljm15832631631/article/details/79346279


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM