报错如下:

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

05-19 10:03