解决Spring boot整合mybatis,xml资源文件放置及路径配置问题
一:问题描述
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资源文件放置及路径配置问题相关推荐
- spring boot 整合mybatis 无法输出sql的问题
使用spring boot整合mybatis,测试功能的时候,遇到到了sql问题,想要从日志上看哪里错了,但是怎么都无法输出执行的sql,我使用的是log4j2,百度了一下,很多博客都说,加上下面的日 ...
- Spring Boot 整合MyBatis(23)
Spring Boot 整合MyBatis Spring Boot 整合 Druid 引入依赖 配置 application.yml pring Boot 整合 tk.mybatis 引入依赖 配置 ...
- 干货必看|Spring Boot整合MyBatis框架详解
在开发中,我们通常会对数据库的数据进行操作,Sprirng Boot对关系型数据库和非关系型数据库的访问操作都提供了非常好的整合支持.所以今天壹哥就给大家讲解一下,如何在SpringBoot环境中整合 ...
- Spring Boot 教程(三): Spring Boot 整合Mybatis
教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...
- Spring boot 整合 Mybatis 实现增删改查(MyEclipse版)
1.首先搭建好一个Spring boot 程序,编写好启动类. 启动类代码如下: @SpringBootApplication public class Start {public static vo ...
- Spring Boot整合MyBatis
最近项目原因可能会继续开始使用MyBatis,已经习惯于spring-data的风格,再回头看xml的映射配置总觉得不是特别舒服,接口定义与映射离散在不同文件中,使得阅读起来并不是特别方便. Spri ...
- Spring Boot 整合 MyBatis Plus实现多数据源的两种方式
第一种:使用配置类的方式: 项目结构 xml依赖: <?xml version="1.0" encoding="UTF-8"?> <proje ...
- Spring Boot整合Mybatis【超详细】
pring Boot整合Mybatis 配置文件形式 pom.xml 配置数据源 UserMapper.xml UserMapper 配置springboot整合mybatis 在运行类上添加@Map ...
- 2021-5-10:Spring Boot整合MyBatis
Spring Boot整合MyBatis Spring Boot 整合MyBatis (一)基础环境搭建 1.数据准备 创建数据库.数据表并插入一定的数据 (1)创建博客数据库blog 在Navica ...
最新文章
- 字符编码的发展(ASCII、Unicode、utf-8)
- 统计c语言中英文字幕,C语言日记——递归
- 架构设计基础:单服务.集群.分布式,基本区别和联系
- threejs加载模型挤压变形_玻璃钢模具表面变形的原因及解决办法
- 解决Chrome浏览器不能访问https网站的问题
- 企业工商信息数据API接口
- Boost Serialization 库
- 聚合支付与智能POS
- linux c设置打印机属性,如何恢复打印机属性的“功能”部分
- 元学习 迁移学习_元学习就是您所需要的
- python调用chrome插件_使用Python开发chrome插件
- 电动汽车充电站的最优选址matlab程序
- java画太极加图片_新手初学java 尝试编程绘画太极图
- 无基础怎么学习java虚拟机,0基础学习Java ——Java 虚拟机结构(上)
- 1t硬盘怎么分区最好_1T的硬盘空间怎么分区比较合理
- python3爬虫系列之使用requests爬取LOL英雄图片
- 发票识别100%智能
- USB Device(VID_1f3a_PID_efe8)
- 安装abuntu时候联想拯救者R7000P怎么进入bios
- Unity Shader unity文档学习笔记(十一):战争迷雾核心算法
热门文章
- 项目中引入composer包
- 【经典回放】多种语言系列数据结构算法:二叉树(JavaScript版)
- ENVI帮助研究人员发现金矿
- CityEngine Web Scene如何在IIS下部署
- C语言九十五之实现经典的反转数组(通过指针或数组下标操作)
- Java之new一个对象简单过程
- Android之AIDL服务
- Android之手机出现已安装了存在签名冲突的同名数据包的原因及解决办法
- python画4维图_用Python 画个六维图,涨姿势了
- “爱因斯坦兄弟”事件轰动纽约时报!双胞胎乱写博士论文,整容后越黑越红,竟然名利双收..........