<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
Spring boot3已經M1了,最近群佬們也開始蠢蠢欲動的開始整活Reactive+Spring Boot3,跟著大家的步伐,我也來整一篇工程入門,我們將用java17+Spring Boot3+r2dbc+Reactive棧來講述,歡迎大家來討論。(關於響應式,請大家非同步到之前的文章裡,有詳細介紹。)
Reactor還有基於其之上的Spring WebFlux框架。包括vert.x,rxjava等等reactive技術。我們實際上在應用層已經有很多優秀的響應式處理框架。
但是有一個問題就是所有的框架都需要獲取底層的資料,而基本上關係型資料庫的底層讀寫都還是同步的。
為了解決這個問題,出現了兩個標準,一個是oracle提出的 ADBC (Asynchronous Database Access API),另一個就是Pivotal提出的R2DBC (Reactive Relational Database Connectivity)。
R2DBC是基於Reactive Streams標準來設計的。通過使用R2DBC,你可以使用reactive API來運算元據。
同時R2DBC只是一個開放的標準,而各個具體的資料庫連線實現,需要實現這個標準。
今天我們以r2dbc-h2為例,講解一下r2dbc在Spring webFlux中的使用。
以下是 pom.xml清單
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0-M1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>wang.datahub</groupId> <artifactId>springboot3demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot3demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-groovy-templates</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-hateoas</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-h2</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <!-- <version>3.4.14</version>--> <!-- <scope>compile</scope>--> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories> </project>
這裡我們只設定了r2dbc連結資訊
用於設定預設連結,建立初始化資料
package wang.datahub.springboot3demo.config; import io.netty.util.internal.StringUtil; import io.r2dbc.spi.ConnectionFactories; import io.r2dbc.spi.ConnectionFactory; import io.r2dbc.spi.ConnectionFactoryOptions; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import reactor.core.publisher.Flux; import static io.r2dbc.spi.ConnectionFactoryOptions.*; @Configuration @ConfigurationProperties(prefix = "r2dbc") public class DBConfig { private String url; private String user; private String password; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Bean public ConnectionFactory connectionFactory() { System.out.println("url ==> "+url); ConnectionFactoryOptions baseOptions = ConnectionFactoryOptions.parse(url); ConnectionFactoryOptions.Builder ob = ConnectionFactoryOptions.builder().from(baseOptions); if (!StringUtil.isNullOrEmpty(user)) { ob = ob.option(USER, user); } if (!StringUtil.isNullOrEmpty(password)) { ob = ob.option(PASSWORD, password); } return ConnectionFactories.get(ob.build()); } @Bean public CommandLineRunner initDatabase(ConnectionFactory cf) { return (args) -> Flux.from(cf.create()) .flatMap(c -> Flux.from(c.createBatch() .add("drop table if exists Users") .add("create table Users(" + "id IDENTITY(1,1)," + "firstname varchar(80) not null," + "lastname varchar(80) not null)") .add("insert into Users(firstname,lastname)" + "values('Jacky','Li')") .add("insert into Users(firstname,lastname)" + "values('Doudou','Li')") .add("insert into Users(firstname,lastname)" + "values('Maimai','Li')") .execute()) .doFinally((st) -> c.close()) ) .log() .blockLast(); } }
建立使用者bean
package wang.datahub.springboot3demo.bean; import org.springframework.data.annotation.Id; public class Users { @Id private Long id; private String firstname; private String lastname; public Users(){ } public Users(Long id, String firstname, String lastname) { this.id = id; this.firstname = firstname; this.lastname = lastname; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } @Override public String toString() { return "User{" + "id=" + id + ", firstname='" + firstname + ''' + ", lastname='" + lastname + ''' + '}'; } }
dao程式碼清單如下,包含查詢列表、按id查詢,以及建立使用者等操作
package wang.datahub.springboot3demo.dao; import io.r2dbc.spi.Connection; import io.r2dbc.spi.ConnectionFactory; import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; import org.springframework.data.relational.core.query.Query; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import wang.datahub.springboot3demo.bean.Users; import static org.springframework.data.r2dbc.query.Criteria.where; import static org.springframework.data.relational.core.query.Query.query; @Component public class UsersDao { private ConnectionFactory connectionFactory; private R2dbcEntityTemplate template; public UsersDao(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; this.template = new R2dbcEntityTemplate(connectionFactory); } public Mono<Users> findById(long id) { return this.template.selectOne(query(where("id").is(id)),Users.class); // return Mono.from(connectionFactory.create()) // .flatMap(c -> Mono.from(c.createStatement("select id,firstname,lastname from Users where id = $1") // .bind("$1", id) // .execute()) // .doFinally((st) -> close(c))) // .map(result -> result.map((row, meta) -> // new Users(row.get("id", Long.class), // row.get("firstname", String.class), // row.get("lastname", String.class)))) // .flatMap( p -> Mono.from(p)); } public Flux<Users> findAll() { return this.template.select(Users.class).all(); // return Mono.from(connectionFactory.create()) // .flatMap((c) -> Mono.from(c.createStatement("select id,firstname,lastname from users") // .execute()) // .doFinally((st) -> close(c))) // .flatMapMany(result -> Flux.from(result.map((row, meta) -> { // Users acc = new Users(); // acc.setId(row.get("id", Long.class)); // acc.setFirstname(row.get("firstname", String.class)); // acc.setLastname(row.get("lastname", String.class)); // return acc; // }))); } public Mono<Users> createAccount(Users account) { return Mono.from(connectionFactory.create()) .flatMap(c -> Mono.from(c.beginTransaction()) .then(Mono.from(c.createStatement("insert into Users(firstname,lastname) values($1,$2)") .bind("$1", account.getFirstname()) .bind("$2", account.getLastname()) .returnGeneratedValues("id") .execute())) .map(result -> result.map((row, meta) -> new Users(row.get("id", Long.class), account.getFirstname(), account.getLastname()))) .flatMap(pub -> Mono.from(pub)) .delayUntil(r -> c.commitTransaction()) .doFinally((st) -> c.close())); } private <T> Mono<T> close(Connection connection) { return Mono.from(connection.close()) .then(Mono.empty()); } }
controller程式碼清單如下,包含了查詢列表、按id查詢,以及建立使用者等操作
package wang.datahub.springboot3demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import wang.datahub.springboot3demo.bean.Users; import wang.datahub.springboot3demo.dao.UsersDao; @RestController public class UsersController { @Autowired private final UsersDao usersDao; public UsersController(UsersDao usersDao) { this.usersDao = usersDao; } @GetMapping("/users/{id}") public Mono<ResponseEntity<Users>> getUsers(@PathVariable("id") Long id) { return usersDao.findById(id) .map(acc -> new ResponseEntity<>(acc, HttpStatus.OK)) .switchIfEmpty(Mono.just(new ResponseEntity<>(null, HttpStatus.NOT_FOUND))); } @GetMapping("/users") public Flux<Users> getAllAccounts() { return usersDao.findAll(); } @PostMapping("/createUser") public Mono<ResponseEntity<Users>> createUser(@RequestBody Users user) { return usersDao.createAccount(user) .map(acc -> new ResponseEntity<>(acc, HttpStatus.CREATED)) .log(); } }
啟動類清單:
package wang.datahub.springboot3demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import wang.datahub.springboot3demo.config.DBConfig; @SpringBootApplication @EnableConfigurationProperties(DBConfig.class) public class WebFluxR2dbcApp { public static void main(String[] args) { SpringApplication.run(WebFluxR2dbcApp.class, args); } }
好了,致此我們整個 Demo 就實現完成了
參考連結:
https://zhuanlan.zhihu.com/p/299069835
到此這篇關於springboot3+r2dbc響應式程式設計實踐的文章就介紹到這了,更多相關springboot3 r2dbc響應式程式設計內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45