溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何手寫(xiě)mybatis框架

發(fā)布時(shí)間:2021-07-20 17:47:16 來(lái)源:億速云 閱讀:128 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“如何手寫(xiě)mybatis框架”,在日常操作中,相信很多人在如何手寫(xiě)mybatis框架問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何手寫(xiě)mybatis框架”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

手寫(xiě)mybatis框架

simple-ibatis

前言:

很久沒(méi)有更新mybatis的源碼解析了,因?yàn)樽罱趯⒆约核斫獾膍ybatis思想轉(zhuǎn)為實(shí)踐。 在學(xué)習(xí)mybatis的源碼過(guò)程中,根據(jù)mybatis的思想自己構(gòu)建了一個(gè)ORM框架 。整個(gè)代碼都是自己手動(dòng)構(gòu)造,沒(méi)有一句代碼是Copy,肯定不如谷歌大神那樣的代碼,但已基本實(shí)現(xiàn)了SQL語(yǔ)句的實(shí)現(xiàn)和對(duì)象關(guān)系映射功能。若對(duì)源碼感興趣,可加入我一起寫(xiě)這個(gè)項(xiàng)目。后文會(huì)附帶地址,若覺(jué)得不錯(cuò),希望手動(dòng)star下哦! 項(xiàng)目地址:simple-ibatis

項(xiàng)目簡(jiǎn)介:

simple-batis是自己編寫(xiě)的一個(gè)簡(jiǎn)單ORM框架。在學(xué)習(xí)mybatis源碼時(shí),有感而發(fā)。耗時(shí)3周左右,基本滿足了一些常用的Sql操作本項(xiàng)目所涉及的代碼都是個(gè)人所寫(xiě),沒(méi)有一句copy,肯定不是很完善,大家理解下,后續(xù)有時(shí)間會(huì)一直更新。如果你對(duì)源碼感興趣,也可以加入一起,將自己的理解轉(zhuǎn)為代碼真的會(huì)加深印象。 代碼運(yùn)行默認(rèn)在java8上,因?yàn)橛玫搅藚?shù)反射,所以在idea中記得開(kāi)啟parameters;

開(kāi)啟步驟如下 1.File->Settings->Build,Execution,Deployment->Compiler->Java Compiler 2.在 Additional command line parameters: 后面填上 -parameters 如何手寫(xiě)mybatis框架

代碼簡(jiǎn)介:

注釋 com.simple.ibatis.annotation

@Dao 標(biāo)注在mapper類上。標(biāo)志著該類是一個(gè)mapper類,在解析時(shí)會(huì)進(jìn)行解析。

@Dao
public interface App1 {
}

@Select 標(biāo)注在mapper類中的方法上。標(biāo)志著該方法是一個(gè)Select方法,并在Select方法內(nèi)部寫(xiě)具體的sql語(yǔ)句。對(duì)于有參數(shù)注入的情況,參數(shù)使用{}進(jìn)行代替。

@Select("SELECT name from sys_user where name = {user.name} and id = {id}")
List<String> test1(User user, int id);

@Param 標(biāo)注在mapper類中的方法參數(shù)上。對(duì)參數(shù)名進(jìn)行一次重命名。若不使用此注釋,會(huì)默認(rèn)按照參數(shù)名當(dāng)做注入的元素。

@Select("SELECT name from sys_user where id = {userId}")
List<String> test2(@Param("userId") int id);

@Update 標(biāo)注在mapper類中的方法上。標(biāo)志著該方法是一個(gè)Update方法。

@Update("update sys_user set name = {user.name} where id = {user.id}")
void update3(User user);

@Insert 標(biāo)注在mapper類中的方法上。標(biāo)注著該方法是一個(gè)Insert方法

@Insert("insert into sys_user(id,name) values ({user.id},{user.name})")
int insert4(@Param("user") User user);

@Delete 標(biāo)注在mapper類中的方法上。標(biāo)注著該方法是一個(gè)Delete方法

@Delete("delete from sys_user where id = {user.id}")
int delete5(@Param("user") User user);

數(shù)據(jù)庫(kù)注冊(cè) com.simple.ibatis.driver

DriverRegister 提供數(shù)據(jù)庫(kù)注冊(cè)功能。未避免重復(fù)注冊(cè),內(nèi)部使用了一個(gè)緩存。

