• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MyBatis R2DBC Adapter

MyBatis R2DBC Adapter

将MyBatis Reactive化,将底层的JDBC替换为R2DBC,从而实现全面异步化,提升数据库访问性能。

MyBatis R2DBC工作原理

MyBatis R2DBC Structure

  • 对数据CRUD操作SqlSession接口进行Reactive化,对于R2DBC适配来说为ReactiveSqlSession
  • Mapper接口Reactive化,将函数的类型从对象和List调整为Mono和Flux
  • SQL的执行器调整为R2DBC的接口,这里我们不需要类似MyBatis的Executor机制,Reactive框架为R2DBC提供了执行调度功能
  • 结果处理和映射:R2DBC Data Types 支持,你也可以扩展R2DBCTypeHandler添加自定义类型。MyBatis R2DBC增加了JDBC Types的支持,主要是考虑兼容性。

如何使用?

和MyBatis的使用流程一样,如下:

// construct MyBatis Configuration
XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(this.getClass().getResourceAsStream("/mybatis-config.xml"));
Configuration configuration = xmlConfigBuilder.parse();

// construct Reactive SQL Session Factory
ReactiveSqlSessionFactory reactiveSqlSessionFactory = new DefaultReactiveSqlSessionFactory(configuration);
ReactiveSqlSession reactiveSqlSession = reactiveSqlSessionFactory.openSession();

// construct Reactive Mapper interface
UserReactiveMapper userMapper = reactiveSqlSession.getMapper(UserReactiveMapper.class);

特性

  • 兼容MyBatis 3的配置和编程接口,主要是Mapper接口和SQL的XML配置方式
  • XxxMapper 调整为 Reactive 接口
  • Metrics support: 每一个statement的metrics都可以被记录
  • R2DBC Pool支持
  • Dynamic SQL支持

核心工作

  • 将底层的JDBC调整为R2DBC
  • 兼容 Spring Data R2DBC
  • r2dbc-mysql的兼容性测试
  • TypeHandler支持

主要调整点

  • Reactive SqlSessionFactory
  • Reactive SqlSession
  • Reactive Mapper interface

R2DBC-MyBatis支持的数据类型

R2DBC有标准的Data Types支持,可以在 https://r2dbc.io/spec/0.8.1.RELEASE/spec/html/#datatypes 进行查阅

考虑到MyBatis JDBC支持的类型,以下的类型也是被支持的:

  • Java Date: java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp

R2DBC Transaction

Please use R2dbcTransactionManager from Spring Data R2DBC

MySQL的R2DBC Driver

从目前开发的情况来看,可能MariaDB R2DBC更具有前景一些。

TypeHandler支持

MyBatis针对resultMap的result有一个typeHandler的设置,可以进行Java对象字段和数据库表的column直接进行值转换。 在MyBatis R2DBC中我们保留这一特性。 由于R2DBC和JDBC的API完全不同,所以新增了一个R2DBCTypeHandler接口,代码如下:

public interface R2DBCTypeHandler<T> {

    void setParameter(Statement statement, int i, T parameter, JdbcType jdbcType) throws R2dbcException;

    T getResult(Row row, String columnName, RowMetadata rowMetadata) throws R2dbcException;

    T getResult(Row row, int columnIndex, RowMetadata rowMetadata) throws R2dbcException;

    Class<?> getType();
}

但是考虑到实际的MyBatis的配置验证,新建的TypeHandler实现需要同时继承TypeHandler和R2DBCTypeHandler接口,同时也保证在JDBC和R2DBC同时生效。

你可以参考 org.apache.ibatis.r2dbc.type 开发包下的type handler进行编写,同时JDBC的时间类型的兼容,也是通过type handler完成的。

Java Enum type handler和MyBatis处理机制类似,只是类名调整到R2DBC下的EnumOrdinalTypeHandler,代码如下:

 <typeHandlers>
        <!-- Enum ordinal type handler-->
        <typeHandler handler="org.apache.ibatis.r2dbc.type.EnumOrdinalTypeHandler"
                     javaType="java.math.RoundingMode"/>
 </typeHandlers>

注意: @MappedJdbcTypes类型的TypeHandler不支持

Transaction支持

Reactive Transaction 支持 https://spring.io/blog/2019/05/16/reactive-transactions-with-spring

Spring的R2dbcTransactionManager是基于R2DBC的ConnectionFactory,所以我们不需要进行什么配置,基本代码如下:

