1. Summary of knowledge points last week
1.1 Linux
-
Know Linux
-
virtual machine
-
Install VMware--Create a virtual machine--Install CentOS
-
-
Cloud host
-
Remote tool
-
xftp file management
-
xshell remote terminal
-
-
Linux file system
-
home root etc usr
-
-
Linux commonly used instructions
-
File management
-
User rights
-
System operation
-
Software Management Instructions
-
wget
-
rpm
-
yum
-
-
-
Software management
-
JDK
-
Tomcat
-
MySQL
-
Nginx
-
-
FTP file upload (windows-(java)-Linux)
-
Project packaging and deployment
1.2 SpringBoot
-
Understanding SpringBoot
-
Habit is better than configuration/convention is greater than configuration
-
Automatic configuration (java configuration method)
-
-
Created the first SpringBoot application
-
Creation process
-
SpringBoot project directory structure
-
Start SpringBoot-
-
-
Custom banner
-
Global configuration file
-
application.properties
-
application.yml (yaml language)
-
-
java configuration method (review)
-
Starter POM (POM depends on importing starter-complete basic configuration)
-
A starter corresponds to a specific development scenario (redis)
-
-
How does SpringBoot complete automatic configuration?
-
Log configuration-(SpringBoot log framework)
1.3 Preview of knowledge points this week
-
SpringBoot
-
SpringBoot (thymeleaf, swagger, logging framework)
-
SpringBoot integrates tk.mapper (tk.mybatis)
-
shiro
-
shiro
2. Plan today
-
SpringBoot automatic configuration review
-
SSM development based on SpringBoot
-
Integrated database connection pool
-
Integrate MyBatis
-
Unit test based on SpringBoot
-
Global exception capture
-
Maven-based profile configuration
-
-
Custom starter implementation (principle)
Three, SpringBoot automatic configuration (review)
-
SpringBoot has built-in configuration of mainstream frameworks (based on java configuration). When the SpringBoot project is started, as long as
满足特定的条件
it will trigger the automatic configuration of SpringBoot -
When the SpringBoot project starts, it will load the spring.factories file through layer-by-layer calls. The spring.factories file contains the custom configuration supported by SpringBoot (various AutoConfig classes); all auto-configuration classes will be Scanning does not mean that all auto-configuration classes will be initialized. This configuration class will be initialized only when the conditions declared by an auto-configuration class are met.
-
Common automatic configuration loading conditions
@ConditionalOnClass @ConditionalOnBean @ConditionalOnProperty @EnableConfigurationProperties
Four, SSM development based on SpringBoot
4.1 Spring support
The SpringBoot project itself depends on Spring, so when we create a SpringBoot application, Spring's support is imported by default. In other words, as long as the SpringBoot project can be started, Spring (IoC\AOP) is supported by default
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
4.2 SpringMVC support
-
Depend on spring-boot-starter-web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
SpringMVC integration:
-
Add spring-boot-starter-web dependency in pom
-
When there is a spring-boot-starter-web dependency in a SpringBoot application, it will trigger WebMvcAutoConfiguration automatic configuration
-
In the WebMvcAutoConfiguration configuration, many configurations required by SpringMVC such as DispatcherServletAutoConfiguration are completed.
-
4.3 Druid support
-
Add druid dependency druid-spring-boot-starter
<!-- druid starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
-
Add mysql database driver dependency
<!--mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
-
Configure database connection pool properties in the global configuration file
spring: datasource: druid: url: jdbc:mysql://47.96.11.185:3306/test # MySQL如果是8.x com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver username: root password: admin123 initial-size: 1 min-idle: 1 max-active: 20
4.4 MyBatis support
-
Add MyBatis dependency mybatis-spring-boot-starter
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
-
Configure application
mybatis: mapper-locations: classpath:mappers/*Mapper.xml type-aliases-package: com.qfedu.springbootssm.beans
4.5 Use case (DAO implementation)
Book information management
-
Create data table
mysql> create table tb_books( -> book_id int primary key auto_increment, -> book_name varchar(100) not null, -> book_author varchar(50) not null, -> book_price float not null, -> book_desc varchar(400) -> );
-
Create entity class
public class Book { private Integer bookId; private String bookName; private String bookAuthor; private double bookPrice; private String bookDesc; //parameterless constructor //full parameter constructor //getter & setter }
-
Create DAO interface
public interface BookDAO { void insertBook(Book book) throws Exception; List<Book> listBooks() throws Exception; Book queryBookById(Integer bookId) throws Exception; }
-
Create a mapping file
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qfedu.springbootssm.dao.BookDAO"> <insert id="insertBook" parameterType="Book" useGeneratedKeys="true" keyProperty="bookId"> insert into tb_books(book_name,book_author,book_price,book_desc) values(#{bookName},#{bookAuthor},#{bookPrice},#{bookDesc}) </insert> <resultMap id="bookMap" type="Book"> <id column="book_id" property="bookId"></id> <result column="book_name" property="bookName"/> <result column="book_author" property="bookAuthor"/> <result column="book_price" property="bookPrice"/> <result column="book_desc" property="bookDesc"/> </resultMap> <select id="listBooks" resultMap="bookMap" resultSets="java.util.List" > select * from tb_books </select> <select id="queryBookById" resultMap="bookMap"> select * from tb_books where book_id=#{bookId} </select> </mapper>
-
Mapper registration
-
Solution 1: Add @Mapper annotation to DAO interface
-
Solution 2: Add @MapperScan(basePackages = "com.qfedu.springbootssm.dao") annotation to the startup class
-
4.6 Unit test based on SpringBoot
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootSsmApplication.class) public class BookDAOTest { @Resource private BookDAO bookDAO; @Test public void testInsertBook() throws Exception{ Book book = new Book(0,"Java","马Total",22.22,"Mr Ma's new work"); bookDAO.insertBook(book); System.out.println(book.getBookId()); //Assertion: Whether the return value and the expected value are always Assert.assertEquals(3,book. getBookId().intValue()); } }
4.7 Use case (service and controller implementation)
-
service interface
public interface BookService { public void insertBook(Book book) throws Exception; public List<Book> listBooks() throws Exception; }
-
service implementation class
@Service public class BookServiceImpl implements BookService { @Resource private BookDAO bookDAO; @Override public void insertBook(Book book) throws Exception { bookDAO.insertBook(book); } @Override public List<Book> listBooks() throws Exception { return bookDAO.listBooks(); } }
-
controller
-
Implementation of vo
@Controller @RequestMapping("book") public class BookController { @Resource private BookService bookService; @ResponseBody @RequestMapping("add") public ResultVO insert(Book book) { ResultVO resultVO = null; try { bookService.insertBook(book); resultVO = ResultVO.getSuccessVO("添加成功!"); } catch (Exception e) { e.printStackTrace(); resultVO = ResultVO.getFailVO("添加失败!"); } return resultVO; } @ResponseBody @RequestMapping("list") public ResultVO listBooks(){ ResultVO resultVO = null; try { List<Book> books = bookService.listBooks(); resultVO = ResultVO.getSuccessVO("Query successful",books); } catch (Exception e) { e.printStackTrace(); resultVO = ResultVO. getFailVO("Network delay, please refresh and pick it up again!"); } return resultVO; } }
-
4.8 Global exception handling
-
Use global exception handling classes to handle exceptions uniformly
-
1. Create a global exception handling class, add @ControllerAdvice annotation
-
2. Provide exception handling methods in the exception handling class
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler @ResponseBody public String exceptionHandler(Exception e){ String message = e.getMessage(); System.out.println("---------------- "+message); //Different processing according to different exception types return message; } }
-
4.9 SpringBoot integrated Maven profile configuration
In actual development, many configurations of development environment, test environment, and production environment are different; we need to make different configurations for different environments, such as:
Log configuration
Development environment - d:/logs/dev
Port configuration
Development environment - 8080
Test environment - 8081
Production environment - 8082
-
Create different configuration files (application-***.yml) for different environments
-
Development environment application-dev.yml
logging: file: path: d:/logs/dev server: port: 8080
-
Test environment application-test.yml
logging: file: path: d:/logs/test server: port: 8081
-
Production environment application-prod.yml
logging: file: path: d:/logs/prod server: port: 8082
-
-
Configure profile in pom.xml
<profiles> <!--开发环境--> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileName>dev</profileName> </properties> </profile> <!--测试环境--> <profile> <id>test</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <profileName>test</profileName> </properties> <profile> <!--Production environment--> </profile> <id>prod</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <profileName>prod</profileName> </properties> </profile> </profiles>
-
Dynamically load profile in application.yml
# Dynamically load three configuration files for different environments in application.yml spring: profiles: active: @profileName@
-
Project packaging (jar) operation
-
Bale
-
run
java -jar springboot-profile-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod -