一:问题描述

1:前言

无论你是将mapper.xml文件是和resources建造在一块,还是将mapper.xml文件和mapper放在一块,我们只要修改在yaml当中的mapper-locations的相对路径即可。(前提是你在pom文件中导入了相关的resources路径)

2:下方是将mapper.xml和mapper写在了一块

3:mapper.xml在resources路径下

4:需要在pom中导入的resources路径

<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory></resource><resource><directory>src/main/resources</directory><includes><include>**/*.yaml</include><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource>
</resources>

二:整合小demo过程

(1):导入场景启动器

org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 ## (2):写全局配置文件 在reources路径下 ![在这里插入图片描述](https://img-blog.csdnimg.cn/ed9c176cb8f549058c2d4a445cd87555.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aSp5aSp5ZCR5LiK55qE6I-c6bih5p2w77yB77yB,size_11,color_FFFFFF,t_70,g_se,x_16)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--方便在mapper.xml中全限定名的简写-->
<typeAliases><typeAlias alias="TextUser" type="com.wyj.Pojo.TextUser"/>
</typeAliases></configuration>

(3):写mapper接口

注意我们的注解@mapper 相当于将其注入到IOC容器中
package com.wyj.mapper;import com.wyj.Pojo.TextUser;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface TextUserMapper {TextUser getUser(int id);
}

(4):mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wyj.mapper.TextUserMapper"><select id="getUser" resultType="TextUser" parameterType="_int">select * from text where id = #{id}</select></mapper>

(5):service层

package com.wyj.Service;import com.wyj.mapper.TextUserMapper;
import com.wyj.Pojo.TextUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class TextUserService {@Autowiredprivate TextUserMapper textUserMapper;public TextUser getUser(int id) {return textUserMapper.getUser(id);}
}

(6):Controller层

@Autowired
private TextUserService textUserService;@ResponseBody
@RequestMapping("/text")
public TextUser getUser(Integer id){return textUserService.getUser(id);}

(7):yaml配置文件

#Mybatis配置
mybatis:
#全局配置文件的路径
config-location: classpath:mybatis/mybatis-config.xml
#指明.xml文件的路径
mapper-locations: classpath:com/wyj/mapper/*.xml

(8):在pom文件中需要引入的resources路径

<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory></resource><resource><directory>src/main/resources</directory><includes><include>**/*.yaml</include><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource>
</resources>

解决Spring boot整合mybatis,xml资源文件放置及路径配置问题相关推荐

  1. spring boot 整合mybatis 无法输出sql的问题

    使用spring boot整合mybatis,测试功能的时候,遇到到了sql问题,想要从日志上看哪里错了,但是怎么都无法输出执行的sql,我使用的是log4j2,百度了一下,很多博客都说,加上下面的日 ...

  2. Spring Boot 整合MyBatis(23)

    Spring Boot 整合MyBatis Spring Boot 整合 Druid 引入依赖 配置 application.yml pring Boot 整合 tk.mybatis 引入依赖 配置 ...

  3. 干货必看|Spring Boot整合MyBatis框架详解

    在开发中,我们通常会对数据库的数据进行操作,Sprirng Boot对关系型数据库和非关系型数据库的访问操作都提供了非常好的整合支持.所以今天壹哥就给大家讲解一下,如何在SpringBoot环境中整合 ...

  4. Spring Boot 教程(三): Spring Boot 整合Mybatis

    教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...

  5. Spring boot 整合 Mybatis 实现增删改查(MyEclipse版)

    1.首先搭建好一个Spring boot 程序,编写好启动类. 启动类代码如下: @SpringBootApplication public class Start {public static vo ...

  6. Spring Boot整合MyBatis

    最近项目原因可能会继续开始使用MyBatis,已经习惯于spring-data的风格,再回头看xml的映射配置总觉得不是特别舒服,接口定义与映射离散在不同文件中,使得阅读起来并不是特别方便. Spri ...

  7. Spring Boot 整合 MyBatis Plus实现多数据源的两种方式

    第一种:使用配置类的方式: 项目结构 xml依赖: <?xml version="1.0" encoding="UTF-8"?> <proje ...

  8. Spring Boot整合Mybatis【超详细】

    pring Boot整合Mybatis 配置文件形式 pom.xml 配置数据源 UserMapper.xml UserMapper 配置springboot整合mybatis 在运行类上添加@Map ...

  9. 2021-5-10:Spring Boot整合MyBatis

    Spring Boot整合MyBatis Spring Boot 整合MyBatis (一)基础环境搭建 1.数据准备 创建数据库.数据表并插入一定的数据 (1)创建博客数据库blog 在Navica ...

最新文章

  1. 多线程共享全局变量以及锁机制
  2. JavaScript中this详解
  3. 计算机网络的体系结构与协议基本概念,计算机网络技术基础-第3章网络体系结构与协议.ppt...
  4. ByteTrack实时多目标跟踪
  5. matlab 里catmull rom,Unity中的曲线插值CatmullRom
  6. 机器学习领域有哪一些值得关注的人
  7. 每日源码分析 - Lodash(remove.js)
  8. Trie图和Fail树
  9. 苹果mac科学计算器:PCalc
  10. JS高级程序设计第四版启航
  11. 在线图片编辑器/在线视频剪辑器/在线批量加水印网站源码
  12. MFC控件重叠显示问题
  13. 大众点评字体解密(最新)2020/4/17
  14. 绿盟科技实习安服面经
  15. Mac系统升级后开发者证书不被信任,报错/usr/bin/codesign --force --sign
  16. cad添加自己线性_CAD2014怎么自定义线型? cad设置线型的方法
  17. python 修改文件内容3种方法,Python实现修改文件内容的方法分析
  18. 如何使用 NoxPlayer 加速 Android 应用程序开发?
  19. 经常用电脑,要注意脸部保养
  20. 新华三发布《中国城市数字经济指数白皮书(2019)》,融绘数字中国蓝图用数据记录城市数字化转型进程!

热门文章

  1. [转]定了!2020年,6种将死的编程语言!
  2. jQuery 3.4.0 Released(2019.4.10)
  3. 【BIM入门实战】Revit 2018模型设计阶段重点及注意事项总结
  4. 【经典回放】多种语言系列数据结构算法:基数排序
  5. Android之华为手机打开app奔溃提示java.io.FileNotFoundException: res/drawable/abc_vector_test.xml
  6. IOS学习笔记七之KVC和Key路径
  7. linux网络编程之setsockopt()函数来设置socket状态以及getsockopt函数只用总结
  8. linux 配置path
  9. HTML基础之bit哥的反客为主之道(9)
  10. python闭环最短路径_深度学习经典算法 | 蚁群算法解析