當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
解决Spring boot整合mybatis,xml资源文件放置及路径配置问题
生活随笔
收集整理的這篇文章主要介紹了
解决Spring boot整合mybatis,xml资源文件放置及路径配置问题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一:問題描述
1:前言
無(wú)論你是將mapper.xml文件是和resources建造在一塊,還是將mapper.xml文件和mapper放在一塊,我們只要修改在yaml當(dāng)中的mapper-locations的相對(duì)路徑即可。(前提是你在pom文件中導(dǎo)入了相關(guān)的resources路徑)
2:下方是將mapper.xml和mapper寫在了一塊
3:mapper.xml在resources路徑下
4:需要在pom中導(dǎo)入的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過(guò)程
(1):導(dǎo)入場(chǎng)景啟動(dòng)器
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中全限定名的簡(jiǎn)寫--> <typeAliases><typeAlias alias="TextUser" type="com.wyj.Pojo.TextUser"/> </typeAliases></configuration>(3):寫mapper接口
注意我們的注解@mapper 相當(dāng)于將其注入到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> 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的解决Spring boot整合mybatis,xml资源文件放置及路径配置问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪吒 GT OTA 升级 V1.3.0
- 下一篇: Springboot后台管理(CRUD)