public class R2dbcTransactionManagerAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(ReactiveTransactionManager.class)
	public R2dbcTransactionManager connectionFactoryTransactionManager(ConnectionFactory connectionFactory) {
		return new R2dbcTransactionManager(connectionFactory);
	}

}

如果是基于R2DBC Pool,R2dbcTransactionManager就是基于ConnectionPool的,当然ConnectionPool就是继承自ConnectionFactory的。

配置参数

和R2DBC相关的配置参数,主要是通过MyBatis的mybatis-config.xml文件中的properties配置完成的,如下:

<configuration>
    <properties>
        <property name="metrics.enabled" value="true"/>
        <property name="r2dbc.pool.initial-size" value="1"/>
        <property name="r2dbc.pool.max-size" value="10"/>
        <!-- max idle time, the unit is Minute-->
        <property name="r2dbc.pool.max-idle-time" value="5"/>
    </properties>
</configuration>

R2DBC MyBatis的不同点

背后的原因其实主要是JDBC和R2DBC的区别,我们都知道JDBC同步Block的,所以我们需要有连接池DataSource,防止应为JDBC的等待造成Thread的堵塞。

SqlSession ReactiveSqlSession

MyBatis的一个核心是SqlSession,但是两者的设计还有很大的区别的:

  • MyBatis的原生SqlSession是基于JDBC,是Block的,所以存在SessionHolder的感念,如commit,rollback, close等API
  • ReactiveSqlSession则是 异步 + 单例(Singleton)的,和Spring的WebClient,Spring Data R2DBC的DatabaseClient一样的,只是名字还是叫做Session,和MyBatis一致而已。

DataSource和R2DBC Pool

JDBC使用连接池这个大家都明白, 通过增加连接数减少因为JDBC Connection等待的结果造成的block,不然应用整体的性能下降的非常快。 在R2DBC中也有Connection Pool的概念,其主要是兼容传统数据库同步协议,如MySQL,解释如下:

Traditionally, many MySQL drivers used a synchronous approach when executing SQL statements. This meant that operations such as opening connections and executing queries were blocked until completion, which could take a long time. To allow for parallel execution, a developer had to write a multithreaded application. Any MySQL client that supports the X Protocol can provide asynchronous execution, either using callbacks, Promises, or by explicitly waiting on a specific result at the moment in time when it is actually needed.

如果数据库通讯协议是全部异步的,那么是没有必要介入Connection Pool的。 如mariadb-connector-r2dbc的实现中就有:

allowPipelining: Permit to send queries to server without waiting for previous query to finish

如果allowPipelining为false,这个时候,出于性能的考虑,你还是需要介入R2DBC Pool来提升应用性能。

Pipelining versus Parallel Query Execution请参考: https://www.percona.com/blog/2016/07/06/pipelining-versus-parallel-query-execution-with-mysql-5-7-x-plugin/

Cache特性

对于Reactive来说,默认就包含了cache特性,也就是你可以调用 Mono.cache() 或 Flux.cache 就可以缓存响应的结果集,然后提供给后续的订阅者消费。

对应MyBatis来说,cache也并复杂,Cache也是基于Mapper的,我们只需要基于paramObject缓存对应的Mono或者Flux对象就可以。

 Mono<String> user = Mono.defer(() -> {
            // r2dbc operation
            return r2dbcResult;
        }).cache(Duration.ofSeconds(2));

但是,这个只是基于内存的对象缓存,如果是分布式的,上述机制不能工作,这涉及到对象序列化和反序列化机制的机制。

至于选择哪一种,这个可能要根据实际的缓存类型进行决定。

spring-boot-starter-data-r2dbc整合

如果你项目中已经包含spring-boot-starter-data-r2dbc,那么你可以完全使用Spring Boot提供的R2DBC的ConnectionFactory bean完成数据库连接相关的任务。

@Bean
public ReactiveSqlSessionFactory reactiveSqlSessionFactory(ConnectionFactory r2dbcConnectionFactory)  {
  XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(this.getClass().getResourceAsStream("/mybatis-config.xml"));
  Configuration configuration = xmlConfigBuilder.parse();
  return new DefaultReactiveSqlSessionFactory(configuration, r2dbcConnectionFactory);
}

不支持的特性

  • MyBatis Plugin机制: org.apache.ibatis.plugin.Interceptor
  • 不支持配置文件中声明Reactive Mapper接口,R2DBC需要额外的API支持
<mappers>
  <mapper class="org.mybatis.builder.AuthorReactiveMapper"/>
</mappers>

参考

More Repositories

1

jenv

Java enVironment Manager
Shell
603
star
2

chatgpt-spring-boot-starter

Spring Boot ChatGPT Starter
Java
481
star
3

spring-boot-dubbo

Spring Boot with Dubbo support
Java
278
star
4

dubbo3

Dubbo3: distributed RPC framework from Alibaba Dubbo2
Java
255
star
5

ddd-base

DDD(Domain Driven Design) base package for java
Java
255
star
6

awesome-rsocket

Awesome RSocket
190
star
7

weixin-robot-java

微信公共平台机器人Java SDK
Java
119
star
8

plantuml-gist

PlantUML with Github Gist and Gitlab Support
Java
96
star
9

dx

A tool and task runner for writing better scripts with Deno
TypeScript
93
star
10

zookeeper-intellij

IntelliJ IDEA ZooKeeper Plugin
Java
62
star
11

spring-boot-starter-redisson

Spring Boot starter for Redisson
Java
58
star
12

h2-functions-4-mysql

H2 functions for MySQL
Java
57
star
13

spring-boot-starter-okhttp3

Spring Boot Starter for OkHttp3
Java
55
star
14

spring-boot-starter-httpclient

Spring Boot Starter for HttpClient
Java
54
star
15

vitest-jetbrains-plugin

Vitest JetBrains plugin
Kotlin
52
star
16

spring-boot-rsocket-demo

Spring Boot RSocket Demo with RPC style
Java
42
star
17

spring-boot-thymeleaf3-bootstrap4-angular4

Spring Boot Kotlin Application Template for Thymeleaf3, BootStrap4 and Angularjs 4
TypeScript
42
star
18

toolchains-maven-plugin

Toolchains Maven Plugin with JDK auto download
Java
36
star
19

xtermjs-spring-boot-starter

Web terminal to manage your Spring Boot app
Java
30
star
20

spring-cloud-function-demo

Spring cloud function with RSocket
Java
24
star
21

spring-boot-starter-wechatmp

Spring Boot with Wechat Support
Java
23
star
22

ngrok-spring-boot-starter

Easy to expose a local Spring Boot Application to the internet by ngrok
Java
23
star
23

smarty4j

Smarty Template Engine for Java
Java
22
star
24

spring-boot-starter-oss

Spring Boot Start with Aliyun OSS
Java
19
star
25

reactive-demo

Reactive Demo with Reactor, RxJava and reactive-stream
Java
19
star
26

jetbrains-just-plugin

JetBrains Just Command Runner Plugin
Kotlin
19
star
27

cli-completion

CLI completion for bash, zsh, fish and powershell
Rust
18
star
28

spring-boot-microservices

Spring Boot MicroServices
Java
18
star
29

spring-boot-starter-shell

Spring Shell with Spring Boot Driven
Java
17
star
30

chrome_js_api_stub

Chrome JavaScript API Stub to assistant extension development in IDE
JavaScript
16
star
31

rsocket-load-balancing

RSocket load balancing based on Spring Cloud Service Registry
Java
15
star
32

kotlin-wasm-demo

Kotlin WebAssembly Demo
Kotlin
15
star
33

spring-boot-kotlin-microservices

Spring Boot MicroServices with Kotlin
Kotlin
14
star
34

task-keeper

A cli to manage tasks from different task runners or package managers
Rust
13
star
35

open2internet-spring-boot-starter

Expose local Spring Boot Application to internet for demonstration, testing and debuging
Java
13
star
36

ali-oss-shell

Aliyun OSS Shell
Java
12
star
37

easy-random-junit5-extension

JUnit 5 extension for easy-random
Java
12
star
38

rocker-template-demo

Rocker template demo with Spring Boot
Java
12
star
39

mail-catcher

MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface
Java
12
star
40

kotlin-platform-bom

Kotlin Platform BOM
Just
11
star
41

axum-demo

Axum demo
Rust
11
star
42

rsocket-deno

🦕RSocket Deno module
TypeScript
11
star
43

r2dbc-demo

R2DBC demo
Java
11
star
44

oh-my-zsh-spring-boot-plugin

oh-my-zsh Spring Boot plugin
10
star
45

team

Team Instruction
10
star
46

java-error-messages-wizard

Java Error Message Wizard
Java
10
star
47

ddd-demo

DDD demo with Java 8 and Spring Boot
Java
9
star
48

jenv-ps

jenv Power Shell Edition
PowerShell
9
star
49

nats-spring-boot-starter

Spring Boot starter for NATS
Java
9
star
50

struts2-spring-boot-integration

Spring Boot with Struts 2.5.x Integration
Java
8
star
51

kafka-demo

Kafka Demo with Spring Boot, Docker and Terraform
Java
8
star
52

rest-editor-client-contrib

REST Editor Client Contrib plugin for IntelliJ IDEA
Java
8
star
53

jenv-idea-plugin

jenv IntelliJ IDEA plugin
Java
8
star
54

mybatis-demo

MyBatis Demo with spring-boot-starter-mybatis integration
Java
8
star
55

platform-bom-contrib

Spring Platform IO BOM Contrib
Java
8
star
56

java9-in-action

Java 9 in Action
Java
7
star
57

aliyun-openapi-rust-sdk

Aliyun OpenAPI SDK for Rust
Rust
7
star
58

tgm

Template generator management
Rust
7
star
59

npm-export-spring-boot-starter

Spring Boot Starter to generate npm package to call REST API
Java
7
star
60

xterm-demo

Xterm.js with RSocket
JavaScript
7
star
61

apache-pulsar-demo

Apache Pulsar demo
Java
7
star
62

http2-java-demo

Spring Boot with HTTP/2
Java
7
star
63

spring-boot-apache-dubbo

Spring Boot 2.0 with Apache Dubbo
Java
7
star
64

markdown-chatgpt

Talk to ChatGPT from Markdown
Kotlin
7
star
65

trpc-spring-boot-starter

Spring Boot Starter for tRPC
Java
7
star
66

reactive-grpc-demo

gRPC with reactive Reactor
Java
6
star
67

unitTest_templates

unitTest IntelliJ IDEA plugin templates
6
star
68

proto-rsocket-plugin

Protobuf to RSocket service interface
Java
6
star
69

grpc-kotlin-demo

gRPC with Kotlin Demo
Kotlin
6
star
70

java11_in_action

Java 11 in Action
Java
6
star
71

markdown-wasm

Markdown wasm based on md4c for Deno 🦕
TypeScript
6
star
72

kotlin-deno-example

🦕Deno app development with Kotlin
Kotlin
6
star
73

zshmarks

zsh marks for oh-my-zsh
6
star
74

trunk-demo

WebAssembly development with Trunk & Vite.js
Rust
6
star
75

reactive-grpc-spring-boot-starter

Spring Boot Starter for Reactive gRPC
Java
6
star
76

hessian-spring-boot-starter

Spring Boot Start for Hessian
Java
6
star
77

alibaba-broker-example-parent

Alibaba RSocket Broker Example
Java
6
star
78

akka-demo

Akka Demo with Kotlin, Spring Integration
Java
5
star
79

intellij-templates

IntelliJ IDEA live templates
5
star
80

zookeeper-shell

New ZooKeeper shell just like bash
Java
5
star
81

embind_demo

Emscripten embind demo
C++
5
star
82

graal-native-image-demo

Graal native image demo with RSocket and Netty
Java
5
star
83

dkim-spring-boot-starter

DKIM for spring-boot-starter-mail
Java
5
star
84

akka-spring-boot-starter

akka-spring-boot-starter
Java
5
star
85

rsocket-dart

RSocket Dart SDK
Dart
4
star
86

deno-rust-wasm-demo

Deno with Rust Wasm
TypeScript
4
star
87

rest-graphql-rsocket-demo

One controller to combine HTTP REST, GraphQL and RSocket
Java
4
star
88

aria2-shell

Aria2 Shell
Java
4
star
89

spring-boot-starter-job-controller

Spring Boot with Job Controller
Java
4
star
90

aeron-demo

Aeron Spring Reactive Demo
Java
4
star
91

rsocket-transport-quic

RSocket QUIC transport
Java
4
star
92

spring-cloud-aliyun

Integration for Aliyun Cloud Services APIs with Spring Cloud
Shell
4
star
93

jvmtop

Clone from https://code.google.com/p/jvmtop
Java
4
star
94

kotlin-wasm-browser-demo

Kotlin WebAssembly demo for Browser
Kotlin
4
star
95

deno-httpx

httpx: cli to run requests from http file
TypeScript
4
star
96

kotlin-coroutine-demo

Kotlin Coroutines demo
Kotlin
4
star
97

spring-boot-polyglot-demo

Spring Boot Polyglot with Kotlin, Java + Lombok + Groovy
Kotlin
4
star
98

esnippet_idea_plugin

eSnippet IntelliJ IDEA plugin
Java
4
star
99

microservices-annotator

Java Annotations for Microservices annotator to mark your code
Java
3
star
100

logback-fluentd-appender

Logback Fluentd Appender
Java
3
star