Spring整合mybatis报错问题纪录

1、sqlSessionFactory创建失败

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [conf/spring-mybatis.xml]:
这里写图片描述

好像是说在spring-mybatis.xml中注册configLocation的地方有错

    <!--&lt;!&ndash; spring和MyBatis完美整合,不需要mybatis的配置映射文件 &ndash;&gt;-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/web/dev/mapper/*.xml"></property>
    </bean>

后来查了一下,classpath后面需要加一个*号,正确的如下:

    <!--&lt;!&ndash; spring和MyBatis完美整合,不需要mybatis的配置映射文件 &ndash;&gt;-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:com/web/dev/mapper/*.xml"></property>
    </bean>

2、报找不到xml配置文件

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.web.dev.dao.WebsitesMapper.queryList

进入编译后的class文件夹中查看,发现mapper.xml文件并为编辑成功;
后经查询发现,
注意:
在使用IDEA开发时,如果打包时*Mapper.xml没有自动复制到class输出目录的mapper类包下,则需要在pom文件中添加mybatis加载配置文件的配置!
如下

<build>
   <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.xml</include>
           </includes>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
       </resource>
    </plugins>
 </build>

本人的问题就是IDEA下碰到的,在pom文件中添加mybatis加载配置文件然后就完美解决了,感谢互联网资源~

复制上述代码时,pom文件可能报:
Element ‘beans’ cannot have character [children], because the type’s content type is element-only.错误。
就是这些从网页上直接复制下来的眼睛看不到的字符导致了这个错误!

要是看不出来改一下编码格式,如ANSI编码!
或事直接手动修改编写就行

3、java.lang.IllegalArgumentException错误

java.lang.IllegalArgumentException: Result Maps collection already contains value for com.web.dev.da
这里写图片描述

这个为mapper.xml文件中的id有重复的,仔细查看一下是否有重复的id命名即可

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值