Preliminary understanding of SpringBoot

SpringBoot is a quick-start scaffolding for the Spring framework, and with SpringBoot we can save our brains and hands from the tedious configuration work of the Spring framework. Using SpringBoot, we can quickly build a Web project based on the Spring framework.

Create projects and introduce dependencies

<! All SpringBoot projects must be spring-boot-starter-parent projects, that is, all SpringBoot projects are children of the project. --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.04..RELEASE</version> </parent> <dependencies> <! -- Launcher for Web functionality, <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! -- Test function initiator, > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- Lombox plugin dependencies, SpringBoot built-in, --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <! --> <dependency> <groupId> MySQL </groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <! Mapper, a universal mapper based on MyBatis, MyBatis </groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.03.</version> </dependency> </dependencies> <build> <! <plugins> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>Copy the code

Application. Yml configuration file

SpringBoot is just a simplification of the project configuration file, not really no configuration, at least such as database connection information, we need to manually configure. We create an application.yml configuration file in the Resources directory.

spring:
  datasource:   SpringBoot Hikari is the built-in database connection pool
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myshop
    username: root
    password: admin
  jackson:		Date used to convert JSON data format
    default-property-inclusion: ALWAYS
    time-zone: GMT+8
    date-format: yyyy-MM-dd
mybatis:
  type-aliases-package: com.hrp.domain  # configure entity class packet scan for MyBatis
Copy the code

This is all of our configuration. SpringBoot simplifies the configuration process considerably compared to the CONFIGURATION in the SSM framework.

Write the SpringBoot boot class

package com.hrp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/** * SpringBoot boot class, SpringBoot application entry *@author hrp
 * @date 2020/2/7 22:14
 */
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) { SpringApplication.run(MyApplication.class); }}Copy the code

Writing entity classes

package com.hrp.domain;

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/ * * *@author hrp
 * @date2020/2/7 [* /
@Data
@Table(name = "user")
public class User {
    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;
    private String username;
    private String password;
    private String name;
    private Integer gender;
    private Date birthday;
    private String phone;
    private String picture;
    private String address1;
    private String address2;
    private String address3;
    private Integer power;
}
Copy the code

MySQL > insert into MySQL database

CREATE TABLE `user` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(1) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `picture` varchar(50) DEFAULT NULL,
  `address1` varchar(50) DEFAULT NULL,
  `address2` varchar(50) DEFAULT NULL,
  `address3` varchar(50) DEFAULT NULL,
  `power` int(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
Copy the code

Write a Common class

package com.hrp.common;

import tk.mybatis.mapper.additional.idlist.IdListMapper;
import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.common.Mapper;

/** * this is our own generic class, essentially a utility class, T for entity generics, D for entity primary key generics *@author hrp
 * 2020/2/22 15:34
 */
@RegisterMapper
public interface BaseMapper<T.D> extends Mapper<T>, IdListMapper<T.D>, InsertListMapper<T> {}Copy the code

Mapper (tk.mybatis.mapper.mon); Mapper (tk.mybatis.mapper.mon); Mapper (tk.mybatis.mapper.mon);

Write a data access layer

package com.hrp.mapper;

import com.hrp.common.BaseMapper;
import com.hrp.domain.User;
import org.apache.ibatis.annotations.Mapper;

/** ** *@MapperThe annotated package is ibatis * second: here BaseMapper is the one we wrote, not Tk.mybatis (general Mapper) *@author hrp
 * @date2020/2/7 22:16 * /
@Mapper
public interface UserMapper extends BaseMapper<User.Long> {}Copy the code

【 Attention 】 here need to pay attention to a few points, is the same guide package problem

  • @mapper annotated package is ibatis
  • BaseMapper here is the one we wrote ourselves, not Tk. mybatis(general Mapper)

Writing the business layer

Business layer interface

package com.hrp.service;

import com.hrp.domain.User;

import java.util.List;

/ * * *@author hrp
 * 2020/2/22 14:44
 */
public interface UserService {

    /** * Find all user information *@return* /
    List<User> findAll(a);
}
Copy the code

Business layer implementation

package com.hrp.service.impl;

import com.hrp.domain.User;
import com.hrp.mapper.UserMapper;
import com.hrp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/ * * *@author hrp
 * 2020/2/22 14:47
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll(a) {
        returnuserMapper.selectAll(); }}Copy the code

[Note] Yes, we did not write any SQL statements, just write some annotations on the entity class, the persistence layer implements a Mapper interface, we can directly use some simple add, delete, change, check methods, this is the power of the universal Mapper.

Write the Web tier

package com.hrp.web;

import com.hrp.domain.User;
import com.hrp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/ * * *@author hrp
 * 2020/2/22 14:48
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public ResponseEntity<List<User>> findAll(){
        returnResponseEntity.ok(userService.findAll()); }}Copy the code

The last

We run the main method of the boot class to launch the SpringBoot project and access the /user/list interface.