1. 程式人生 > >mybatis與Spring三種開發方式詳解(二&三)Mapper動態代理開發&Mapper動態代理掃描包形式開發

mybatis與Spring三種開發方式詳解(二&三)Mapper動態代理開發&Mapper動態代理掃描包形式開發

mybatis與Spring三種開發方式詳解(二)Mapper動態代理開發

之前我們說到傳統的dao層開發,今天我們來談談第二種mybatis與Spring的開發方式,Mapper動態代理開發。

首先這裡上一波@test測試程式碼,大家先看一下mybatis測試的完整的過程程式碼,以便後面的講解。

public class MybatisMapperTest {

    @Test
    public void testMapper() throws Exception {
        //載入核心配置檔案
        String resource = "sqlMapConfig.xml"
; InputStream in = Resources.getResourceAsStream(resource); //建立SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); //建立SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //SqlSEssion幫我生成一個實現類 (給介面)
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserById(10); System.out.println(user); }

1.編寫Mapper.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.itheima.mybatis.mapper.UserMapper"> <!-- 通過ID查詢一個使用者 --> <select id="findUserById" parameterType="Integer" resultType="User"> select * from user where id = #{v} <!-- 寫什麼都行,寫個abc也行 --> </select> </mapper>

2.實現UserMapper介面

package com.itheima.mybatis.mapper;
import com.itheima.mybatis.pojo.User;

public interface UserMapper {

    public User findUserById(Integer id);

}

3.配置Spring核心檔案applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 資料庫連線池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- Mybatis的工廠 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置檔案的位置 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    </bean>

    <!-- Mapper動態代理開發 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
        <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"/>
    </bean>
</beans>

注意這塊配置,生成我們定義的UserMapper介面的實現類

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
        <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"/>
    </bean>
</beans>

這裡對應的就是我們一開始完整的@test程式碼的如下程式碼,這個過程:

SqlSession sqlSession = sqlSessionFactory.openSession();

//SqlSEssion幫我生成一個實現類  (給介面)
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

配置檔案中注入的mapperInterface屬性就是對應getMapper(UserMapper.class)中的引數。
3.Mapper配置檔案UserMapper.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.itheima.mybatis.mapper.UserMapper">


    <!-- 通過ID查詢一個使用者 -->
    <select id="findUserById" parameterType="Integer" resultType="User">
        select * from user where id = #{v} <!-- 寫什麼都行,寫個abc也行 -->
    </select>
</mapper>

4.mybatis核心配置檔案sqlMapConfig.xml

<?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>
    <!-- 設定別名 -->
    <typeAliases>
        <!-- 2. 指定掃描包,會把包內所有的類都設定別名,別名的名稱就是類名,大小寫不敏感 -->
        <package name="com.itheima.mybatis.pojo" />
    </typeAliases>
    <mappers>
        <package name="com.itheima.mybatis.mapper"/>
    </mappers>

</configuration>

[email protected]測試類junitTest.java

package com.itheima.mybatis.junit;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itheima.mybatis.mapper.UserMapper;
import com.itheima.mybatis.pojo.User;

public class JunitTest {


    @Test
    public void testMapper() throws Exception {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = ac.getBean(UserMapper.class);
//      UserMapper mapper = (UserMapper) ac.getBean("userMapper");
        User user = mapper.findUserById(10);
        System.out.println(user);
    }
}

這就是第二種mybatis與Spring開發方式,Mapper動態代理開發。

mybatis與Spring三種開發方式詳解(三)Mapper動態代理掃描包形式開發dao(增強第二種方式)

只需把Spring核心配置檔案applicationContext.xml中改為如下程式碼即可:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 資料庫連線池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- Mybatis的工廠 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置檔案的位置 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    </bean>

    <!-- Mapper動態代理開發   掃描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 基本包 -->
        <property name="basePackage" value="com.itheima.mybatis.mapper"/>
    </bean>

新增後我們便不用去書寫複雜的注入配置程式碼,因為MapperScannerConfigurer會自動去尋找basePackage即com.itheima.mybatis.mapper下的子包及其子包中的檔案,以後開發用這種最多。

相關推薦

mybatisSpring開發方式&Mapper動態代理開發&Mapper動態代理掃描形式開發

mybatis與Spring三種開發方式詳解(二)Mapper動態代理開發 之前我們說到傳統的dao層開發,今天我們來談談第二種mybatis與Spring的開發方式,Mapper動態代理開發。 首先這裡上一波@test測試程式碼,大家先看一下mybat

Python selenium 等待方式

建議 方法 comm 決定性 是否 等待時間 python 弊端 style 1. 強制等待第一種也是最簡單粗暴的一種辦法就是強制等待sleep(xx),強制讓閃電俠等xx時間,不管凹凸曼能不能跟上速度,還是已經提前到了,都必須等xx時間。看代碼: # -*- codin

網站分析資料的收集方式

回顧網站資料分析歷史,從“您是第***位來訪使用者”到現在百家齊放的專業工具提供商,網站分析已經逐漸發展衍化成一門科學。但面對形態各異的分析資料,很多人仍然困惑於資料的來源,瞭解資料的收集原理,也許對你解決這些困惑有所幫助。 眼下網站分析資料主要有三種收集方式:Web日誌、JavaScript標記和包嗅

Python selenium 等待方式(必會)

很多人在群裡問,這個下拉框定位不到、那個彈出框定位不到…各種定位不到,其實大多數情況下就是兩種問題:1 有frame,2 沒有加等待。殊不知,你的程式碼執行速度是什麼量級的,而瀏覽器載入渲染速度又是什麼量級的,就好比閃電俠和凹凸曼約好去打怪獸,然後閃電俠打完回來之後問凹凸曼

網站分析資料即使用者行為資料收集方式

