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

原理分析Java?Mybatis中的Mapper

 更新時(shí)間:2022年01月07日 15:07:06   作者:qq_40178533  
這篇文章主要為大家介紹了Java?Mybatis中的Mapper,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

準(zhǔn)備

1.pom文件

 <dependencies>
        <!--mybatis坐標(biāo)-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!--mysql驅(qū)動(dòng)坐標(biāo)-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>runtime</scope>
        </dependency>
        <!--單元測(cè)試坐標(biāo)-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--日志坐標(biāo)-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

2.user類-數(shù)據(jù)庫(kù)

在這里插入圖片描述

3.實(shí)體類

@Getter
@Setter
@ToString
@NoArgsConstructor
public class user {
    private int id;
    private String username;
    private String password;
}

4.dao 層

public interface userDao {
    List<user> findAll();
}

5.Mapper 文件

<?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="dao.userDao">
    <select id="findAll" resultType="mode.user">
        select * from user
    </select>
</mapper>

核心配置文件

<?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>
    <!--通過(guò)properties標(biāo)簽加載外部properties文件-->
    <properties resource="jdbc.properties"></properties>
    <!--自定義別名-->
    <typeAliases>
        <typeAlias type="mode.user" alias="user"></typeAlias>
    </typeAliases>
    <!--數(shù)據(jù)源環(huán)境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--加載映射文件-->
    <mappers>
        <mapper resource="userMapper.xml"></mapper>
    </mappers>

</configuration>

核心代碼

import dao.userDao;
import mode.user;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMapper {
    public static void main(String[] args) throws IOException {
        //加載核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //獲得sqlSession工廠對(duì)象
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(resourceAsStream);
        //獲得sqlSession對(duì)象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //執(zhí)行sql語(yǔ)句
        userDao mapper = sqlSession.getMapper(userDao.class);
        List<user> userList = mapper.findAll();
        //打印結(jié)果
        System.out.println(userList);
        //釋放資源
        sqlSession.close();
    }
}

源碼分析

1.斷點(diǎn)

在這里插入圖片描述

2.查看源碼

DefaultSqlSession:

在這里插入圖片描述

Configuration:

在這里插入圖片描述

這是Configuration 類。我們主要看getMapper()方法

在這里插入圖片描述

MapperRegistry:

在這里插入圖片描述

關(guān)鍵代碼:mapperProxyFactory.newInstance(sqlSession);

MapperProxyFactory:

在這里插入圖片描述

返回的是MapperProxy 對(duì)象

MapperProxy:

源碼:

// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.apache.ibatis.binding;
import java.io.Serializable;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import org.apache.ibatis.lang.UsesJava7;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.SqlSession;
public class MapperProxy<T> implements InvocationHandler, Serializable {
    private static final long serialVersionUID = -6424540398559729838L;
    private final SqlSession sqlSession;
    private final Class<T> mapperInterface;
    private final Map<Method, MapperMethod> methodCache;
    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
        this.methodCache = methodCache;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            if (Object.class.equals(method.getDeclaringClass())) {
                return method.invoke(this, args);
            }
            if (this.isDefaultMethod(method)) {
                return this.invokeDefaultMethod(proxy, method, args);
            }
        } catch (Throwable var5) {
            throw ExceptionUtil.unwrapThrowable(var5);
        }
        MapperMethod mapperMethod = this.cachedMapperMethod(method);
        return mapperMethod.execute(this.sqlSession, args);
    }
    private MapperMethod cachedMapperMethod(Method method) {
        MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method);
        if (mapperMethod == null) {
            mapperMethod = new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration());
            this.methodCache.put(method, mapperMethod);
        }
        return mapperMethod;
    }
    @UsesJava7
    private Object invokeDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable {
        Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        Class<?> declaringClass = method.getDeclaringClass();
        return ((Lookup)constructor.newInstance(declaringClass, 15)).unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
    }
    private boolean isDefaultMethod(Method method) {
        return (method.getModifiers() & 1033) == 1 && method.getDeclaringClass().isInterface();
    }
}

在這里插入圖片描述

在invoke方法中可以看到,如果我們調(diào)用的是Object中的方法,不做任何處理,直接調(diào)用,否則執(zhí)行:

mapperMethod.execute(this.sqlSession, args);

MapperMethod:

在這里插入圖片描述

在MapperMethod 中對(duì)SQL語(yǔ)句進(jìn)行分類反射

