mapper报错非法绑定?xml却查不出错误?

2022-09-05 14:04:37 浏览数 (1)

背景

今天重新跑了一下之前的ssm项目,但是发现数据查不出来,而且也没报错,首先f12查看报错,500.我debug项目后发现是service.login()的方法那后面便没了回声,便猜到应该是mapper出了问题,但我还不知道在哪里。也回忆起之前写项目的时候写mapper.xml的时候,新写的方法就是没有用。我顿悟了,去查看target的dao层下的xml的文件,果然!没有。一时间,风起云涌,我知道这一刻,我不再是个小丑,不再是那个被bug欺负的小丑。

原因

对于Maven项目,IntelliJ IDEA默认是不处理src/main/java中的非java文件的,不专门在pom.xml中配置<resources>是会报错的。

所以src/main/java中最好不要出现非java文件。实际上,将mapper.xml放在src/main/resources中比较合适。

解决

方法一

代码语言:javascript复制
<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

方法二

既然maven不会生成dao下的xml文件,那就放在资源文件夹下,下面贴出spring和mybatis的相关配置仅供参考

spring-mybatis.xml

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
 
    <!--DB配置文件-->
    <context:property-placeholder location="classpath:db.properties"
            ignore-unresolvable="true" />
 
    <!--数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
 
    <!--qlSessionFactory-->
    <bean id="sqlSessionFactory"
            class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--Mybatis配置文件-->
        <property name="configLocation"
                value="classpath:mybatis-config.xml" />
        <!--mapper.xml所在位置-->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
        <!--指定需要使用别名的PO类所在的包-->
        <property name="typeAliasesPackage"
                value="com.spldeolin.demoapp.po" />
    </bean>
 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--mapper接口所在的包-->
        <property name="basePackage" value="com.spldeolin.demoapp.dao" />
    </bean>
 
</beans>

mybatis-config.xml

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 
    <!-- 其他全局配置 -->
    <settings>
        <setting name="logImpl" value="LOG4J2" />
        <setting name="cacheEnabled" value="true" />
    </settings>
 
    <!--全局分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql" />
            <property name="offsetAsPageNum" value="true" />
            <property name="rowBoundsWithCount" value="true" />
            <property name="pageSizeZero" value="true" />
            <property name="reasonable" value="false" />
            <property name="returnPageInfo" value="check" />
            <property name="params" value="pageNum=start;pageSize=limit;" />
        </plugin>
    </plugins>
 
</configuration>

小知识

chasspath路径

下面是classpath的三种路径:

tomcat控制台输出中文

tomcat中的vm配置上下面的代码

代码语言:javascript复制
-Dfile.encoding=UTF-8

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:mapper报错非法绑定?xml却查不出错误?

0 人点赞