最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring與Mybatis整合方式(mybatis-spring整合jar包功能)

 更新時(shí)間:2025年05月21日 09:32:51   作者:OREO夾心  
這篇文章主要介紹了Spring與Mybatis整合方式(mybatis-spring整合jar包功能),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、 MyBatis與Spring的集成

在學(xué)習(xí)mybatis配置時(shí),對(duì)于mybatis-config配置的時(shí)候我們發(fā)現(xiàn),大致是需要配置三個(gè)方面:setting、datasource、mappers

而mybatis的setting往往使用默認(rèn)配置,所以我們經(jīng)常配置datasource數(shù)據(jù)源與mappers映射,但學(xué)習(xí)spring之后發(fā)現(xiàn),對(duì)于datasource的配置交由spring進(jìn)行管理,所以在spring與mybatis整合后mybatis的配置文件中將不需要配置datasource,mybatis的配置幾乎都會(huì)在Spring配置之中完成。

當(dāng)然要想要實(shí)現(xiàn)spring與mybatis的整合,其中最重要的就是 mybatis-spring.jar 包

  • mybatis-spring會(huì)用于幫助你將 MyBatis 代碼無(wú)縫地整合到 Spring 中。
  • Spring 將會(huì)加載必要的 MyBatis 工廠類和 Session 類
  • 提供一個(gè)簡(jiǎn)單的方式來注入 MyBatis 數(shù)據(jù)映射器和 SqlSession 到業(yè)務(wù)層的 bean 中。
  • 方便集成 Spring 事務(wù)
  • 翻譯 MyBatis 的異常到 Spring 的 DataAccessException 異常(數(shù)據(jù)訪問異常)中。

Mybatis-Spring 兼容性,我們?cè)谶x擇Spring、MyBatis以及mybatis-spring時(shí),應(yīng)注意版本之間的兼容性

二、spring與mybatis快速整合

①導(dǎo)入spring與mybatis相應(yīng)坐標(biāo)

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <!--定義Spring版本號(hào)-->
    <org.springframework.version>4.3.18.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <!-- 單元測(cè)試相關(guān)依賴 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>

    <!-- spring必要依賴 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <!-- spring aop織入依賴 -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>

    <!-- mysql驅(qū)動(dòng) -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

    <!-- 數(shù)據(jù)庫(kù)連接池 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.29</version>
    </dependency>

    <!-- mybatis相關(guān)依賴 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>

    <!-- mybatis與spring對(duì)接依賴 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
  </dependencies>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172

②創(chuàng)建數(shù)據(jù)庫(kù)連接配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

# 初始化大小,最小,最大
initialSize=10
minIdle=6
maxActive=50
123456789

③創(chuàng)建mybatis配置文件

mybatis文件與之前不同,之前在mybatis-config.xml中配置數(shù)據(jù)庫(kù)連接的,現(xiàn)在要把這些放在spring的配置文件中,所以mybatis配置文件中只寫setting配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
    	<!--  緩沖配置  -->
    	<setting name="cacheEnabled" value="true"/>
    	<!-- 懶加載 -->
    	<setting name="lazyLoadingEnabled" value="true"/>
    	<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
     	<setting name="aggressiveLazyLoading" value="false"/>
   		<!-- 緩沖區(qū)配置 -->
    	<setting name="localCacheScope" value="SESSION"/>
	</settings>
	<!--<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments> -->
	<!-- 映射文件的配置
	<mappers>
		<package name="com.dao" />
	</mappers> -->
</configuration>
1234567891011121314151617181920212223242526272829303132

④創(chuàng)建spring配置文件

2)spring配置頭文件與約束地址

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
1234567891011121314

2)spring注解開發(fā)

    <!-- 開啟spring 注解掃描 -->
    <context:component-scan base-package="com.yunhe"/>
12

3)導(dǎo)入properties配置文件

    <!-- 加載類路徑下的properties配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
12

4)dataSource數(shù)據(jù)源配置

     <!-- datasource數(shù)據(jù)源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialPoolSize" value="${initialSize}"/>
        <property name="minPoolSize" value="${minIdle}"/>
        <property name="maxPoolSize" value="${maxActive}"/>
    </bean>
12345678910

5)sqlSessionFactory數(shù)據(jù)會(huì)話工廠配置

 <!-- 配置SqlSessionFactory對(duì)象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入數(shù)據(jù)庫(kù)連接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <!-- 配置mapper映射文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
123456789

6)mapper接口配置

    <!-- mybatis.spring自動(dòng)映射,DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yunhe.mapper" />
    </bean>
