Scarecrow Project--Preliminary Preparation for the Project

table of Contents

Scarecrow project:

Demonstration of the Scarecrow project process:

The functional realization components and working principle of the Scarecrow project:

Create database tables and table field names:

1. Preliminary preparation of the project-learn to create a Maven aggregation project

1.1. Create an aggregate project

1.2. Through the configuration of the parent project, each sub-module project can use the same dependency

1.3. Configure optional dependencies for child projects in the parent project

2. Preliminary preparation of the project-learn to create a SpringBoot aggregation project

2.1. Create parent project

2.2. Create subproject

3. Use SLF4j to print logs


Scarecrow project:

Demonstration of the Scarecrow project process:

The functional realization components and working principle of the Scarecrow project:

Create database tables and table field names:

-- MySQL dump 10.13  Distrib 5.7.15, for osx10.11 (x86_64)
--
-- Host: localhost    Database: straw
-- ------------------------------------------------------
-- Server version	5.7.15

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `user`
--

DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) UNIQUE NOT NULL COMMENT '用户名',
  `nickname` varchar(20) NULL COMMENT '昵称',
  `password` char(68) NOT NULL COMMENT '密码',
  `gender` int(1) COMMENT '性别',
  `day_of_birth` date COMMENT '生日',
  `phone` varchar(20) COMMENT '电话号码',
  `class_id` int(11) COMMENT '所属班级id',
  `created_time` datetime COMMENT '注册时间',
  `enabled` int(1) COMMENT '账号是否可用,0-否,1-是',
  `locked` int(1) COMMENT '账号是否锁定,0-否,1-是',
  `type` int(1) COMMENT '0-学生,1-老师',
  `self_introduction` varchar(255) COMMENT '自我介绍',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-07-01 16:30:32

 

1. Preliminary preparation of the project-learn to create a Maven aggregation project

1.1. Create an aggregate project

First create a normal Maven project:

Then, fill in the project name and organization ID:

When the project is successfully created, pom.xml will be opened by default , and the configuration will be added to the file:

<packaging>pom</packaging>

E.g: 

After completion, the project is regarded as the parent project and can not be processed for the time being. Next, click the right mouse button on the name of the parent project, and then create the child project. The created menu command is New> Module:

When creating a Module project, still select the Maven project. In the detailed configuration interface, you need:

  • Determine the Parent project;

  • Custom name, it is recommended to use "parent project name-" as a prefix;

  • Fill in the GroupId;

  • Check Location.

E.g:

When the submodule is created, you can see the submodule project in the project management panel on the left:

And, the node has been automatically added in the pom.xml of the parent project <modules>, and the submodule project has been configured:

The submodule project also has its own independent pom.xml . As you can see, the parent project has been configured:

Then, use the same steps to create the second submodule project shop-order:

 

1.2. Through the configuration of the parent project, each sub-module project can use the same dependency

Add dependencies and related configurations in the pom.xml of the parent project :

Then each sub-module project does not need to add junitdependencies, but can be used directly junitfor unit testing:

 

1.3. Configure optional dependencies for child projects in the parent project

If some dependencies are only part of the sub-module project that needs to be used, and some other sub-module projects do not need to be used, in order to facilitate the overall unified management, it should be configured through the node in the pom.xml of the parent project <dependencyManagement>, and the dependency configured by the node should be used , Sub-items cannot be used directly:

If you try to use the dependent class directly in the subproject, an error will occur:

Assume that the shop-order sub-module project needs to use MySQL dependencies, and the dependency needs to be added in the pom.xml of the sub-module project . However, when adding dependencies, you do not need to specify the version:

Then, update Maven (click the refresh button) and test again:

At this point, the sub-project can use the MySQL dependency!

Using this approach, there is no need to require sub-projects to manage the dependent versions, all version management can be concentrated in the parent project! Of course, if the sub-project must use other versions, you can also add <version>nodes to specify.

In addition, in the above steps, the shop-user submodule project does not add a dependency on MySQL, so when the same test is executed, an error will definitely occur!