總結(jié)

  • MapperProxyFactory中,使用JDK的動(dòng)態(tài)代理生成Mapper接口的代理代理類
  • 由動(dòng)態(tài)處理器MapperProxy中調(diào)用MapperMethod中的方法處理執(zhí)行SQL
  • 最后,在MapperMethod中根據(jù)執(zhí)行的方法返回值決定調(diào)用SqlSession中的對(duì)應(yīng)方法執(zhí)行SQL

 本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • SpringBoot整合MongoDB完整實(shí)例代碼

    SpringBoot整合MongoDB完整實(shí)例代碼

    本文主要介紹了SpringBoot整合MongoDB完整實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java實(shí)現(xiàn)List去重的五種方法詳解

    Java實(shí)現(xiàn)List去重的五種方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java中List去重的5種方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)和參考價(jià)值,需要的小伙伴可以了解一下
    2022-10-10
  • Mybatis批量刪除多表

    Mybatis批量刪除多表

    MyBatis的作用我想不用多說(shuō),今天說(shuō)說(shuō)MyBatis中的批量刪除操作。 需要的朋友一起看看吧
    2017-10-10
  • 解決阿里云OSS使用URL無(wú)法訪問(wèn)圖片的兩種方法

    解決阿里云OSS使用URL無(wú)法訪問(wèn)圖片的兩種方法

    這篇文章主要介紹了解決阿里云OSS使用URL無(wú)法訪問(wèn)圖片的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • hotspot解析jdk1.8?Unsafe類park和unpark方法使用

    hotspot解析jdk1.8?Unsafe類park和unpark方法使用

    這篇文章主要為大家介紹了hotspot解析jdk1.8?Unsafe類park和unpark方法使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • mybatis查詢返回Map<String,Object>類型的講解

    mybatis查詢返回Map<String,Object>類型的講解

    這篇文章主要介紹了mybatis查詢返回Map<String,Object>類型的講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java文件復(fù)制多種方法實(shí)例代碼

    Java文件復(fù)制多種方法實(shí)例代碼

    近期用到文件復(fù)制,雖然程序很簡(jiǎn)單,因?yàn)闀r(shí)間久了淡忘了,所以寫一篇文章記錄一下,下面這篇文章主要給大家介紹了關(guān)于Java文件復(fù)制多種方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Java中的ConcurrentLinkedQueue松散隊(duì)列解析

    Java中的ConcurrentLinkedQueue松散隊(duì)列解析

    這篇文章主要介紹了Java中的ConcurrentLinkedQueue松散隊(duì)列解析,鏈表是松散的,鏈表節(jié)點(diǎn)并不都是有效的,允許存在無(wú)效節(jié)點(diǎn)val=null,但是只有最后一個(gè)節(jié)點(diǎn)才能next=null,需要的朋友可以參考下
    2023-12-12
  • 關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問(wèn)題

    關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問(wèn)題

    這篇文章主要介紹了關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java基礎(chǔ)之并發(fā)相關(guān)知識(shí)總結(jié)

    Java基礎(chǔ)之并發(fā)相關(guān)知識(shí)總結(jié)

    隨著摩爾定律逐步失效,cpu單核性能達(dá)到瓶頸,并發(fā)逐漸逐漸得到廣泛應(yīng)用,因而學(xué)習(xí)了解以及使用并發(fā)就顯得十分重要,但并發(fā)相關(guān)的知識(shí)比較瑣碎,不易系統(tǒng)學(xué)習(xí),因而本篇文章參照王寶令老師《Java并發(fā)編程》來(lái)勾勒出一張“并發(fā)全景圖”,需要的朋友可以參考下
    2021-05-05

最新評(píng)論

扎兰屯市| 阳城县| 宣化县| 岳阳县| 嘉荫县| 彝良县| 子洲县| 台州市| 呼玛县| 明溪县| 岑溪市| 文山县| 城固县| 香港 | 昂仁县| 温泉县| 双鸭山市| 余姚市| 京山县| 明溪县| 大港区| 白水县| 灵台县| 商水县| 凤城市| 五家渠市| 乃东县| 洛宁县| 婺源县| 南漳县| 西贡区| 陕西省| 永泰县| 克东县| 昌图县| 旺苍县| 东乌珠穆沁旗| 平潭县| 麻江县| 辽阳市| 遂宁市|