SpringCloud 项目学习(四)实现增删查改

此模块依然基于以上博客书写。并在 blog_base 模块实现增删查改功能。

一、 配置启动类

在 启动类中 实例化雪花算法,代码实现如下:

@SpringBootApplication
@CrossOrigin   // 跨域
public class BlogBaseApplication {
    public static void main(String[] args) {
        SpringApplication.run(BlogBaseApplication.class, args);
    }

    @Bean
    public IdWorker idWorker(){
        return new IdWorker(1,1);
    }
}

二、 定义 配置文件 application.yml .

配置内容如下:

server:
  port: 9001

## 应用名称
spring:
  application:
  ##  中间只能是横杠,不能使用下划线
    name: blog-base
## 数据库配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://47.94.10.48:3306/tensquare_base?characterEncoding=utf-8
    username: root
    password: 123456
## jpa 配置
  jpa:
    database: mysql
    show-sql: true

三、 实现增删查改

  1. 定义实体类 Label ,实现序列化接口,内容如下:
@Entity
@Data
@Table(name = "tb_label")
public class Label implements Serializable {

    /** id **/
    @Id
    private String id;
    /** 标签名称 **/
    private String labelname;
    /** 状态 **/
    private String state;
    /** 使用次数 **/
    private String count;
    /** 关注数量 **/
    private String fans;
    /** 是否推荐 **/
    private String recommend;
}

  1. 定义Controller 类,内容如下:
package com.dongl.controller;

import com.dongl.entity.Label;
import com.dongl.entity.Result;
import com.dongl.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 标签控制类
 * @author: YaoGuangX
 * @date: 2020/3/14 1:22
 * @Version: 1.0
 */

@CrossOrigin
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @RequestMapping(method = RequestMethod.GET)
    public Result getAll(){
        List<Label> labels = labelService.getLabelAll();
        return Result.success("查询成功",labels);
    }

    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String labelId){
        Label label = labelService.findById(labelId);
        return Result.success("查询成功",label);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Label label){
        labelService.save(label);
        return Result.success("添加成功");
    }

    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result update(@PathVariable("labelId") String labelId, @RequestBody Label label){
        Label labelInfo = labelService.findById(labelId);
        if (null == labelInfo){
            return Result.error("标签不存在,修改失败!");
        }
        label.setId(labelId);
        labelService.update(label);
        return Result.success("修改成功");
    }
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("labelId") String labelId){
        Label labelInfo = labelService.findById(labelId);
        if (null == labelInfo){
            return Result.error("标签不存在,删除失败!");
        }
        labelService.deleteById(labelId);
        return Result.success("删除成功");
    }

}

  1. 定义service 类,内容如下:
@Service
@Transactional
public class LabelService {

    @Autowired
    private ILabelDao labelDao;
   
    @Autowired
    private IdWorker idWorker;

    public List<Label> getLabelAll(){
        return labelDao.findAll();
    }

    public Label findById(String id){
        return labelDao.findById(id).get();
    }

    public void save(Label label){
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }

    public void update(Label label){
        labelDao.save(label);
    }

    public void deleteById(String id){
        labelDao.deleteById(id);
    }
}
  1. 定义Dao 接口,内容如下:
    在Dao 接口中继承 JpaRepository<Label,String> 接口主要使用其中的常用操作数据库的方法。
    接口中的参数 Label:表示实体 ,String:Label 对象id的数据类型。
    接口:JpaSpecificationExecutor<Label> : 包含复杂的条件查询,排序,分页查询。
/**
 * JpaRepository<Label,String> 包含jpa 中的简单方法,Label:实体 ,String:Label 对象id的类型。
 * JpaSpecificationExecutor: 包含复杂的条件查询,排序,分页查询。
 * @Description: 标签接口类
 * @author: YaoGuangXun
 * @date: 2020/3/14 15:12
 * @Version: 1.0
 */
public interface ILabelDao extends JpaRepository<Label,String> , JpaSpecificationExecutor<Label> {

}

四、 启动项目并访问

  1. 测试查询功能
    在浏览器中输入:http://localhost:9001/label

    image.png

  2. 测试新增功能
    使用idea 的 REST Client 功能,点击 Tools --> Http Client --> Test RESTful Web Service 。


    image.png

    填写要保存的数据:
    选择请求类型:POST
    Host/port :地址端口号
    Path:请求路径
    Request :请求数据类型
    Text :JSON 或对象格式


    image.png

    点击请求保存数据
    image.png

如果对你有所帮助,请点个赞或给个好评!谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容