docker+gradle+SpringWebFlux+R2DBC+Mysql的RestfulAPI(CRUD)

前言

今天有个兄弟拿到一个笔试题,就是使用docker部署gradle构建的SpringWebFlux+R2DBC+Mysql基于Restful的增删改查,这里面gradle和SpringWebFlux以及R2DBC之前没有使用过,那么我们开始从0搭建.

一. Gradle

首先我们要知道Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。
OK直接上手

  1. 创建项目
    创建gradle项目
    直接下一步选好路径就完事,进入到这个页面,我们发现并没有写代码的地方
    在这里插入图片描述
    等1分钟后(注意要联网)出现了我们熟悉的maven的src了
    在这里插入图片描述
    那么接下来我们写个helloworld试试.
    helloGradle
    helloworld没有问题,接着我们导入springboot的包,把项目改造为springboot项目.怎么做呢?
    百度一下,你就知道Gradle入门及SpringBoot项目构建

百度完之后,我们直接复制build.gradle的配置进行导包操作

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'  //应用的插件

group = 'com.milo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories { 
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

接着刷新一下等着下jar包
在这里插入图片描述

等了6分24秒总算把jar包下好了,接着补全启动引导类和yml就可以run了.
在这里插入图片描述
第一步gradle搞定,详细教程在这:Gradle教程以后用到再学.

二. Mysql

创建数据库,表
步骤:略

三. R2DBC+SpringWebFlux

官网:https://r2dbc.io/
Spring集成:Spring Data R2DBC
SpringWebFlux
废话不多说,直接上手
配置文件build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'  //应用的插件

group = 'com.milo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '9'

repositories {

    maven { url "https://maven.aliyun.com/repository/spring" }
    mavenCentral()
}

test{
    useJUnitPlatform()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'io.r2dbc:r2dbc-pool'
    implementation 'dev.miku:r2dbc-mysql'
    implementation 'mysql:mysql-connector-java'
    implementation 'io.projectreactor:reactor-test:3.4.24'

    compileOnly 'org.projectlombok:lombok:1.18.24'
    annotationProcessor 'org.projectlombok:lombok:1.18.24'
    testCompileOnly 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

application.yml

server:
  port: 8888
spring:
  r2dbc:
    name: r2dbc
    url: r2dbcs:mysql://127.0.0.1:3306/r2dbc?serverTimezone=GMT&SSL=false&sslMode=DISABLED
    username: root
    password: root
    pool:
      enabled: true
      validation-query: SELECT 1
  application:
    name: r2dbc
logging:
  level:
    org.springframework.r2dbc: DEBUG

dao层有两种实现,第一种是自己手动写接口实现他提供的CRUD接口ReactiveCrudRepository,第二种是直接使用R2dbcEntityTemplate

手动实现dao

package com.milo.mapper;

import com.milo.entity.Person;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public interface PersonDaoRepository extends ReactiveCrudRepository<Person, String> {
    @Modifying
    @Query("update person set name=:#{[0].name},age=:#{[0].age} where id=:#{[0].id}")
    public Mono<Person> updateById(Person person);

    @Query("select * from person limit :page,:size")
    Flux<Person> findPage(Integer page, Integer size);
}

service接口

package com.milo.service;

import com.milo.entity.Person;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Auther: milo
 * @Date: 2022/12/04/20:15
 * @Description:
 */
public interface PersonService {

    public Mono<Person> insert(Person person);
    public Mono<Integer> remove(String id);
    public Mono<Integer> update(Person person);
    public Mono<Person> select(String id);
    public Flux<Person> select(Integer page, Integer size);

}

service第一种实现

package com.milo.service.impl;

import com.milo.entity.Person;
import com.milo.mapper.PersonDaoRepository;
import com.milo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Auther: milo
 * @Date: 2022/12/04/20:36
 * @Description:
 */
@Service("personServiceImplForDao")
public class PersonServiceImplForDao implements PersonService {

    @Autowired
    private PersonDaoRepository dao;

    public Mono<Person> insert(Person person) {
        return dao.save(person);
    }

    public Mono remove(String id) {
        return dao.deleteById(id);
    }

    public Mono update(Person person) {
        return dao.updateById(person);
    }

    public Mono<Person> select(String id) {
        return dao.findById(id);
    }

    public Flux<Person> select(Integer page, Integer size) {
        return dao.findPage(page,size);
    }
    
}

service第二种实现

package com.milo.service.impl;

import com.milo.entity.Person;
import com.milo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.CriteriaDefinition;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.core.query.Update;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Auther: milo
 * @Date: 2022/12/04/20:36
 * @Description:
 */
@Service("personServiceImplForTemplate")
public class PersonServiceImplForTemplate implements PersonService {

    @Autowired
    private R2dbcEntityTemplate template;

    public Mono<Person> insert(Person person) {
        return template.insert(person);
    }

    public Mono<Integer> remove(String id) {
        Query query = Query.query(Criteria.where("id").is(id));
        return template.delete(query, Person.class);
    }

    public Mono<Integer> update(Person person) {
        CriteriaDefinition criteria = Criteria.where("id").is(person.getId());
        Query query = Query.query(criteria);
        Update update = Update.update("name", person.getName());
        return template.update(query, update, Person.class);
    }

    public Mono<Person> select(String id) {
        Query query = Query.query(Criteria.where("id").is(id));
        return template.select(query, Person.class).single();
    }

    public Flux<Person> select(Integer page, Integer size) {
        Query query = Query.empty().offset((page - 1) * size).limit(size);
        return template.select(query, Person.class);
    }
}

controller

package com.milo.web;

import com.milo.entity.Person;
import com.milo.service.PersonService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
/**
 * @Auther: milo
 * @Date: 2022/12/04/21:00
 * @Description:
 */
@RestController
@RequestMapping("person")
public class PersonController {
    //通过名字切换service实现
    //@Resource(name = "personServiceImplForDao")
    @Resource(name = "personServiceImplForTemplate")
    private PersonService personService ;

    @PostMapping("insert")
    public Mono<Person> insert(@RequestBody Person person) {
        return personService.insert(person) ;
    }

    @GetMapping("remove/{id}")
    public Mono<Integer> remove(@PathVariable("id")String id) {
        return personService.remove(id) ;
    }

    @PostMapping("update")
    public Mono<Integer> update(@RequestBody Person person) {
        return personService.update(person) ;
    }

    @GetMapping("query/{id}")
    public Mono<Person> select(@PathVariable("id") String id) {
        return personService.select(id).single() ;
    }

    @GetMapping("pager")
    public Flux<Person> select(Integer page, Integer size){
        return personService.select(page,size);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值