1234

7)事務(wù)管理器

    <!--平臺(tái)事務(wù)管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
1234

8)設(shè)置事務(wù)掃描

    <!-- 開啟事務(wù)控制的注解支持 --> 
	<tx:annotation-driven transaction-manager="transactionManager" /> 
12

⑤根據(jù)配置文件創(chuàng)建相應(yīng)的包、接口、實(shí)體類

  • Account
package com.yunhe.pojo;
public class Account {
    private String name;
    private double money;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
123456789101112131415161718192021222324
  • AccountMapper
package com.yunhe.mapper;
import com.yunhe.pojo.Account;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface AccountMapper {
    public List<Account> selectAll();
}
12345678

⑥書寫mapper的sql映射配置文件

  • AccountMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yunhe.mapper.AccountMapper">
    <select id="selectAll" resultType="com.yunhe.pojo.Account">
        select * from account
    </select>
</mapper>
1234567

⑦書寫測(cè)試代碼

import com.yunhe.mapper.AccountMapper;
import com.yunhe.pojo.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class T {
     @Test
    public void asd(){
         ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
         AccountMapper accountMapper = (AccountMapper) ac.getBean("accountMapper");
         List<Account> accounts = accountMapper.selectAll();
         System.out.println(accounts);
     }
}
123456789101112131415

結(jié)果:

三、mybatis-spring整合jar包功能

①SqlSessionFactoryBean

在基礎(chǔ)的 MyBatis 用法中,是通過 SqlSessionFactoryBuilder 來創(chuàng)建 SqlSessionFactory 的。 而在 MyBatis-Spring 中,則使用 SqlSessionFactoryBean 來創(chuàng)建。

要?jiǎng)?chuàng)建工廠 bean,將下面的代碼放到 Spring 的 XML 配置文件中:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>
123

1)DataSource

SqlSessionFactory 有一個(gè)唯一的必要屬性:用于 JDBC 的 DataSource。這可以是任意的 DataSource 對(duì)象,它的配置方法和其它 Spring 數(shù)據(jù)庫(kù)連接是一樣的。

2)configLocation

一個(gè)常用的屬性是 configLocation,它用來指定 MyBatis 的 XML 配置文件路徑。它在需要修改 MyBatis 的基礎(chǔ)配置非常有用。通常,基礎(chǔ)配置指的是 或 元素。
需要注意的是,這個(gè)配置文件并不需要是一個(gè)完整的 MyBatis 配置。確切地說,任何環(huán)境配置(),數(shù)據(jù)源()和 MyBatis 的事務(wù)管理器()都會(huì)被忽略。SqlSessionFactoryBean 會(huì)創(chuàng)建它自有的 MyBatis 環(huán)境配置(Environment),并按要求設(shè)置自定義環(huán)境的值。

如果 MyBatis 在映射器類對(duì)應(yīng)的路徑下找不到與之相對(duì)應(yīng)的映射器 XML 文件,那么也需要配置文件。這時(shí)有兩種解決辦法:第一種是手動(dòng)在 MyBatis 的 XML 配置文件中的 部分中指定 XML 文件的類路徑;第二種是設(shè)置工廠 bean 的 mapperLocations 屬性。

3)mapperLocations

mapperLocations 屬性接受多個(gè)資源位置。這個(gè)屬性可以用來指定 MyBatis 的映射器 XML 配置文件的位置。屬性的值是一個(gè) Ant 風(fēng)格的字符串,可以指定加載一個(gè)目錄中的所有文件,或者從一個(gè)目錄開始遞歸搜索所有目錄。比如:

    <!-- 配置SqlSessionFactory對(duì)象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入數(shù)據(jù)庫(kù)連接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <!-- 配置mapper映射文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
123456789

②MapperScannerConfigurer

basePackage