數(shù)據(jù)源 com.simple.ibatis.datasource

NormalDataSource 普通數(shù)據(jù)源,沒(méi)有池化的功能,提供獲取數(shù)據(jù)庫(kù)連接的功能。 PoolDataSource 池化數(shù)據(jù)源,存放著活躍連接列表和空閑連接列表。并對(duì)獲取連接和釋放連接做了一系列操作。 PoolConnection 連接的包裝類,除了存放真實(shí)連接外,還存放此連接被獲取時(shí)間,用于判斷連接是否超時(shí)。

核心類 com.simple.ibatis.core

Config 全局核心類,存放數(shù)據(jù)源,mapper包地址,mapper類解析文件 MapperCore mapper類解析文件 SqlSource 具體的sql語(yǔ)句封裝

代理類 com.simple.ibatis.mapping

MapperProxy mapper接口代理類。使用動(dòng)態(tài)代理技術(shù)

執(zhí)行器類 com.simple.ibatis.execute

Executor 執(zhí)行器接口 SimpleExecutor 具體執(zhí)行器,執(zhí)行具體的sql方法。生成結(jié)果 ExecutorFactory 生成Executor的工廠類

反射類 com.simple.ibatis.reflect

ClassWrapper 類加強(qiáng)器,封裝了Object的get和set方法。 ObjectWrapper 對(duì)象包裝類。調(diào)用ObjectWrapper.setVal和getVal就可以設(shè)置和獲得屬性。不需要顯示的調(diào)用對(duì)象的getxxx和setxxx方法。 ObjectWrapperFactory 對(duì)象包裝類生成器

處理器類 com.simple.ibatis.statement

PreparedStatementHandle PreparedStatement生成器。將java屬性轉(zhuǎn)為jdbc屬性并注入。 ResultSetHandle 對(duì)查詢結(jié)構(gòu)ResultSet進(jìn)行解析,轉(zhuǎn)換為Java類型

工具類 com.simple.ibatis.util

PackageUti 解析包的工具類 TypeUtil 類型判斷的工具類

操作示例:

1. 構(gòu)建pojo文件(并在數(shù)據(jù)庫(kù)中建立該表)

public class User {
private int id;

private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

2. 構(gòu)建mapper文件

package com.simple.ibatis.mapper;

@Dao
public interface App1 {

@Select("SELECT * from sys_user")
List<User> select2();

@Select("SELECT name from sys_user where name = {user.name} and id = {id}")
List<String> select3(User user, @Param("id") int id);

@Update("update sys_user set name = {user.name} where id = {user.id}")
void update4(User user);

@Insert("insert into sys_user(id,name) values ({user.id},{user.name}) ")
int insert5(@Param("user") User user);
}

3. 構(gòu)建數(shù)據(jù)源和執(zhí)行器工廠類:

PoolDataSource poolDataSource = new PoolDataSource("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/auth","root","root");
/**輸入mapper文件所在包及數(shù)據(jù)源*/
ExecutorFactory executorFactory = new ExecutorFactory("com/simple/ibatis/mapper",poolDataSource);

4. 操作:

 /**拿到具體的mapper代理類*/
        SimpleExecutor executor = executorFactory.getExecutor();
        App1 app1 = executor.getMapper(App1.class);

        /**構(gòu)建查詢條件*/
        User user = new User();
        user.setName("xiabing");
        user.setId(1);
        /**調(diào)用插入方法*/
        int count = app1.insert5(user);

        /**調(diào)用更新方法*/
        user.setName("root");
        app1.update4(user);

        /**查詢用戶名,返回字符*/
        List<String> users = app1. select3(user,3);
        System.out.println(users.get(0));

       /**查詢用戶,返回對(duì)象*/
       List<User> userLists = app1.select2();
       System.out.println(userLists.get(0).getName());

如何手寫(xiě)mybatis框架

5. 框架待完善:

  1. 目前該框架僅支持注解注入SQL語(yǔ)句,不支持XML注入SQL語(yǔ)句

  2. 目前對(duì)象屬性不支持集合類,對(duì)象中也不支持嵌套非基本數(shù)據(jù)類型

  3. 框架中沒(méi)有加入緩存

  4. 解析mapper文件僅支持一個(gè)包下的mapper文件

到此,關(guān)于“如何手寫(xiě)mybatis框架”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI