Grain Academy - day2

main mission
image.png

Front-end and back-end separation development concept

image.png
The development interface refers not to the development of the interface, but the process of developing the controller, mapper, and service

Database Design Specifications

The following stipulations are only for this module. For more comprehensive documents, please refer to "Alibaba Java Development Manual": 5. MySQL database
1. The library name and the application name should be as consistent as possible.

2. Table names and field names must use lowercase letters or numbers, and numbers at the beginning are prohibited.

3. Table names do not use plural nouns

4. The name of the table is best to add "the role of the business name_table". For example, edu_teacher

5. The table must have three fields: id, gmt_create, gmt_modified

Description:
The id must be the primary key, the type is bigint unsigned, it is incremented for a single table, and the step size is 1.
(If using cluster deployment with sub-database and sub-table, the id type is verchar, which is not self-incrementing, and distributed id generator is used in the business.)
The types of gmt_create and gmt_modified are both datetime types. The former means active creation in the present tense, and the latter is past participle. Indicates passive updates.

6. The number of rows in a single table exceeds 5 million rows or the capacity of a single table exceeds 2GB, only sub-database and table-sharding are recommended. Note: If it is expected that the data volume in three years will not reach this level at all, please do not split the database and the table when creating the table.

7. Fields that express the concept of yes or no must be named in the way of is_xxx, and the data type is unsigned tinyint (1 means yes, 0 means no).
Description: If any field is non-negative, it must be unsigned.
Note: Do not add the is prefix to any boolean variable in the POJO class. The database represents the value of yes or no, using the tinyint type, and insisting on the naming method of is_xxx in order to clarify its value meaning and value range.
Positive example: the field name is_deleted expressing tombstone, 1 means deleted, 0 means not deleted.

8. The decimal type is decimal, and the use of float and double is prohibited. Note: When float and double are stored, there is a problem of precision loss, and it is very likely that incorrect results will be obtained when comparing values. If the range of stored data exceeds the range of decimal, it is recommended to split the data into integers and decimals and store them separately.

9. If the length of the stored strings is almost equal, use the char fixed-length string type.

10. varchar is a variable-length string, no storage space is allocated in advance, and the length should not exceed 5000. If the storage length is greater than this value, define the field type as text, separate a table, and use the primary key to correspond to avoid affecting other field indexes. efficiency.

11. The unique index name is uk_field name; the common index name is idx_field name.
Description: uk_ is unique key; idx_ is short for index

12. Foreign keys and cascades must not be used. All foreign key concepts must be resolved at the application layer. Foreign keys and cascading updates are suitable for single-machine low concurrency, but not suitable for distributed and high-concurrency clusters; cascading updates are strong blocking, and there is a risk of database update storms; foreign keys affect the insertion speed of the database.

Create project structure

  • Create parent project
    • pom type manages version dependencies and puts public dependencies
    • Create submodule in parent project
    • In submodules you can also create submodules

image.png

Project structure

image.png

Module description

guli-parent: Online teaching root directory (parent project), manages four sub-modules:
** canal-client: canal database table synchronization module (statistical synchronization data) **
** common: common module parent node **
common-util: Tool module, all modules can depend on it
service-base: the base package of the service service, including the public configuration class of the service service, all service modules depend on it
spring-security: the authentication and authorization module, the service service that requires authentication and authorization Depends on it
** infrastructure: basic service module parent **
api-gateway: api gateway service
** service: api interface service parent **
service-acl: user rights management api interface service (user management, role management and permissions management, etc.)
service-cms: cms api interface service
service-edu: teaching-related api interface service
service-msm: SMS api interface service
service-order: order-related api interface service
service-oss: Alibaba Cloud oss ​​api interface service
service-statistics : Statistical report api interface service
service-ucenter: Member api interface service
service-vod: video on demand api interface service

Develop lecturer management module (backend)

Ready to work

  1. Create database, create instructor data table

image.png

  1. Build the project structure and configure the pom.xml file

image.png

  1. Create an application configuration file and use the CodeGenerator code generator to automatically generate code

image.png
image.png
Always set to absolute path
image.png
The primary key strategy is written as the type of your primary key, where the primary key type is char type, use ID_WORKER_STR, if it is long type, use ID_WORKER
image.png
date type, the date type in the database is datetime, and the date type in the entity class is date
image.png
The result generated is as follows
image.png

  1. finall

image.png
Date format issue
image.png
added in application.properties

#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

image.png

  1. Tombstone

image.png

  1. Configure swagger
    1. Create a common module common, integrate swagger, make all modules accessible,
      1. The pom file of the module that needs to be imported introduces the public module
      2. Add an annotation to the startup class and configure the package scan rule @ComponentScan(basePackages = { "com.xuan" })
    2. Write a configuration class
    3. 访问 http://localhost:8001/swagger-ui.html
  2. Unified return data format
  3. In the project, we will encapsulate the corresponding data into json. Generally, we will unify the data format of all interfaces, so that the front-end operation of the data is more consistent and easy. The basic format our system requires to return is as follows

list:
image.png
pagination:
image.png
no data returned:
image.png
failed:
image.png
so we define uniform result
image.png

  1. Create an interface to define the return code, create a result class, add dependencies to the service module, and finally modify the return result in the controller

  1. Paging query
    1. Configure the paging plugin in MyBatisPlusConfig

image.png

  1. Paging Controller method

image.png

  1. Test in Swagger
  2. Paging condition query
    1. Configure the mp paging plugin
    2. The method of writing the lecturer's paging query interface
    3. Encapsulate the conditional value into the vo class
    4. RequestBody: You must use post to submit, use json to transmit data, and encapsulate the json data into the corresponding object
    5. ResponseBody: The returned data is json dataimage.png
  3. Adding and Modifying Instructors
  4. Add the package handler to the service-base module and create an autofill classimage.png
  5. Add autofill annotations to entity classesimage.png
  6. write controllerimage.png
  7. image.png
  8. Unified exception handling
  9. Annotation ControllerAdvice is used with ExceptionHandler
  10. image.png
  11. The common module is introduced into the base module

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/122414124