使用SpringBoot搭建RESTful风格的javaweb项目实现CRUD和分页功能

之前一直没写关于Springboot的文章, 因为的确Springboot配置非常简单, 而且集成度比较高, 这次我做的是一个用Springboot框架搭建的符合RESTful风格的web应用项目, 主要是实现了CRUD功能和分页功能;

这个项目的所有源代码我已经上传至github

持久层使用的是springboot jpa 基于hibernate来操作mysql数据库
控制层使用的是Springboot的Controller, 本质上还是基于SpringMVC的
视图层使用的Thymeleaf 这也是Springboot官方推荐的前端框架, 同时为了美观我使用bootstrap套了一下

最终的界面大概是这样的:
result
然后说一下我写这个应用的顺序, 首先从持久层写起, 把实体类创建好, 因为我使用的是jpa(hibernate), 所以要写好DAO类, 然后定义好Service类接口并且写好Impl的实现类, 然后想好前后端交互的时候用什么的样的url, 然后写好前端界面之后实现对应的后端控制器;

我并没有写一个类的时候就完全写完整, 比如我的DAO类是继承自JPARepository, 然后Service层中应该要包装有需要用到的方法, 但是我也不知道我以后会需要什么方法, 所以我在Service接口中并没有预先写好要用的方法, 而是先空着, 等到Controller需要用到对应的Service的时候再去写好所需要的Service类;

从头开始吧, 在idea中使用Spring Initializr新建一个Springboot项目:
maven
然后可以得到一个idea自动生成的文件结构, 可以按照我这里的文件结构新建好每一个层的包;
在这里插入图片描述

一.Springboot相应配置

应该也不用多说, 大家看一看名字就知道这些配置是什么意思了吧…
自己写的时候要把这里的数据库的数据库名和用户名密码改成自己的

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
server.servlet.context-path=/thymeleaf

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=yourusername
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql=true

然后这里最好导入一下所需要的包, 在pom层中做相应的导入即可, 我把我的pom代码也附在博客最后, 需要的可以直接复制过去, 然后这里记得要在idea中enable auto import功能;

大概就是需要导入Springboot-web的包, thymeleaf的包, springboot-jpa的包, mysql-connector的包;

二.实体类

首先写好实体类的Category:
这里用到了一些JPA的注解, 这里稍作记录:
@Entity表示这是一个实体类
@Table(name = "category_")表示这个实体类对应的数据库中的表格, 表示对应的表名, 如果表名和实体类相同, 这个注解不写也没关系
@Id表示这个是主键ID
@GeneratedValue(strategy = GenerationType.IDENTITY)表示这个主键由数据库的autoincrement自动生成, 这里还有一些其他的strategy, 可以自己查一下
@Column(name = "id")这里的两个Column是表示在表中对应的列名

package com.recluse.spider.demo.pojo;

import javax.persistence.*;

@Entity
@Table(name = "category_")
public class Category {
   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column(name = "name")
    private String name;

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }
}

在这里最好能先把数据库所需要的相应配置完成, 配置好相应的表格和一些测试用的数据, 我把需要的sql文件附在博客最后面, 需要的可以下载然后导入mysql;

三.DAO层

要实现CategoryDAO层非常简单, 因为我这里不需要使用比较复杂的sql操作, 所以用JPA自带的简单方法就够了, 这里我使用了@Repository注解来标记这是一个DAO层类, 代码如下:

package com.recluse.spider.demo.dao;

import com.recluse.spider.demo.pojo.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryDAO extends JpaRepository<Category, Integer> {
   

}

如果有需要什么复杂的查询, 这里可以使用JPA的命名方法会自动生成对应的sql语句, 然后就可以使用啦~

四.Service层

其实既然我们在DAO层没做什么复杂操作, 那么在Service层也不太需要做什么封装, 但是出于规范考虑我还是分了一下Service层, 接口代码如下:

package com.recluse.spider.demo.Service;

import com.recluse.spider.demo.pojo.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;


public interface CategoryService {
   
    Page<Category> list(Pageable pageable);
    void add(Category category);
    void delete(int id);
    Category get(int id);
    void update(Category category);
}

ServiceImpl实现类代码如下:

package com.recluse.spider.demo.Service.Impl;

import com.recluse.spider.demo.Service.CategoryService;
import com.recluse.spider.demo.dao.CategoryDAO;
import com.recluse.spider.demo.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl implements CategoryService {
   
    @Autowired
    CategoryDAO categoryDAO;

    @Override
    public Page<Category> list(Pageable pageable) {
   
        return categoryDAO.findAll(pageable);
    }

    @Override
    public void add(Category category) {
   
        categoryDAO.save(category);
    }

    @Override
    public void delete(int id) {
   
        categoryDAO.deleteById(id);
    }

    @Override
    public Category get(int id) {
   
        return categoryDAO.getOne(id);
    }

    @Override
    public void update(Category category) {
   
        categoryDAO.save(category);
    }
}

这里有一点, 首先我使用了@Service来标记这个类是Service层的内容, 这里我使用@Autowired来自动绑定CategoryDAO类用于实现Service层的对应功能;
可以看到这里的Service层其实本质上就是和DAO层差不多;

五.View层(Thymeleaf)

这里主要是listCategory.html的文件, 这个文件可以直接放在Templates文件夹下;

这里要在开头的时候使用<html xmlns:th="http://www.thymeleaf.org">来用th标记使用thymeleaf的标记;

然后我导入了jquery和bootstrap的需要的库;

这里我设计时想的是, 对于一个listCategory页面, 我会传过来一个page对象, 然后依此做对应的分页, 查询操作;

首先用th:each来实现对传过来的page.content这个列表的遍历;
然后对应地取出id和name显示出来, 在edit和delete的url我们使用的restful风格, 也就是下面这样的:

比如我这里要对Category做增删改查, 就应该符合:

Add -> /Category -> POST
Delete -> /Category/id -> DELETE
Update -> /Category -> PUT
Get -> /Category/id -> GET
List -> /Category -> GET

在这里的PUT中, 我看到过两种设计, 有的是直接对/Category进行PUT, 也有对/Category/id进行PUT的操作, 我在这里选了前者, 因为我觉得似乎后者多传一个id到服务器并没有什么意义…

然后注意这里的thymeleaf中的标准url的写法:

<a class="delete" th:href="@{/Category/{id}(id=${c.id})}">Delete</a>

这里使用了占位符, 而不能直接

<a th:href="@{/Category/${c.id}}">Delete</a>

因为这样thymeleaf会误以为${}这些字符也是url的一部分;

然后这里我实现分页也是用了类似的标准url的写法:

<a class="page-link" th:href="@{/Category(start=${page.number}-1)}">Previous</a>

这里要注意, 实现DELETE方法, 要用的METHOD是DELETE, 而不是直接POST;
这里使用了一个隐藏的form来实现这个功能:

    <form id="deleteForm" method="post">
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值