Build springboot based backend project 1 from 0

idea will build the springboot project in the next step
First, create a new project
Select name shoulder
Language: java
Type: Mavan


First, import some common dependencies for testing

After the project is built, the initial directory structure is as follows
. idea is the configuration of idea, regardless
We mainly write various codes in src and configure POM XML file and application properties

Since the front and back ends are separated, static and templates can be deleted

Next, the configuration file is generally used. There are two kinds of suffix configuration files in the springboot project. Now, the YML format is commonly used. In fact, both of them can be written in different ways. I use the YML format here and delete the properties (if there is no YML format by default, the application.yml file needs to be created)

I use yml configuration here. Through the configuration file, you can easily set various configuration information, such as port number, database connection information, etc

Creating a database using navicat

Create a simple user table through visual operation, or create it with sql statement. This is just for a simple curd exercise

CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'user name',
  `password` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'password',
  `nick_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'nickname',
  `age` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'Age',
  `sex` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'Gender',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User information table';

Then run the project,

After the project runs, you can enter it in the browser http://localhost:9090/
Seeing such a page can prove that the project is running

After running, find some common dependencies, such as mybatis plus, to more easily complete the function of adding, deleting, checking and modifying

Import dependency. Since we are a springboot project, we import the springboot version

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

Next, write some configuration information, create a new config class and a new result Java file, encapsulate the returned results

package com.cz.examback.config;

public class Result<T> {
    private Integer code;
    private String msg;
    private T data;
    
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

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

    public Result() {

    }

    public Result(T data) {
        this.data = data;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result success() {
        Result result = new Result<>();
        result.setCode(0);
        result.setMsg("success");
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setCode(0);
        result.setMsg("success");
        return result;
    }
    
    @Override
    public String toString() {
        return "Result{" +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

}

There is also the paging configuration of mbp. Create a new mybatisplusconfig Java file, and create a mapper directory to put the mapper file for scanning at that time

Copy the official code here

The complete code is as follows

package com.cz.examback.config;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.cz.examback.mapper")
public class MybatisPlusConfig {
    /**
     * mybatis-plus Paging plug-in
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

Keywords: Mybatis Spring AOP

Added by mark_kccs on Sun, 06 Mar 2022 17:18:03 +0200