2. Preliminary preparation of the project-learn to create a SpringBoot aggregation project

2.1. Create parent project

Create the parent project first, select Spring Initializrit on the left side of the first panel of the project, confirm the available URLs on the right side, and click OK:

In the second interface, fill in Group and Artifact , select Type as Maven POM, other options do not require modification, just confirm it directly:

In the third interface, you can choose to depend on. Because this parent project is not sure which dependencies need to be used for the time being, you can uncheck it first, or add it yourself later.

In the fourth interface, it is mainly to determine whether the location where the project file is stored is correct, and after everything is correct, the creation process is completed!

2.2. Create subproject

Right-click the parent project, select New> Module, still select Spring Initializrin the first interface of creating a sub-module project, and confirm the information of the sub-module project in the second interface. Note that when there is a decrease in the Artifact value of the sub-module project The default Package value is to directly connect the names on both sides of the minus sign. It is recommended to add a decimal point to separate it:

In the third interface, check the dependencies according to your needs. Assuming you want this subproject to be deployed to Tomcat, you can check the Spring Webdependencies (each dependency can also be added after the creation is successful):

In the fourth interface, if there is no problem, the creation is completed directly.

After the creation is complete, you can see in the pom.xml file of the submodule project that its parent project is still the SpringBoot project instead of the blog project created before :

You need to change the parent project here to the blog project (open the pom.xml file of the parent project and find the relevant information and copy and paste it into the submodule project):

 

At the same time, <modules>nodes are not automatically added in the parent project to configure the submodule project, and need to be added manually (in addition, <dependencies>nodes and <build>nodes are not needed in the parent project at this time , and you can delete them by yourself):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.tedu</groupId>
    <artifactId>blog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>blog</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>blog-user</module>
        <module>blog-comment</module>
    </modules>

</project>

After all is completed, use the same operation method to create another sub-module project blog-comment .

It should be noted that since the two sub-module projects can be run directly, when Spring Webdependencies are added , Tomcat can be started directly, and the 8080ports are occupied by default . Therefore, in order to avoid conflicts, these sub-module projects should be explicitly added The port is changed to a different one!

3. Use SLF4j to print logs

Using SLF4j can customize the output log, similar to using the System.out.println()output of some content, the advantage of using SLF4j is that the execution efficiency is high, and the log level can be customized, so that the log can be filtered (so that the lower level log will not be displayed ).

When using SLF4j, it is recommended lombokto use it together. lombokThe dependency should be added first . Since this dependency may be used in several submodule projects, you can directly add this dependency to the parent project:

Next, you can test and use SLF4j in any sub-module project:

In SLF4j, the log levels from low to high are:

  • TRACE: tracking information;

  • DEBUG: debugging;

  • INFO: general information;

  • WARN: Warning, usually caused by poor code quality, such as using an API declared as outdated, or the stream object is not closed, etc.;

  • ERROR: Error, generally will not cause the project to crash;

  • FATAL: fatal error, will cause the project to crash;

  • OFF: Turn off the output of all logs.

The output level of the log can be configured in the application.properties of the project :

logging.level.cn.tedu.blog.user=trace

The above configuration information indicates the configuration log level ( logging.level), and the cn.tedu.blog.userpackage is set , which means that after the class output log in this package and all its descendants in the current project, they are tracedisplayed in accordance with the level, traceand the logs at and higher levels will be display!

In the above configuration information, a class name can also be added to the right of the package name, which means that only the log display level of a certain class is configured.

When set to the tracelevel, the log of each level can be output and displayed. Generally, in the development stage, you can set it to the tracelevel directly , and during development, you should distinguish logthe methods used, and the more sensitive information should debugbe output using methods. , When the project is completed and needs to be deployed to the server, it is recommended to set to warn(Warning) or higher, then the lower-level logs will not be displayed during the operation of the official server!

When calling logeach method, the output content can be used {}as a placeholder (there is no content between the brackets), and then use variable parameters to add the value corresponding to the placeholder, for example:

Guess you like

Origin blog.csdn.net/c202003/article/details/107317420