The previous article has introduced the basic use process of SpringBoot, this article mainly introduces the implementation of a simple interface with the SpringBoot project configured in that article.

The final project catalog looks like this:

1. Project directory structure

This is a simple directory structure for our reference, which is divided into four main categories:

  • repository

Inherited from JpaRepository, which mainly encapsulates SQL actions to operate the database. Methods can be used to operate the database, and SQL statements can be customized to operate the database. Repository is the emissary of Jpa and the direct operator of the database.

  • controller

Request interface class, request/ Response class, etc., need related annotations before the class;

  • service

Process model instances and synchronize them to the database through Repository;

  • model

Data instance, an Entity that corresponds to a table in the database.

And finally, one more.. The Application class is generated by default and does not need to be processed.

2. Implementation of each class

Achieve GET, POST type of two interfaces: /todo/list /todo/listAdd

  • TodoController
package com.dcxz.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class TodoController {

    @Autowired
    TodoService todoService;
    
    @RequestMapping(value = "/todo/list", method = RequestMethod.GET)
    public TodoResponse getTodoList(@RequestParam long id) {

        Todo todo = todoService.getTodoData(id);

        TodoResponse response = new TodoResponse();
        response.setName(todo.getName());
        //response.setMember(100);
        //response.setCount(20);
        return response;
    }


    @RequestMapping(value = "/todo/listAdd", method = RequestMethod.POST)
    public String addTodoList(@RequestBody AddTodoRequest request) {

        String name = request.getName();
        if (name == null) { throw new RuntimeException("Parameter error"); }

        todoService.addTodoData(name);

        return "success!"; }}Copy the code
  • Request body and response body
/// AddTodoRequest
package com.dcxz.demo;

public class AddTodoRequest {

    private String name;

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name; }}/// TodoResponse
package com.dcxz.demo;

import java.util.Set;

public class TodoResponse {

    private String name;
    private int count;
    private int member;

    public String getName(a) {
        return name;
    }

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

    public int getCount(a) {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getMember(a) {
        return member;
    }

    public void setMember(int member) {
        this.member = member; }}Copy the code
  • service
package com.dcxz.demo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class TodoService {

    @Autowired
    TodoRepository todoRepository;

    public void addTodoData(String name) {

        Todo todo = new Todo();
        todo.setName(name);
        todo.setCreateTime(new Date());

        todoRepository.save(todo);
    }


    public Todo getTodoData(long id) {

        Todo todo = todoRepository.findById(id).get();
        returntodo; }}Copy the code
  • Todo
package com.dcxz.demo;


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

@Entity
@Table(name = "s_todo")
public class Todo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    @Column(name = "create_time")
    private Date createTime;


    public long getId(a) { return id; }
    public void setId(long id) { this.id = id; }

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

    public Date getCreateTime(a) { return createTime; }
    public void setCreateTime(Date createTime) { this.createTime = createTime; }}Copy the code
  • repository
package com.dcxz.demo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface TodoRepository extends JpaRepository<Todo.Long> {}Copy the code

3. Invoke the interface

You can call the two interfaces written above through the browser, PostMan, to test whether the interface is normal. Ensure that the IP address, port, interface name, and parameter Settings are correct.