欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)單的方式來(lái)注入 MyBatis 數(shù)據(jù)映射器和 SqlSession 到業(yè)務(wù)層的 bean 中。
  • 方便集成 Spring 事務(wù)
  • 翻譯 MyBatis 的異常到 Spring 的 DataAccessException 異常(數(shù)據(jù)訪問(wèn)異常)中。

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 用法中,是通過(guò) SqlSessionFactoryBuilder 來(lái)創(chuàng)建 SqlSessionFactory 的。 而在 MyBatis-Spring 中,則使用 SqlSessionFactoryBean 來(lái)創(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,它用來(lái)指定 MyBatis 的 XML 配置文件路徑。它在需要修改 MyBatis 的基礎(chǔ)配置非常有用。通常,基礎(chǔ)配置指的是 或 元素。
需要注意的是,這個(gè)配置文件并不需要是一個(gè)完整的 MyBatis 配置。確切地說(shuō),任何環(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è)屬性可以用來(lái)指定 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)文章

  • Java深入分析講解反射機(jī)制

    Java深入分析講解反射機(jī)制

    反射是框架的靈魂,Java框架底層都是用反射機(jī)制+xml配置等來(lái)實(shí)現(xiàn)的,本文將通過(guò)示例詳細(xì)講解Java中的反射機(jī)制,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2022-06-06
  • java刪除文件時(shí)總是返回false,刪不掉的解決方案

    java刪除文件時(shí)總是返回false,刪不掉的解決方案

    這篇文章主要介紹了java刪除文件時(shí)總是返回false,刪不掉的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例

    Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例

    幻燈片母版可供用戶設(shè)置幻燈片的樣式,本文將介紹如何用Java創(chuàng)建并應(yīng)用單個(gè)或多個(gè)幻燈片母版。感興趣可以了解一下
    2020-06-06
  • Scala中如何中斷循環(huán)詳解

    Scala中如何中斷循環(huán)詳解

    在Scala中,可以使用break和continue語(yǔ)句來(lái)中斷循環(huán)。break語(yǔ)句可以在滿足某個(gè)條件時(shí)中斷循環(huán),而continue語(yǔ)句可以在滿足某個(gè)條件時(shí)跳過(guò)當(dāng)前循環(huán)迭代,直接執(zhí)行下一次迭代
    2023-04-04
  • MyBatis實(shí)現(xiàn)注冊(cè)及獲取Mapper

    MyBatis實(shí)現(xiàn)注冊(cè)及獲取Mapper

    本文主要介紹了MyBatis實(shí)現(xiàn)注冊(cè)及獲取Mapper,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • java 注解annotation的使用以及反射如何獲取注解

    java 注解annotation的使用以及反射如何獲取注解

    這篇文章主要介紹了java 注解annotation的使用以及反射如何獲取注解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • java字符串轉(zhuǎn)JSON簡(jiǎn)單代碼示例

    java字符串轉(zhuǎn)JSON簡(jiǎn)單代碼示例

    這篇文章主要給大家介紹了關(guān)于java字符串轉(zhuǎn)JSON的相關(guān)資料,JSON?是一種輕量級(jí)的數(shù)據(jù)交換格式,常用于Web應(yīng)用程序中的數(shù)據(jù)傳輸,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • IntelliJ IDEA 報(bào)錯(cuò):找不到包或者找不到符號(hào)的問(wèn)題及解決方案

    IntelliJ IDEA 報(bào)錯(cuò):找不到包或者找不到符號(hào)的問(wèn)題及解決方案

    這篇文章主要介紹了IntelliJ IDEA 報(bào)錯(cuò):找不到包或者找不到符號(hào)的問(wèn)題及解決方案,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot靜態(tài)資源目錄訪問(wèn)

    SpringBoot靜態(tài)資源目錄訪問(wèn)

    今天小編就為大家分享一篇關(guān)于SpringBoot靜態(tài)資源目錄訪問(wèn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • java中常見(jiàn)的中文亂碼總結(jié)

    java中常見(jiàn)的中文亂碼總結(jié)

    本文主要介紹了java中常見(jiàn)的中文亂碼以及解決方法,主要包括字節(jié)碼文件讀取時(shí)出現(xiàn)的亂碼問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有很好的參考價(jià)值,感興趣的朋友跟隨小編一起看看吧
    2017-03-03

最新評(píng)論