Spring WebFlux+Spring Boot:入门示例

Spring WebFlux是啥

Spring WebFlux是Spring FrameWork 5.0新增的Web框架,特点如下:

  1. 非阻塞式;
  2. 不依赖Servlet;
  3. 多种部署方式,例如:jetty,tomcat,netty等

java

源码

完整源码:https://coding.net/u/pruidong/p/SpringBoot2WebFluxDemo/git

这个例子,使用了Spring Boot + Spring WebFlux + Thymeleaf + H2 ,通过Maven构建整个项目,在pom.xml中引用了还未正式发布的Spring Boot 2.0,所以部分依赖需要从Spring的Maven仓库中获取,速度可能较慢(2017/10/25,后期会直接更新pom.xml,但不会更新文章).

使用方式如下:

首先是pom.xml ,Maven构建文件:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xyz.suancaiyu</groupId>
    <artifactId>SpringBoot2WebFluxDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>
    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
    </dependencies>
    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Dao操作类:

package xyz.suancaiyu.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import xyz.suancaiyu.pojo.Book;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
/**
 * 数据库操作.
 *
 * @author puruidong
 * @version 2017/10/21 1.0
 */
@Repository
public class BookDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 保存数据.
     *
     * @param todo 要增加的项目名称.
     */
    public void save(String todo) {
        jdbcTemplate.update("INSERT INTO Book(id,todo,createdate) VALUES(?,?,?)", new Object[]{UUID.randomUUID(), todo, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))});
    }
    /**
     * 项目列表
     *
     * @return 数据列表.
     */
    public List<Book> list() {
        return jdbcTemplate.query(" select id,todo,createdate from Book ", (RowMapper) (ResultSet resultSet, int i) -> {
            Book book = new Book(resultSet.getString("id"), resultSet.getString("todo"), resultSet.getString("createdate"));
            return book;
        });
    }
    /**
     * 删除数据.
     *
     * @param id
     * @return
     */
    public int delete(String id) {
        return jdbcTemplate.update(" delete from Book where id=? ", id);
    }
}

其余文件,可以参考:项目地址

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据