一.文章微服务具体实现

1.编写启动类ArticleApplication

package cn.sm1234.article;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * 文章微服务
 */
@SpringBootApplication
public class ArticleApplication {
    public static void main(String[] args) {
        SpringApplication.run(ArticleApplication.class,args);
    }
}

2.编写pojo实体类Article,Result

package cn.sm1234.article.pojo;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 *文章实体
 */
@Entity
@Table(name = "tb_article")
public class Article implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) //自增长
    private Integer id;
    private String title;
    private String content;
    private String author;
    private Date addtime;

    public Integer getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Date getAddtime() {
        return addtime;
    }

    public void setAddtime(Date addtime) {
        this.addtime = addtime;
    }
}
package cn.sm1234.article.pojo;

import java.io.Serializable;

/**
 * 统一返回数据实体类
 */
public class Result implements Serializable{

    private Boolean flag; //是否成功
    private String message;//消息
    private Object data;//返回数据

    public Result() {
    }

    public Result(Boolean flag, String message) {
        this.flag = flag;
        this.message = message;
    }

    public Result(Boolean flag, String message, Object data) {
        this.flag = flag;
        this.message = message;
        this.data = data;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

3.编写DAO

package cn.sm1234.article.dao;
import cn.sm1234.article.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;
/**
 * 文章 dao
 */
public interface ArticleDao extends JpaRepository<Article,Integer>{
}

4.Service实现ArticleService,ArticleServiceImpl

package cn.sm1234.article.service;
import cn.sm1234.article.pojo.Article;
import java.util.List;
/**
 * 文章 service 接口
 */
public interface ArticleService {
    public List<Article> findAll();
    public Article findById(Integer id);
    public void add(Article article);
    public void update(Article article);
    public void deleteById(Integer id);
}
package cn.sm1234.article.service;

import cn.sm1234.article.dao.ArticleDao;
import cn.sm1234.article.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章service实现
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    public List<Article> findAll() {
        return articleDao.findAll();
    }

    @Override
    public Article findById(Integer id) {
        return articleDao.findById(id).get();
    }

    @Override
    public void add(Article article) {
        articleDao.save(article);
    }

    @Override
    public void update(Article article) {
        articleDao.save(article);
    }

    @Override
    public void deleteById(Integer id) {
        articleDao.deleteById(id);
    }
}

5.Controller实现

package cn.sm1234.article.controller;
import cn.sm1234.article.pojo.Article;
import cn.sm1234.article.pojo.Result;
import cn.sm1234.article.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * 文章 Controller
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    /**
     * 查询所有
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true," 查询成功",articleService.findAll());
    }
    /**
     * 查询一个
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable Integer id){
        return new Result(true," 查询成功",articleService.findById(id));
    }
/**
 * 添加
 */
@RequestMapping(method = RequestMethod.POST)
public Result add(@RequestBody Article article){
    articleService.add(article);
    return new Result(true," 添加成功");
}
    /**
     * 修改
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody Article article,@PathVariable Integer id){
        article.setId(id);
        articleService.update(article);
        return new Result(true," 修改成功");
    }
    /**
     * 删除
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable Integer id){
        articleService.deleteById(id);
        return new Result(true," 删除成功");
    }
}

6.整体项目结构

ZJ1TFF4D1QF`L5EP23KQ6.png

7.启动并使用apipost工具对接口进行测试

4CDAUYAKGTBK36OJ`~4.png