1 Maven
Maven is the most popular Java project building tool
Why do I need to use Maven?
Maven can help us manage jar packages and build projects automatically in Java Web development.
1.1 Install Maven
-
Official download: Maven – Welcome to Apache Maven
-
Installation: Decompress the package.
-
Maven Core Profile Set.xml
1.2 Configuring environment variables
MAVEN_HOME Configuration Maven Directory
Configuring%MAVEN_in the Path of the system HOME%\bin
Open the DOS window and enter the mvn-v command to view the version. The following message indicates that the Maven installation was successful
1.3 Configuration Mirror
<mirrors> <!-- Mirror: easy to download and use, Maven Is a foreign website, Download jar Package is slow, configure mirror to speed up download,Suggest Ali Yun Mirror to be used in China --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> </mirrors>
1.4 Configure local warehouse
Local repository for jar packages downloaded from server
<localRepository>D:/JavaTools/Maven/apache-maven-3.6.1/repository</localRepository>
1.5 Maven Integrated IDEA
There is a native Maven in the idea. We want idea to use its own installed maven.
-
Select File-->Settings-->Build, Execution, Deployment-->Maven
2. Select File-->Settings-->Build, Execution, Deployment-->Maven-->Runner
-DarchetypeCatalog=internal
3. Select File---> Other Settings---> Settings for New Projects
Other project Maven configurations in front of the window are the same as before
2 First SpringBoot program
Target: Output HelloSpringBoot to web browser
The steps are as follows:
2.1 Create Parent Project
2.2 POM file defining parent project
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersio n> <groupId>com.shouyi</groupId> <artifactId>shouyixueyuan-springboot-water</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>shouyixueyuan-springboot-water-demo-01</module> </modules> <!-- POM Project Object Management It is Maven Core of the project <packaging>pom</packaging> The packaging type is pom,Usually used in parent projects The functions are as follows: 1 used to jar Version control of packages 2 Parent Project Usage Maven Modular Management Project(There can be several sub-modules under each parent project) 3 Submodule reuses parent module's groupId,version. Just define artifactId --> <packaging>pom</packaging> <!--Unified Management Dependency jar Package Version --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!--SpringBoot Version 2.3.0.RELEASE--> <springboot.version>2.3.0.RELEASE</springboot.version> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.23</druid.version> <mybatisplus.version>3.3.2</mybatisplus.version> <lombok.version>1.18.20</lombok.version> <jcl-over.version>1.7.26</jcl-over.version> <slf4j.version>1.7.26</slf4j.version> <junit.version>1.8.0</junit.version> <log4j.version>1.2.17</log4j.version> <hutool.version>5.1.0</hutool.version> <swagger.version>2.9.2</swagger.version> </properties> <!--Parent project manages only child modules jar Version of the package,Do not import jar Package that allows a child module to reference a parent project dependency without displaying a listed version number--> <dependencyManagement> <dependencies> <!--Used SpringBoot Version 2.3.0.RELEASE--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springboot.version}</version> <!--Can only be used in dependencyManagement Inside, the role is to manage dependencies jar Version of the package, maintaining all dependencies of the current project jar Unified versions of packages--> <scope>import</scope> <!--Introducing the download Jar All dependencies of the package--> <type>pom</type> </dependency> <!--MySQL Database Driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--mybatis-plus Persistent Layer Framework Launcher--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.version}</version> </dependency> <!--Druid Database Connection Pool Launcher--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- Hutu Tool Class --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <!-- Lombok Plug-in unit --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> <version>${lombok.version}</version> <!--Dependencies are not passed between multiple projects--> <optional>true</optional> </dependency> <!--swagger Is a front-end and back-end separated interface document--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <!--swagger ui--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!-- unit testing --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Logging Tool Class --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${jcl-over.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <resources> <resource> <directory>src/main/java</directory> <!--Compile java Simultaneous import of source files src/main/java All in Catalog properties Profiles and xml configuration file--> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <!-- Configuration files are excluded at compile time Java source file--> <directory>${project.basedir}/src/main/resources</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <!--SpringBoot To configure Maven Plug-in unit--> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.3 Create Submodules
Select the project, right-click New-->Module
Select Maven and click Next
Define the module name, click Finish
2.4 POM files defining sub-modules
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>minzu-springboot</artifactId> <groupId>com.minzu</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>minzu-springboot-demo-01</artifactId> <dependencies> <!--Springboot Development Web Application Launcher--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--Exclude SpringBoot All log packages that are dependent by default--> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <!-- Starter for monitoring system health--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--Hot Deployment--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!--Springboot Log Launcher--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.3.8.RELEASE</version> </dependency> <!--SpringBoot Unit Test Dependency--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!--Exclude JUnit 4 Engine Test--> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--MySQL Database Driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Tool plug-ins that provide many useful annotations --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <!-- Hutu Tool Class Dependency jar --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency> <!--Log Dependency jar--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> </dependency> </dependencies> </project>
2.5 Writing YML configuration files
application.yml
.yml is a special configuration file and is also a configuration file for Springboot
What is 2.5.1 YML
YAML (Ain't a Markup Language)YAML is not a markup language, usually a file with a suffix of.yml, is an intuitive computer-recognized data serialization format, is easy to read by humans, and is a language specially designed for writing profiles. Can be used for example: Java, C/C++, Ruby, Python, Perl, C#, PHP, etc.
2.5.2 YML Advantages
-
YAML is easy to read.
-
YAML data is portable between programming languages.
-
YAML is expressive and extensible.
-
YAML is easy to implement and use.
2.5.3 YML syntax
-
k: v represents a key-value pair relationship and must have a space after the colon
-
Case Sensitive
-
Tab keys are not allowed for indentation, only spaces are allowed.
# Microservice Application Port Number server: port: 8081
2.6 Write log profile
Define the log configuration file log4j.properties in the resources directory with the following configuration items:
# Export log information at the log level of DEBUG to both file (file) and console (console) destinations log4j.rootLogger=INFO, file, console # Settings for file output log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/System.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n #Settings for console output log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} %5p %c{1}:%L - %m%n #Log Output Level log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
2.7 Write Controller
/** * TODO * * @author caojie * @version 1.0 * A class decorated with the @RestController annotation is injected into the SpringBoot container at program startup, indicating that this class acts as a Controller and that it * All methods defined inside are returned to the browser as Json strings */ @RestController public class HelloController { /** * @GetMapping Map HTTP GET requests to specific methods. For example, map browser/hello requests to the hello() method * @return */ @GetMapping("/hello") public String hello() { return "Hello SpringBoot"; } }
2.8 Writing the SpringBoot startup class
package com.wenhua.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * TODO * SpringBoot Entrance * @author caojie * @version 1.0 The class decorated with the @SpringBootApplication annotation is the SpringBoot startup class */ @SpringBootApplication public class HelloSpringBoot { public static void main(String[] args) { SpringApplication.run(HelloSpringBoot.class,args); } }
2.9 Running Programs
http://localhost:8081/hello
2.10 Summary
Parent Project: 1 Create Project 2 Write the POM 3 of the parent project Delete the src directory of the parent project
Sub-module: 1 Establish sub-module 2 Write POM 3 yml configuration file 4 Define log file 5 Write Controller controller class 6 Write SpringBoot entry class
Core:
@SpringBootApplication @RestController
3 Use SpringBoot to output customer information in browser
3.1 Create a Module
3.2 POM files defining sub-modules
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>shouyixueyuan-springboot-water</artifactId> <groupId>com.shouyi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shouyixueyuan-springboot-water-demo-01</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--Exclude SpringBoot All log packages that are dependent by default--> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <!-- Tools for monitoring health--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--Hot Deployment--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!--Springboot Log Launcher--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.3.8.RELEASE</version> </dependency> <!--SpringBoot Unit Test Launcher--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!--Exclude JUnit 4 Engine Test--> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--MySQL Database Driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--MyBatis-plus starter baomidou Baked rice bean--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <!-- Druid Database Connection Pool Launcher The database connection pool developed by Alibaba is also the best one at present. Features: Yes SQL Statement Monitoring --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!-- Tool plug-ins that provide many useful annotations, --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--swagger: Front-End Separated Interface Document--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <!-- Hutu Tool Class --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency> <!--Log Configuration Dependency jar--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> <!--Unit Test Dependency jar--> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> </dependency> </dependencies> </project>
3.3 Write YML configuration file
application.yml
server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/shouyi?useUnicode=true&characterEncoding=UTF8&useSSL=false driver-class-name: com.mysql.jdbc.Driver username: root password: root druid: min-idle: 5 max-active: 10 max-wait: 5000 type: com.alibaba.druid.pool.DruidDataSource mybatis-plus: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # Global policy configuration, all with auto policy (database ID self-growth) global-config: db-config: id-type: auto # Configure Log Level logging: com.shouyi: info
3.4 Write log profile
# Export log information at the log level of DEBUG to both file (file) and console (console) destinations log4j.rootLogger=INFO, file, console # Settings for file output log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/caojie.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n #Settings for console output log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} %5p %c{1}:%L - %m%n #Log Output Level log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
3.5 Define related package s
entities packages place entity classes
The mapper package places the Mapper map file
service packages place business logic interfaces and implementation classes
Controller package places Controller controller
3.6 Define SpringBoot Startup Class
package com.wenhua.springboot; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * TODO * @author caojie * @version 1.0 @MapperScan Scan all interfaces under the specified package and generate corresponding implementation classes after compilation */ @SpringBootApplication @MapperScan("com.wenhua.springboot.mapper") public class CustomerSpringBootApplication { public static void main(String[] args) { SpringApplication.run(CustomerSpringBootApplication.class,args); } }
3.7 Install Lombok plug-ins in IDEA
File--->Plugins
3.8 Define Customer Entity Class
package com.wenhua.entities; import lombok.AllArgsConstructor; import lombok.Data; /** * TODO * @author caojie * @version 1.0 */ @Data @AllArgsConstructor @NoArgsConstructor public class Customer { private Integer cid; private String custName; private String custMobile; private String custAddress; private Integer custTicket; }
3.9 Define Mapper Interface
Encapsulates all operations accessed by the database (CRUD add-delete check). Mapper is used to map attributes between database tables and Java entity classes
package com.wenhua.springboot.mapper; import com.wenhua.springboot.entities.Customer; import java.util.List; /** * TODO:Mapper Used for mapping between database tables and Java entity classes, encapsulating all additions and deletions of the database, for interacting with the database * @author caojie * @version 1.0 */ @Repository public interface CustomerMapper extends BaseMapper<Customer> { List<Customer> listCustomer(); }
3.10 Writing Business Logic Interface Service
package com.shouyi.service; import com.shouyi.pojo.Customer; import java.util.List; /** * TODO * @author caojie * @version 1.0 * @date 2021/10/09 13:10 */ public interface CustomerService { String hello(); List<Customer> listCustomer(); }
3.11 Writing Service implementation classes
package com.shouyi.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.shouyi.mapper.CustomerMapper; import com.shouyi.pojo.Customer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * TODO * @author caojie * @version 1.0 * @date 2021/10/19 13:10 */ @Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerMapper customerMapper; @Override public List<Customer> listCustomer() { return customerMapper.selectList(null); } }
3.12 Writing Controller Controller Controller
package com.wenhua.springboot.controller; import com.wenhua.springboot.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * TODO: When the customer enters in the browser http://localhost:8097/listCustomer , the listCustomer() method dispatched to the CustomerController handles the request (outputs all customer information to the browser) * @author caojie * @version 1.0 * @date 2021/6/21 21:48 */ @RestController public class CustomerController { /** * @Autowired Automatically assemble CustomerService objects of the business logic layer based on type */ @Autowired private CustomerService customerService; @GetMapping("/listCustomer") public String listCustomer(){ return customerService.listCustomer().toString(); } }
3.13 Start application
http://localhost:8081/listCustomer
3.14 Summary
Place entity class 1 under entities package
The mapper package places the Mapper interface, which is responsible for mapping database tables and Java entity class properties 2
service packages place business logic interfaces and implementation class 3
Controller package placement controller class 4
service business logic relies on the Mapper interface, so it needs to be assembled automatically
Controller controller needs to depend on service, so it needs to be assembled automatically