一:问题描述
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>