當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
解决Spring boot整合mybatis,xml资源文件放置及路径配置问题
生活随笔
收集整理的這篇文章主要介紹了
解决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路徑下  <?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资源文件放置及路径配置问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪吒 GT OTA 升级 V1.3.0
- 下一篇: 快讯|全国银行首家 飞书宣布完成民生银行