黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 MyBatis分頁插件原理

MyBatis分頁插件原理

更新時間:2022-10-08 09:53:23 來源:動力節點 瀏覽1614次

插件介紹

Mybatis 作為一個被廣泛使用的 ORM 開源框架,具有很大的靈活性,在四個組件(Excutor、StatementHandler、ParameterHandler、ResultHandler)處理簡單易用的插件擴展機制,Mybatis 對 Long 層的操作是借助四個核心對象。Mybatis支持用插件攔截四個核心對象,是Mybatis插入一個就是攔截器,增強核心對象的功能,增強本質上是借助底層動態代理來實現的, 也就是說,Mybatis 中的四個對象都是代理對象

Mybatis 允許的攔截方式如下:

 actuator Executor(update、query、commit、rollback Other methods );
Parameter processor ParameterHandler(getParameterObject、setParameters Method );
Result set processor ResultSetHandler(handleResultSets、handleOutputParameters Other methods );
SQL Syntax builder StatementHandler(prepare、parameterize、batch、update、query Other methods );

原理

當四個對象被創建時,

 1. Each created object is not returned directly , It is interceptorChain.pluginAll(parameterHandler);
2. All that you get interceptor( Interceptor ){ The interface that the plug-in needs to implement }; call interceptor.plugin(target);
return target Packed object ;
3. Plug-in mechanism , We can use the plug-in as the target object ;AOP( Face to face ) Our plug-in can create proxy objects for four objects
, The proxy object can intercept each execution of the four objects ;

簡單攔截 Executor 的具體實現類獲取代碼:

 public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? this.defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Object executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (this.cacheEnabled) {
 // Second level cache Executor
executor = new CachingExecutor((Executor)executor);
}
// Every Executor example , Will be passed by interceptorChain To deal with 
Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
return executor;
}
 In the actual development process , We use it a lot Mybaits Plugins are paging plugins ,
Through the paging plug-in, we can write without count Statement and limit Of In this case, you can get paging
Later data , It brings us great convenience in development . Except paging , Plug in usage scenarios mainly include
Update the common fields of the database , Sub database and sub table , Encryption and decryption, etc .

自定義插件

1.Mybatis插件接口-interceptor

intercept 方法,

插件插件方法的核心方法,生成

setProperties 方法的目標 Proxy 對象,傳遞插件所需的參數

2.自定義插件

設計并實現一個自定義插件:

package com.lg.plugin;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import java.sql.Connection;
import java.util.Properties;
@Intercepts({
 // Notice the curly braces , In other words, there can be more @Signature Intercept multiple places , All use this interceptor 
@Signature(type = StatementHandler.class, // Which class to intercept 
method = "prepare", // Which method to intercept 
args = {
Connection.class,Integer.class}) // To avoid intercepting overloaded methods , Pass in interception parameters , The input parameter must be consistent with the input parameter type of the interception method 
})
public class MyPlugin implements Interceptor {
// As can be seen from the above principles , When the target method of the target object is executed , Every time I do intercept Method 
public Object intercept(Invocation invocation) throws Throwable {
System.out.println(" Enhance the method ...");
return invocation.proceed();
}
// The target class object that will be intercepted , As the reference , Incoming interceptors , The interceptor acts as a proxy , Generate proxy objects 
/** * Packaging target object , Create a proxy object for the target object * @param target For intercepted objects * @return Proxy object */
public Object plugin(Object target) {
return Plugin.wrap(target,this);
}
// Get the properties of the configuration file 
// When the plug-in is initialized, call , It's only called once , The properties of plug-in configuration come in from here 
public void setProperties(Properties properties) {
System.out.println(" The parameter received is ==>" + properties);
}
}

Mybatis插件機制-pageHelper

開發步驟:

導入通用 PageHelper 坐標

在核心配置文件PageHelper插件中配置mybatis

測試尋呼數據采集

1.導入通用PageHelper坐標

 <!-- Paging assistant -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>

