Getting started with springboot: 02 – Implementing MVC


In the previous article, we have created entity classes and mapping interfaces. The next thing we need to do is to implement the service layer for processing business logic and the controller layer for controlling process.

Create business logic layer

Create a service directory in the project and create a new directory File:

package com.foxescap.wxbox.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foxescap.wxbox.mapper.UserMapper;
import com.foxescap.wxbox.model.User;
import org.springframework.stereotype.Service;

 * @author imxfly
public class UserService extends ServiceImpl<UserMapper, User> {

     *Get a single user based on the user name
     *@ param user name
     *@ return user
    public User findByUsername(String username) {
        return lambdaQuery().eq(User::getUsername, username).one();

The above is a logic to obtain a single user according to the user name. There are several points to explain:
The class is automatically injected into the spring container by adding the official @ service annotation of spring

By inheriting the serviceimpl < usermapper, user > class provided by mybatis plus dependency, you can easily use its query, lambdaquery and other methods to perform chain operation, instead of the tedious XML configuration of mybatis. This is the biggest way to improve the development efficiency

Create control process layer

Create a controller directory in the project and create a new directory File:

package com.foxescap.wxbox.controller;

import com.foxescap.wxbox.model.User;
import com.foxescap.wxbox.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

 * @author imxfly
public class UserController {

    //private UserService userService;
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;

     *Get users by user name
     *@ param user name
     *@ return user
    public User findUserByUsername(@RequestParam(name = "username") String username) {
        return userService.findByUsername(username);

The @ restcontroller annotation provided by spring is automatically injected into the spring container, and the return value is placed in the response body instead of returning a page.

Many people are used to using @ Autowired annotation to automatically inject instances of the current class, but I’m used to using constructors. One way to keep business logic from spring boot code intrusion is to use constructor injection. Not only is the @ Autowired annotation optional in the constructor, but it can also easily instantiate bean without spring. For other advantages and disadvantages, please read the first item in the reference link below.

The creation of route is realized through @ getmapping annotation provided by spring.

The @ requestparam annotation provided by spring is used to bind request parameters.

Run the test

After running, if there is no port configured, the default is 8080. In order to see the data, we insert a message in the database:

INSERT INTO `wxbox`.`user`(`id`, `username`, `secret_key`, `expired_at`, `created_at`) VALUES (1, 'steve', 'xxx', '2020-08-28 00:05:40', '2020-07-07 00:05:47');

Then look at the following through curl command:

$ curl http://localhost:8080/user\?username\=steve

Reference link

• Field injection is not recommended – Spring IOC

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]