mybatis 加載配置文件的方法(兩種方式)
一. 使用sqlSessionFactory 的 mapperLocations 進(jìn)行加載,
<!-- SessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="singleton"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 映射文件路徑,可以集中寫到一個(gè)地方,也可以與dao寫到一個(gè)地方,支持多個(gè)路徑,支持通配符--> <property name="mapperLocations" value="classpath:mapper/*.xml,classpath:com/sunny/shop/*/dao/*.xml"></property> </bean>
此種方法可以使用通配符, 可以指定位置, 可以使用多個(gè)位置,
二. 使用MapperScannerConfigurer進(jìn)行掃描
<!-- 掃描指定包下的所有接口,創(chuàng)建代理類,如果mysql的配置文件名與接口名相同的話,可以不用一一配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sunny.shop" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
此種方法可以掃描指定包下的接口, 如果需要掃描配置文件, 則配置文件須與對(duì)應(yīng)的DAO接口處于同一目錄, 且名字必須相同
三.配置 mybatis 的 mapper
<mappers> <!-- 既可寫映射文件, 也可寫對(duì)應(yīng)的接口 --> <!--<mapper resource="com/mybatis/student/StudentMapper.xml" /> <mapper resource="com/mybatis/classes/ClassesMapper.xml" /> <mapper class="com.sunny.shop.user.dao.UserDao" /> --> </mappers>
前兩種都是在spring的配置文件中配置的, 在 mybatis 的配置文件中配置 <mappers>節(jié)點(diǎn)
PS:下面給大家介紹下mybatis 加載配置文件的兩種方式
package com.atguigu.day03_mybaits.test; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { public static void test1(){ ///加載mybatis的配置文件(它也加載關(guān)聯(lián)的映射文件) String str="conf.xml"; InputStream is=Test.class.getClassLoader().getResourceAsStream(str); //構(gòu)建sqlSession的工廠 SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); //映射sql的標(biāo)識(shí)字符串,是在影射文件中找到namespace+“”+select中的id String statement="com.atguigu.day03_mybaits.userMapper.getUser"; //執(zhí)行查詢返回一個(gè)唯一user對(duì)象的sql User user=session.selectOne(statement, 1); System.out.println(user); } public static void test2() throws IOException{ ///加載mybatis的配置文件(它也加載關(guān)聯(lián)的映射文件) String resource = "conf.xml"; //加載mybatis的配置文件(它也加載關(guān)聯(lián)的映射文件) Reader reader = Resources.getResourceAsReader(resource); //構(gòu)建sqlSession的工廠 SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader); SqlSession session=factory.openSession(); //映射sql的標(biāo)識(shí)字符串,是在影射文件中找到namespace+“”+select中的id String statement="com.atguigu.day03_mybaits.userMapper.getUser"; //執(zhí)行查詢返回一個(gè)唯一user對(duì)象的sql User user=session.selectOne(statement, 2); System.out.println(user); } public static void main(String[] args) throws IOException { test1(); test2(); } }
總結(jié)
以上所述是小編給大家介紹的mybatis 加載配置文件的方法(兩種方式),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring AI 使用本地 Ollama Embeddings的操作方法
使用 OpenAI 的 Embeddings 接口是有費(fèi)用的,如果想對(duì)大量文檔進(jìn)行測試,使用本地部署的 Embeddings 就能省去大量的費(fèi)用,所以我們嘗試使用本地的 Ollama Embeddings,這篇文章主要介紹了Spring AI 使用本地 Ollama Embeddings,需要的朋友可以參考下2024-05-05Spring boot如何配置請(qǐng)求的入?yún)⒑统鰠son數(shù)據(jù)格式
這篇文章主要介紹了spring boot如何配置請(qǐng)求的入?yún)⒑统鰠son數(shù)據(jù)格式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11詳解如何保護(hù)SpringBoot配置文件中的敏感信息
使用過SpringBoot配置文件的朋友都知道,資源文件中的內(nèi)容通常情況下是明文顯示,安全性就比較低一些,所以為了提高安全性,就需要對(duì)配置文件中的敏感信息進(jìn)行保護(hù),下面就為大家介紹一下實(shí)現(xiàn)方法吧2023-07-07Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08java web監(jiān)聽器統(tǒng)計(jì)在線用戶及人數(shù)
本文主要介紹了java web監(jiān)聽器統(tǒng)計(jì)在線用戶及人數(shù)的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)
今天小編就為大家分享一篇關(guān)于SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03