MapperScannerConfigurer:有一個(gè)重要的屬性basePackage用于掃描映射指定包下所有的mapper映射接口,mybatis執(zhí)行時(shí)會(huì)與已經(jīng)加載的mapper對(duì)應(yīng)的xml進(jìn)行映射調(diào)用相應(yīng)的方法執(zhí)行sql語(yǔ)句返回結(jié)果

   <!-- mybatis.spring自動(dòng)映射,DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yunhe.mapper" />
    </bean>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 分享7款開源Java反編譯工具

    分享7款開源Java反編譯工具

    今天我們要來分享一些關(guān)于Java的反編譯工具,反編譯聽起來是一個(gè)非常高上大的技術(shù)詞匯,通俗的說,反編譯是一個(gè)對(duì)目標(biāo)可執(zhí)行程序進(jìn)行逆向分析,從而得到原始代碼的過程。尤其是像.NET、Java這樣的運(yùn)行在虛擬機(jī)上的編程語(yǔ)言,更容易進(jìn)行反編譯得到源代碼
    2014-09-09
  • Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境

    Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境

    本文主要介紹了Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringCloud?openfeign聲明式服務(wù)調(diào)用實(shí)現(xiàn)方法介紹

    SpringCloud?openfeign聲明式服務(wù)調(diào)用實(shí)現(xiàn)方法介紹

    在springcloud中,openfeign是取代了feign作為負(fù)載均衡組件的,feign最早是netflix提供的,他是一個(gè)輕量級(jí)的支持RESTful的http服務(wù)調(diào)用框架,內(nèi)置了ribbon,而ribbon可以提供負(fù)載均衡機(jī)制,因此feign可以作為一個(gè)負(fù)載均衡的遠(yuǎn)程服務(wù)調(diào)用框架使用
    2022-12-12
  • 批量上傳Jar包到Maven私服的工具的方法

    批量上傳Jar包到Maven私服的工具的方法

    這篇文章主要介紹了批量上傳Jar包到Maven私服的工具的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • Spring?Boot?Interceptor的原理、配置、順序控制及與Filter的關(guān)鍵區(qū)別對(duì)比分析

    Spring?Boot?Interceptor的原理、配置、順序控制及與Filter的關(guān)鍵區(qū)別對(duì)比分析

    本文主要介紹了Spring?Boot中的攔截器(Interceptor)及其與過濾器(Filter)的區(qū)別,攔截器是Spring?MVC提供的接口,可以實(shí)現(xiàn)請(qǐng)求處理前后的邏輯,本文介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway詳細(xì)分析

    SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway詳細(xì)分析

    這篇文章主要介紹了SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-04-04
  • 詳解Java串行化接口的用法和原理

    詳解Java串行化接口的用法和原理

    在Java開發(fā)中,我們經(jīng)常需要將對(duì)象進(jìn)行序列化和反序列化,以便在網(wǎng)絡(luò)傳輸或存儲(chǔ)到持久化介質(zhì)中,Java提供了一種機(jī)制,即通過實(shí)現(xiàn)Serializable接口來實(shí)現(xiàn)對(duì)象的串行化,本文將詳細(xì)介紹Java串行化接口的用法和原理,以及一些相關(guān)的注意事項(xiàng),需要的朋友可以參考下
    2023-11-11
  • mybatis plus數(shù)據(jù)權(quán)限插件在項(xiàng)目中的使用方式

    mybatis plus數(shù)據(jù)權(quán)限插件在項(xiàng)目中的使用方式

    本文介紹了如何在MyBatis中實(shí)現(xiàn)數(shù)據(jù)權(quán)限攔截器,通過自定義注解和攔截器,動(dòng)態(tài)地在SQL中添加數(shù)據(jù)權(quán)限條件
    2026-01-01
  • Java線程池獲取池中所有線程列表的方法總結(jié)

    Java線程池獲取池中所有線程列表的方法總結(jié)

    在Java中,獲取線程池中所有線程列表并不是一個(gè)直接支持的功能,因?yàn)榫€程池的設(shè)計(jì)通常是為了隱藏和管理底層的線程細(xì)節(jié),從而提供更高層次的抽象和并發(fā)控制能力,本文給大家介紹了Java線程池獲取池中所有線程列表的方法,需要的朋友可以參考下
    2024-10-10
  • SpringBoot自定義FailureAnalyzer詳解

    SpringBoot自定義FailureAnalyzer詳解

    這篇文章主要介紹了SpringBoot自定義FailureAnalyzer詳解,FailureAnalyzer是一種在啟動(dòng)時(shí)攔截?exception?并將其轉(zhuǎn)換為?human-readable?消息的好方法,包含在故障分析中,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

遂昌县| 通城县| 马龙县| 杂多县| 伊宁市| 华宁县| 介休市| 盐源县| 乌鲁木齐县| 孝昌县| 泊头市| 乳山市| 黄浦区| 潼关县| 巫山县| 平邑县| 盘山县| 若羌县| 岳西县| 阜平县| 陵川县| 若羌县| 三门县| 翁牛特旗| 兴业县| 河曲县| 蓬溪县| 琼结县| 平原县| 轮台县| 申扎县| 新安县| 吐鲁番市| 乌什县| 玛纳斯县| 抚顺县| 南城县| 太和县| 南乐县| 四平市| 化州市|