Now that the database is ready, you can polish the back-end code.

Now, to explore how the front and back end interact, the back end must return data. In this case, the first step is to return user information. Then you can determine whether the user name and password from the front end exist, if so, you can log in (PS: here is only for learning purposes, not to say that the real login is implemented in this way).

First, familiarize yourself with the layering of back-end code.

A, pojo

Entity layer, used to write entity class, such as User, it has a lot of attributes, such as ID, USERNAME and so on, and the previous design of the database table field corresponding. Note that the @data annotation eliminates the need to write set(), get(), and toString () methods, which saves a lot of effort and makes the code simpler. Remember to add dependencies and install the Lombok plugin in IDEA.

package com.mock.platform.pojo; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import javax.persistence.*; import java.util.Date; @Entity @Table(name = "user") @JsonIgnoreProperties({"handler", "HibernateLazyInitializer "}) @data public class User {@id // Declare a field" Id "as the primary key @generatedValue (strategy =) of the database table Generationtype.auto) // Annotate the primary key generation strategy, using the strategy attribute to specify @column (name = "ID ") // The corresponding Column name in the database table private int ID; // User id private String username; // User name private String password; // Password private Date createTime; Public int getId() {// return id; // } // public void setId(int id) { // this.id = id; // } // public String getUsername() { // return username; // } // public void setUsername(String username) { // this.username = username;Copy the code

Second, the dao

The DAO layer, full name for Data Access Object, is responsible for dealing with databases.

The UserDAO class inherits from JpaRepository and provides all the usual CRUD and paging functions. JpaRepository

. The parameters are the entity class and the type of the entity class ID.
,>

package com.mock.platform.dao; import com.mock.platform.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; /** * The UserDAO class inherits JpaRepository and provides various common CRUD and paging functions. * JpaRepository<User, Integer>. The parameters are the entity class and the type of the entity class ID. */ public interface UserDAO extends JpaRepository<User, Integer> { }Copy the code

Third, the service

The business logic layer, where the main business logic is implemented, so the basic business requirements change this layer.

Let’s say I implement a method to query user information.

package com.mock.platform.service; import com.mock.platform.dao.UserDAO; import com.mock.platform.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import java.util.List; Public class UserDAO {@autowired // Automatic assembly UserDAO; Public List<User> userList() {// Sort = sort.by (sort.direction.desc, "id"); // Query by categoryDAO return userdao.findall (sort); }}Copy the code

Fourth, the controller

This layer is usually used to control the business logic, but the actual business logic is not implemented here, but by calling methods in the Service layer.

For example, I received the request from the front end and returned the user information.

package com.mock.platform.controller; import com.mock.platform.pojo.User; import com.mock.platform.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; RestController // mark this is the controller, Public class UserController {@autoWired // CategoryService UserService UserService; @getMapping ("/users") // When accessing users, all User objects are retrieved and returned. RestController automatically converts to JSON for the browser public List<User> userList() throws Exception {return userService.userList(); }}Copy the code

Fifth, the properties

Now that the logic is basically done, you need to configure the data source in properties so that the entire service can be used.

Spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / my_platform? characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 Spring.datasource. Driver-class-name = com.mysql.cj.jdbc.driver spring.jpa.hibernate. DDL -auto = none # Server.servlet. context-path=/my_platform # JPA will automatically underline camelback named attributes when converting them to field names. The effect of this configuration is to remove the underscore #. For example, if the attribute name is createDate, JPA defaults to the field name create_Date. With this configuration, It will be converted to the createDate field of the same name Spring. Jpa. Hibernate. Naming. Physical - strategy = org. Hibernate. Boot. Model. The naming. PhysicalNamingStrategyStandardImpl # show SQL statements executed by Hibernate. This should be turned off after the launch, because a large amount of console output can seriously affect system performance. Now debug with spring.jpa.show-sql=trueCopy the code

For convenience, I first inserted three pieces of user information into the database:

Six, validation,

When it’s time to validate, start the back-end service first.

Next, I use Postman to request the interface exposed in the Controller to see if it returns the correct data.



Data is returned correctly, in reverse order of ID.