Introduction to Java

Introductory test

Identify requirements

Project Name: Journey

  1. Addition, deletion, modification and query of database
  2. The primary and secondary menus of the page are displayed.
  3. Image upload and deletion
  4. Side catalogue display
  5. Page Pagination

Determine the tools and frameworks used

springboot framework, idea, mysql, mybatis, maven

Design table structure

Hero information table:

Name, nickname, height, weight, age, date of birth, occupation, hobby, home address, remarks

Family member information form:

Name, age, date of birth, current relationship, future relationship, remarks

Line:

Year, age, position, major deeds, remarks

Prototype design

Create a springboot project

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Create database tables

springboot connects to the database

server.port=8080

###Database connection information
#Database driven
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#Connection address
spring.datasource.url=jdbc:mysql://localhost:3306/firstspringboot?serverTimezone=UTC&characterEncoding=utf-8&useSSL=flase
#Database account
spring.datasource.username=root
#Database password
spring.datasource.password=997573jqws
#Configure jpa
#Help us automatically generate table structure
#spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

Create four layers

  1. controller: the control layer controls the business logic

    • For the control of specific business module processes, the controller layer mainly calls the interface in the Service layer to control specific business processes, and the control configuration should also be carried out in the configuration file.
  2. dao: persistence layer, which mainly interacts with the database

    • The dao layer first creates a dao interface, and then defines the implementation class of the interface in the configuration file; Then you can call the interface of dao to process data business in the module without paying attention to which class is the specific implementation of the interface, and the data source and the database in the dao layer are configured in the actual configuration file.
  3. model: entity layer, the class of database in the project

    • It is mainly used to define the properties of database objects, provide get/set methods, and construction methods with and without parameters.

    • @Data: Annotation on class; It provides getting and setting methods for all attributes of the class, as well as equals, canEqual, hashCode and toString methods

  4. service: business layer control business

    • The logical application design of the business module, like the DAO layer, first designs the interface, then creates the class to be implemented, and then configures the association of its implementation in the configuration file. Next, you can call the interface at the service layer to process the business logic application.

    About the four layers of spring boot

    I have seen a very vivid metaphor:

    The Controller layer is like a waiter. He summarizes the dishes (data, type of request, etc.) ordered by the guest (front end) and gives them to the chef (Service layer). The chef tells the dip board Chef (Dao 1), soup room (Dao 2), side dish Chef (Dao 3) (collectively referred to as Dao layer) what semi-finished products I need, The Deputy Chefs (Dao level) are responsible for completing the tasks assigned by the chef (Service).

About automatically generating database tables

@Both the Entity annotation and the @ Table annotation are annotations defined in the Java Persistence API.

@Entity indicates that this class is an entity class, and the default orm rule is used, that is, the class name is indicated in the database table, and the class field name is the field name in the table@ The entity annotation indicates that this is an entity Bean.

import lombok.Data;
 
import javax.persistence.*;
 
@Data
@Entity(name = "t_book")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "book_name")
    private String name;
    @Column(name = "book_author")
    private String author;
    private Float price;
    @Transient
    private String description;
}

@The Entity annotation indicates that the class is to be given to an Entity class. A table will be automatically generated according to the class when the class is started. The name of the table is the value of name in the @ Entity annotation. If name is not configured, it is the class name by default
All entity classes must have a primary key, @ Id annotation indicates that the attribute is a primary key, @ GeneratedValue annotation indicates that the annotation is automatically generated, and strategy indicates the generation strategy of the primary key
By default, the name of the field in the generated table is the name of the attribute in the entity class. The generated field attribute can be customized through the @ Column annotation. Name indicates the name of the field in the data table corresponding to the attribute, and nullable indicates that the field is not empty
@The Transient annotation indicates that this attribute is ignored when generating tables in the database, that is, the corresponding fields are not generated

Keywords: Java Spring Boot JavaSE intellij-idea

Added by majiclab on Tue, 11 Jan 2022 11:07:18 +0200