SSM之mybatis:修改了Mybatis的xml文件不生效

这两天写一个项目是,修改了下mybatis的xml文件中的一个sql语句,结果发现修改后的xml文件始终不生效。情况是这样的:

一开始我的语句

 <select id="selectAll" resultMap="BaseResultMap" >
    select category, ctime, annotation
    from category
    </select>

修改后(错误的修改)为

 <select id="selectAll" resultMap="BaseResultMap" >
    select category, ctime, annotation
    from category 
    order by
    </select>

然后我就直接运行了,提示报错

于是我立马修改成原来第一个图的代码,重新运行,结果还是报错,提示和上图一模一样。

去debug打印sql语句后,显示

“Preparing: select category, ctime, annotation from category ORDER BY ”

于是懵逼了,发现怎么修改都没有用,一直报一样的错误。

此时思考:为什么修改没用,一直sql语句会有“order by”,

1.缓存为清理,重启试试?

2.是不是xml文件根本就没读取到?映射有问题?

对第一个猜想,进行清理缓存,重启idea,发现,问题依然存在。

第二个猜想,查阅资料,查看xml配置文件,发现问题所在:我没有配置mapper映射文件的路径,所以每次mybatis的mapper.xml文件都会被漏掉,运行时,始终读取最初的xml。

如何配置mapper映射文件的路径:

1.在配置sqlSessionFactoryBean是,加上

<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>,即将映射文件放到resources文件夹下
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置文件的位置 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
            <!--配置mapper映射文件的路径,必须放在resources文件夹下,否则mybatis的mapper.xml文件都会被漏掉。或者在pom.xml配置-->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
    </bean>

2.在pom.xml配置,这个方法大家可以自行去网上搜索,给个spring-boot中使用这个方法的例子链接,大家可以看这个

https://segmentfault.com/q/1010000013418822

总结:通过上面的讲解,我们可以知道。在SSM中,对mybatis的xml文件,一定要记得配置它的映射路径,方法有两种(看上面)。一旦没有配置,那么mapper.xml文件都会被遗漏,造成修改无法成功或者其他问题(比如你把这个xml文件删除,数据库照样能读取数据之类的)。所以,以后凡是mapper.xml文件里面产生问题,第一想法,看映射路径是否正确

发布了22 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41623154/article/details/100974491