Create a commonly used dependency configuration and commonly used dependencies in the SpringBoot Maven project
The total (father) dependency of the project
<parent> <artifactId>spring-boot-dependencies</artifactId> <groupId></groupId> <version>2.3.</version> </parent>
When we use spring or spring-boot development projects, we need to introduce many dependencies, including spring itself components, various spring-boot-starters, and other third-party dependencies (such as slf4j, redis). If you have too many dependencies, the choice of the version is a problem. I am afraid that the wrong version will lead to some unexpected bugs.
The function of spring-boot-dependencies is mainly to constrain versions. Various version numbers are declared in this package for sub-items to reference. Similar to spring-cloud-dependencies and spring-cloud-alibaba-dependencies are the versions that declare cloud and cloud-alibaba components. You can click in and see what you want. If you use it in the < dependency> below, you can do without configuring the version number <version>
2. Executable web applications and include SpringBoot core launcher
Contains various springboot configuration logs, etc., and the dependency will be automatically introduced when creating a project.
- Support annotations: @controller, @Service, @Component, @Resource are spring, so spring boot can be used after it is created (supported by spring-boot-starter)
- Support annotations: @RestController, @RequestMapping, @ResponseBody, @JsonFormat (supported by spring-boot-starter-web)
<!--Spring Boot Web--> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
What is spring-boot-starter-web?
spring-boot-starter-web is a dependency library. Spring Boot is an open-source framework created based on Spring. It provides spring-boot-starter-web (web scene launcher) to support web development. spring-boot-starter-web provides embedded Servlet containers and SpringMVC provides a large number of automatic configurations, which can be used in most web development scenarios.
As long as we introduce spring-boot-starter-web dependency in Spring Boot project, we can use Spring MVC for web development even without any configuration. Spring Web launcher uses Spring MVC, REST and Tomcat as the default embedded servers. A single spring-boot-starter-web dependency passively obtains all dependencies related to web development. It also reduces the build dependency count.
After configuring the dependency, no need to configure it
<dependency> <groupId></groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
Because spring-boot-starter-web includes spring-boot-starter, etc., you can click in and take a look
Test, the dependency will be automatically introduced when creating a project
Used to write springboot Test test class
Use of SpringBoot Test Test Class
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Data configuration
When configuring mysql dependencies, if the version number is not written, the default version of mysql dependencies will be introduced.
- SpringBoot2. The default version of mysql 8 will be used in the future.
- SpringBoot2. The default version of mysql was used before
When configuring data sources, there are differences:
- Configure the lower version:
-class-name= =jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&useSSL=false =root =123456
- Configure a higher version:
-class-name= =jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false =root =123456 <!--MySQL Connect components--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
Data processing layer persistence layer framework, connecting to database
Focus on writing SQL, rather than constantly operating Connection, Statment, and ResultSet through the traditional jdbc method
Annotation @Mapper Specify the mapping interface
Configure the automatically recognized xml in the configuration file:
mybatis:
mapper-locations: classpath:mapper/**/*.xml type-aliases-package:
<!--MyBaits--> <dependency> <groupId></groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>
Connection pool
<!--Druid--> <!--Can't be worthy of this becausedruid-spring-boot-starterThere is already inside,Take this dependency with you,The code is more readable,Anyway, it has no effect on anything else--> <dependency> <groupId></groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!-- Druid Spring Boot Components--> <dependency> <groupId></groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency>
Configure the use in the yaml file:
spring: datasource: # Basic data source configuration url: jdbc:mysql://localhost:3306/hotel?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: type: # Other configurations of data source druid: # Configure the initialization size, minimum, and maximum number of threads initialSize: 5 minIdle: 5 # The number of CPU cores +1, it can also be larger but not more than 20. The performance of the database is too many connections when locking the database maxActive: 20 # Maximum waiting time, intranet: 800, external network: 1200 (three handshakes 1s) maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 # Configure the maximum space time of a connection in the pool, in milliseconds minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 testWhileIdle: true # Set whether to check the validity of the connection when obtaining the connection from the connection pool, check true, and do not check false testOnBorrow: true # Set whether to check the validity of the connection when returning the connection from the connection pool, check true, and do not check false testOnReturn: true # Can support PSCache (improve writing and query efficiency) poolPreparedStatements: true # Configure filters for monitoring statistics intercepting. After removing it, the monitoring interface SQL cannot be counted. 'wall' is used for firewall filters: stat,wall,log4j # Keep long connections keepAlive: true maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: =true;=500
Format conversion tool Fastjson
Fastjson is a Java library that can convert Java objects to JSON format, and of course it can also convert JSON strings to Java objects.
In Java, Json, String, jsonObject, and jsonArray formats are converted to each other
<dependency> <groupId></groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency>
The best thing about lombok is the annotation, and one annotation kills a lot of code
Annotations in entity classes.
- @Data: The Get and Set methods can be omitted directly
- @Slf4j: There is no need to introduce log dependencies and configuration logs separately, directly print logs ( )
How to use the lombok plugin in IDE compiler?
- You can search and download it directly in the compiler in idea, so I won't explain it much
- eclipse needs to download the package from the official website, then double-click to start the jar package, gradually operate, point to, and restart eclipse
<!--LomBok--> <dependency> <groupId></groupId> <artifactId>lombok</artifactId> </dependency>
9.AOP-oriented programming
Supported annotations: @AspectJ, @Pointcut, notification annotations (such as: @Before, @After, etc.), @Aspect and custom annotations
- Spring-boot-starter-aop and its usage scenario description
- Use of Aop annotations in SpringBoot + custom annotations
<!--Spring Boot Aop--> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
Implementation of verification parameters
Supported annotations: @Max, @Min, etc.
Common annotations and demos
<!--Spring Validation--> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
monitor
Mainly used for server operation and maintenance, and the development process is not often used
Springboot Monitor Actuator settings
<!--Spring Boot Actuator--> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Toolkit
Provides many packaging methods for developers to use
<!--Hutool--> <dependency> <groupId></groupId> <artifactId>hutool-all</artifactId> <version>5.4.7</version> </dependency>
Its dependencies include junit-jupiter-api, junit-jupiter-engine, and junit-vintage-engine.
<!--Junit--> <dependency> <groupId></groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency>
14. Packaging configuration
Used to generate packages deployed to the server
JAVA project in server deployment process
<build> <plugins> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
15. Multi-yaml file configuration
Specify which file it uses, and does not configure the following profiles, but the file format created is also available if this is the case.
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profilesActive>dev</profilesActive> </properties> </profile> <profile> <id>pro</id> <properties> <profilesActive>pro</profilesActive> </properties> </profile> </profiles>
16.Use properties tags to unify encoding and JAVA version
<!--Unified encoding andJAVAVersion--> <properties> <>UTF-8</> <>1.8</> <>1.8</> <>1.8</> </properties>
-plus
Upgraded tools based on mybatis avoid writing a large number of xml files when using mybatis
<dependency> <groupId></groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
Hot deployment
After modifying the java code, you can directly test the latest without restarting the project, which omits the trouble of constantly modifying the code and restarting the project.
<dependency> <groupId></groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.