日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2023/12/4 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决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资源文件放置及路径配置问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。