maven系列之打包问题

今天写mybatis练手时碰到了一个棘手的问题,弄了半个小时.
话不多说, 上错误日志

org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name ‘springDemo’: Unsatisfied dependency expressed through field ‘userService’: Error creating bean with name ‘userServiceImpl’: Unsatisfied dependency expressed through field ‘usersMapper’: Error creating bean with name ‘usersMapper’ defined in file [E:\apache-tomcat-8.0.48\webapps\ROOT\WEB-INF\classes\com\springmybatis\dao\UsersMapper.class]: Cannot resolve reference to bean ‘sqlSessionFactory’ while setting bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [org.springframework.core.io.Resource[]] for property ‘mapperLocations’; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/springmybatis/mapping/.xml]: class path resource [com/springmybatis/mapping/] cannot be resolved to URL because it does not exist; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘usersMapper’ defined in file [E:\apache-tomcat-8.0.48\webapps\ROOT\WEB-INF\classes\com\springmybatis\dao\UsersMapper.class]: Cannot resolve reference to bean ‘sqlSessionFactory’ while setting bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [org.springframework.core.io.Resource[]] for property ‘mapperLocations’; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/springmybatis/mapping/.xml]: class path resource [com/springmybatis/mapping/] cannot be resolved to URL because it does not exist; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userServiceImpl’: Unsatisfied dependency expressed through field ‘usersMapper’: Error creating bean with name ‘usersMapper’ defined in file [E:\apache-tomcat-8.0.48\webapps\ROOT\WEB-INF\classes\com\springmybatis\dao\UsersMapper.class]: Cannot resolve reference to bean ‘sqlSessionFactory’ while setting bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [org.springframework.core.io.Resource[]] for property ‘mapperLocations’; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/springmybatis/mapping/.xml]: class path resource [com/springmybatis/mapping/] cannot be resolved to URL because it does not exist; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘usersMapper’ defined in file [E:\apache-tomcat-8.0.48\webapps\ROOT\WEB-INF\classes\com\springmybatis\dao\UsersMapper.class]: Cannot resolve reference to bean ‘sqlSessionFactory’ while setting bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [org.springframework.core.io.Resource[]] for property ‘mapperLocations’; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/springmybatis/mapping/.xml]: class path resource [com/springmybatis/mapping/] cannot be resolved to URL because it does not exist

大致意思就是你在配置文件里面配置的mapper层的扫描路径不存在.
我回头就是检查写法:

<!-- 配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:com/springmybatis/mapping/*.xml"/>
    </bean>

我的项目目录:
这里写图片描述
这个路径

com/springmybatis/mapping/*.xml

写的没问题的.
那为什么死活找不到呢?
后来一看target目录下的东西
这里写图片描述
发现了吧, 少了mapping层中的xml文件, 这个没有当然找不到了.

原来在idea中, 以文件夹类型来决定打包文件类型, source文件下的java文件,propertites文件之类的; resource文件夹下的xml文件等一堆配置文件.
我是把xml文件放在java文件夹下, java是个source文件夹,所以我的mapping文件夹就被忽略掉了…

找到原因了,这里给出两种解决方法:
(1)
修改pom.xml打包配置

<build>
      <finalName>SpringWebDemo</finalName>
      <resources>
          <resource>
              <directory>src/main/java</directory>
              <includes>
                  <include>**/*.xml</include>
              </includes>
              <filtering>false</filtering>
          </resource>
      </resources>
  </build>

强制不忽略xml文件, 这样我们的mapping文件夹下的xml文件自然能被打包.
(2)
将我们mapping层的xml文件放在resource文件夹下, 不过这里要注意一个路径问题, 因为你需要mybatis的自动映射,所以原来在java文件夹下什么路径, 在resource文件夹下就用什么路径.

然后再build一下看看:
这里写图片描述
OK, 文件进来了,
再测试下, 通过了

NICE.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值