        回顧網站資料分析歷史,從“您是第***位來訪使用者”到現在百家齊放的專業工具提供商,網站分析已經逐漸發展衍化成一門科學。但面對形態各異的分析資料,很多人仍然困惑於資料的來源,瞭解資料的收集原理,也許對你解決這些困惑有所幫助。眼下網站分析資料主要有三種收集方式:

Javascript設計模式開發實踐:策略模式 http://www.jianshu.com/p/ef53781f6ef2

的人 思想 ram gis pan pro msg have 改變 上一章我們介紹了單例模式及JavaScript惰性單例模式應用這一次我主要介紹策略模式策略模式是定義一系列的算法,把它們一個個封裝起來,並且讓他們可以互相替換。比方說在現實中很多時候也有很多途徑到達同一個

js中作用域全域性,函式,塊級

1.全域性變數:宣告在函式外部的變數(所有沒有var直接賦值的變數都屬於全域性變數) 2.區域性變數:宣告在函式內部的變數(所有沒有var直接賦值的變數都屬於全域性變數) JS中變數申明分顯式申明和隱

(轉)Spring boot——logback.xml 配置

回到頂部1 根節點<configuration>包含的屬性scan:當此屬性設定為true時,配置檔案如果發生改變,將會被重新載入,預設值為true。scanPeriod:設定監測配置檔案是否有修改的時間間隔,如果沒有給出時間單位,預設單位是毫秒。當scan為true時,此屬性生效。預設的時間間隔

Spring boot——logback.xml 配置

原文地址:https://www.cnblogs.com/lixuwu/p/5810912.html                   https://aub.iteye.com/blog/1101260 閱

Spring ------- AOP關鍵概念以及兩實現方式

目錄 1. AOP 關鍵詞 2. AOP 的作用 3. AOP 的通知型別 4. 基於 xml 的配置方式 5. 基於註解的配置方式 6. 切面的優先順序 7. 重用切點表示式 8. 兩種方式的比較(摘自 spring 官方文件) 1. AOP 關鍵詞

Spring ------- AOP概念以及兩實現方式

  target:目標類,需要被代理的類。例如:ArithmeticCalculator      Joinpoint(連線點):所謂連線點是指那些可能被攔截到的方法。例如:所有的方法      PointCut 切入點:已經被增強的連線點。例如:add()      advice:通知/增強,增強

O(n*logn)級別的演算法之快速排序實現方法及其歸併排序的對比

快速排序被稱為二十世紀演算法界的一大傑作 一,單路快排 1.測試用例: #ifndef INC_06_QUICK_SORT_DEAL_WITH_NEARLY_ORDERED_ARRAY_SORTTESTHELPER_H #define INC_06_QUICK_

【Android】Android開發之常用的loading等待效果實現,仿微博等待動畫。兩實現方式

長期維護的Android專案,裡面包括常用功能實現,以及知識點詳解, 當然還有Java中的知識點。 具體請看github:https://github.com/QQ986945193/DavidAndroidProjectTools 首先大家都知道,當我

Spring事務Transaction配置的五注入方式

前段時間對Spring的事務配置做了比較深入的研究,在此之間對Spring的事務配置雖說也配置過,但是一直沒有一個清楚的認識。通過這次的學習發覺Spring的事務配置只要把思路理清,還是比較好掌握的。     總結如下:     Spring配置檔案中關於事務配置總是由

枚舉所有子集的算法-《算法入門經典》

函數 全排列 算法入門 n-1 printf 算法 枚舉 turn 詳解 方法一:增量構造法   理解遞歸必須得理解函數到底是做什麽的。 #include<cstdio> void print_subset(int n,int *a,int cur

Spring 依賴註入方式

管理所 pri 理解 stat 工廠類 pro this 容器 pos 閱讀目錄 1.Set註入 2.構造器註入 3.靜態工廠的方法註入 4.實例工廠的方法註入 平常的Java開發中,程序員在某個類中需要依賴其它類的方法。 通常是new一個依賴類再調用類實例的方

JAVA中this的用法的

enc 所有 其它 println 用途 詳細介紹 示例 一次 調用構造   this關鍵字必須放在非靜態方法裏面 this關鍵字代表自身,在程序中主要的使用用途有以下幾個方面:    使用this關鍵字引用成員變量    使用this關鍵字在自身構造方法內部引用其它構造方

Ribbon的幾配置方式

文章目錄 Ribbon的自定義配置(java程式碼方式)生效條件 Ribbon的注意事項 0、Ribbon的自定義配置優先順序: 1、自定義配置時,@Configuration和@ComponentScan包不應重疊 2、使用R

Vmware虛擬機器網路模式

由於linux目前很熱門,越來越多的人在學習linux,但是買一臺服務放家裡來學習,實在是很浪費。那麼如何解決這個問題?虛擬機器軟體是很好的選擇,常用的虛擬機器軟體有vmware workstations和virtual box等。在使用虛擬機器軟體的時候,很多初學者都會遇到

JVM記憶體模型及GC的

(本文基於JDK6) 說到GC,首先要對Java 的記憶體模型有所瞭解。 Java 的記憶體模型各個代的預設排列有如下圖(適用JDK1.4.*  到 JDK6): Java 的記憶體模型分為 Young(年輕代) Tenured(終身代) Perm(永久代