2.核心配置文件PageHelper插件中的mabatis配置

<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>

3.測試分頁代碼實現

 // Test paging 
@Test
public void testPage(){
PageHelper.startPage(1,3);
StuDao mapper = sqlSession.getMapper(StuDao.class);
List<Stu> stus = mapper.selectList();
for (Stu stu:
stus) {
System.out.println(stu.toString());
}
PageInfo<Stu> stuPageInfo = new PageInfo(stus);
System.out.println(" Total page number ==>" + stuPageInfo.getPages());
System.out.println(" Total number of articles ==>" + stuPageInfo.getTotal());
System.out.println(" The current page ==>" + stuPageInfo.getPageNum());
}

結果 :

Mybatis Universal 插件 Mapper

什么是通用映射器?

Universal Mapper 這個是為了解決單表的增刪改查問題,基于Mybatis的插件機制,開發者不用寫sql,也不需要DAO里面的Add方法,直接寫實體類,可以支持相應的增刪改查方法

如何使用 ?

1.導入通用映射器依賴

 <!-- Universal Mapper-->
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.5</version>
</dependency>

2.在核心配置文件mapper插件中的mabatis配置

<plugin interceptor="tk.mybatis.mapper.mapperhelper.MapperInterceptor">
<!-- Specifies the current generic mapper Which interface is used -->
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>

3.設置實體類的主鍵,設置表名

package com.lg.pojo;
import javax.persistence.*;
/** * Student information sheet */
@Table(name = "Stu") // Map a table 
public class Stu {
@Override
public String toString() {
return "Stu{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", gender='" + gender + '\'' +
", phone='" + phone + '\'' +
", hobby='" + hobby + '\'' +
", info='" + info + '\'' +
'}';
}
@Id // Corresponding to the annotation id
@GeneratedValue(strategy = GenerationType.IDENTITY) // mysql yes IDENTITY Self increasing ,Oracle yes SEQUENCE Sequence 
private int sid;
@Column(name = "sname") // Consistent names , No need to write 
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}

4.定義是通用的Mapper

// Just inherited Mapper Interface can 
public interface StuMapper_Plugin extends Mapper<Stu> {
}

測試班:

// Test common Mapper plug-in unit 
@Test
public void testMapper(){
StuMapper_Plugin mapper = sqlSession.getMapper(StuMapper_Plugin.class);
Stu stu = new Stu();
stu.setSid(313);
Stu stu1 = mapper.selectOne(stu);
System.out.println(stu1.toString());
// 2. Example Method 
Example example = new Example(Stu.class);
example.createCriteria().andEqualTo("sid",313);
mapper.selectByExample(example);
}

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日韩爽爽爽视频免费播放 | 亚洲乱乱 | 丁香六月婷婷综合 | 亚洲经典在线中文字幕 | 亚洲成综合人影院在院播放 | 亚洲伦理视频 | 最新国产精品亚洲二区 | 香蕉视频网站在线 | 欧美一区二区激情三区 | 成人三级网址 | www精品一区二区三区四区 | 天天艹天天操 | 国产麻豆媒一区一区二区三区 | 久久天堂 | 黄色一区二区三区 | 亚洲色五月| 全黄性性激高免费视频 | 久久精品99视频 | 天天干天天在线 | 日本三级特黄三级 | 欧美高清在线精品一区 | 国产一二三四区在线观看 | 久久精品九九 | 欧美丝袜制服 | 男女男在线观看视频网站 | 啪啪日韩| 黄色小毛片 | 欧美成人www在线观看网页 | 亚洲欧美91 | 国产a久久精品一区二区三区 | 日韩一区二区三区不卡 | 亚洲动漫精品 | 欧美爆操 | 免费一看一级毛片人 | 欧美性视频一区二区三区 | 欧美有码在线观看 | 亚洲精品福利 | 中文字幕小明 | 国产2区 | 黄色小视频观看 | 亚洲国产成人久久综合一区77 |