Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write springboot interface

Shulou Source: shulou.com Published: 2022-06-01 08:59:37 10月27日 Update

This article will explain in detail how to write the springboot interface. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

First of all, you need to understand the direction of data flow:

The trigger of the data is caused by the front-end request back-end. If we follow the traditional mvc specification, we need four levels of pojo mapper service controller. Pojo corresponds directly to the fields in the database.

Build a springboot project online

Https://start.spring.io/

Four dependencies that need to be added

Click OK to delete the useless files and save the last two:

Add the version of jdk here:

Start writing interface implementations

Pon.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot Mybatis-spring-boot-starter 2.2.1 mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test Org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok

Application.yml

Spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: 123456server: port: 8001

Persistence layer:

Package com.example.demo.entity;import lombok.Data;@Datapublic class User {private Integer id; private String name; private String address; private Integer age; private String sex; private String phone;}

Here we introduce lombok without writing get and set methods to simplify the code

Org.projectlombok lombok 1.16.10 provided

Mapper layer

Package com.example.demo.mapper;import com.example.demo.entity.User;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestBody;import java.util.List;public interface UserMapper {@ Select ("select * from user") List findAll () @ Update ("INSERT INTO `user` (`name`, `address`, `age`, `sex`, `phone`) VALUES (# {name}, # {address}, # {age}, # {sex}, # {phone});) @ Transactional void save (User user); @ Update (" update user set name=# {name}, address=# {address}, age=# {age}, sex=# {sex}, phone=# {phone} where id = # {id} ") @ Transactional void updateById (User user) @ Delete ("delete from user where id = # {id}") @ Transactional void deleteById (Long id); @ Select ("select * from user where id = # {id}") User findById (Long id); @ Select ("select * from user limit # {offset}, # {pageSize}") List findByPage (Integer offset, Integer pageSize); @ Select ("select count (id) from user") Integer countUser ();}

Controller

Package com.example.demo.controller;import com.example.demo.entity.User;import com.example.demo.mapper.UserMapper;import com.example.demo.vo.Page;import org.apache.ibatis.annotations.Delete;import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;import java.util.List;@RestController@RequestMapping ("/ user") public class UserController {@ Resource UserMapper userMapper; @ GetMapping public List getUser () {return userMapper.findAll () } @ PostMapping public String addUser (@ RequestBody User user) {/ / convert the data from the front end into objects of the user entity class and insert it into the database userMapper.save (user); return "success";} @ PutMapping public String updateUser (@ RequestBody User user) {userMapper.updateById (user); return "success" } @ DeleteMapping ("/ {id}") / / A pair of corresponding relationships public String deleteUser (@ PathVariable ("id") Long id) {/ / Note is a sequential json return with id userMapper.deleteById (id); return "success" } @ GetMapping ("/ {id}") / / wraps the returned result as a user object public User findById (@ PathVariable ("id") Long id) {/ / Note is a sequential json return with id return userMapper.findById (id) } @ GetMapping ("/ page") public Page findByPage (@ RequestParam (defaultValue = "1") Integer pageNum, @ RequestParam (defaultValue = "10") Integer pageSize) {Integer offset = (pageNum-1) * pageSize; List userData = userMapper.findByPage (offset, pageSize); Page page = new Page (); page.setData (userData); Integer total = userMapper.countUser () Page.setTotal (total); page.setPageNum (pageNum); page.setPageSize (pageSize); return page;}}

Note: in the process of implementation, you need to add the annotation of scanning mapper to the startup class.

In the past, it was the implementation of the addition, deletion, modification, query and paging query of the interface.

Implementation process:

Write out insert statements quickly

Insert to implement the analog front end to send json data to the back end

Update the test:

Delete the implementation:

To delete is to pay attention to the one-to-one correspondence of id.

Paging query:

Paging query parameter 1 which page parameter 2 how much data is there on a page?

This is the end of the article on "how to write springboot Interface". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Data interface front-end annotation article query parameter object database more process good practical one-to-one correspondence two code traditional content field entity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Linux Docker Xiaomi