This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

1. Springboot quote

Spring Boot is a new framework from the Pivotal team designed to simplify the initial setup and development of Spring applications. The framework uses a specific way to configure so that developers no longer need to define boilerplate configurations. In this way, Spring Boot aims to be a leader in the burgeoning field of rapid Application development.

Springboot (Microframework) = SpringMVC (Controller) + Spring (Project Management)


2. Characteristics of Springboot

1. Micro-frameworks, born with Spring4, such as @RestController 2. Springboot can create applications that run independently of container 3. 4. Maven provides minimal configuration, with the disadvantage of introducing a lot of packages that you don't need. Depend on the project to automatically configure Spring, and do what you need to do, controllable 6. 7. Simplified configuration, no need to configure too much XML, just need to use annotations can achieve the same effect


3. Build the Springboot environment

Environmental requirements:

  1. MAVEN 3.x+
  2. Spring FrameWork 4.x+
  3. JDK7.x +
  4. Spring Boot 1.5 x +

3.1 Introduce dependencies into the project

<! > <parent> <groupId>org.springframework. Boot </groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.57..RELEASE</version> </parent> <dependencies> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>Copy the code

3.2 Importing a Configuration file

Project in SRC/main/resources/application. The yml

3.3 Creating packages and Creating Controllers

// Create the specified package structure in the project

/* com +| xxx +| controller */ 
                  @Controller
                    @RequestMapping("/hello")
                    public class HelloController {
                        @RequestMapping("/hello")
                        @ResponseBody
                        public String hello(a){
                            System.out.println("======hello world=======");
                            return "hello"; }}Copy the code

               

3.4 Writing entry classes

Create the entry class Application in the following package structure in the project
/* com +| xxx */
            @SpringBootApplication
            public class Application {
                public static void main(String[] args) { SpringApplication.run(Application.class,args); }}Copy the code

3.5 Running main to start the project

o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8989 (http)Com.xxxx. Application: Started Application in 2.152seconds (JVM running for 2.611)

// Note: The startup is successful if the preceding logs are displayed
Copy the code

3.6 Accessing Projects

/ / note: springboot default there is no project name / / access path of the project: http://localhost:8080/hello/helloCopy the code


4. The Tomcat startup port is occupied

In the application. Yml configuration

Server: port: 8989 # used to specify the embedded server port number context-path: /springboot # Used to specify the project access pathCopy the code


5. Springboot related notes

Description: Spring Boot usually has a class called xxxApplication with a main method in the entry class, In the main method used in SpringApplication. Run (xxxApplication. Class, args) start springboot application project. @restController: it is the combination of @Controller+ @responseBody, which supports RESTful access and returns JSON strings. The @springBootApplication annotation is equivalent to the following three annotations: @Configuration Automatically configures spring and SpringMVC initial setup when the project starts @EnableAutoConfiguration automatically integrates with third party technologies integrated in the project @ComponentScan Scan annotations in subpackages of entry classes and subpackage descendantsCopy the code


6. Split the configuration file in Springboot

It is necessary to separate the production configuration from the test configuration. Springboot also provides a configuration file split method. The following uses inconsistent item names in production as an example:Copy the code

In production project name: CMFZ in test project name: Springboot port: 8080 Split as follows: # application dev. Yml server: context-path: / CMFZ # application dev. Yml server: context-path: /springbootCopy the code


7. Integrated JSP display in Springboot

7.1 Introduction of JSP integrated JAR package

<dependency> <groupId> JSTL </artifactId> JSTL </artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>Copy the code

7.2 Introduction of JSP running plug-in

<build> <finalName>springboot_day1</finalName> <! <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>Copy the code

7.3 Configuring a View Parser

# Introduce the view parser spring: MVC: view: prefix: / # / to represent the page suffix:.jsp in the webapp in the projectCopy the code

7.4 Enabling the ACCESS to the JSP Page

http://localhost:8989/cmfz/index.jsp
Copy the code


8. Springboot integrates mybatis

8.1 Importing Dependencies

<! <dependency> <groupId>org.mybatis.spring. Boot </groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.3.3 < / version > < / dependency > < the dependency > <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> < the groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.38 < / version > < / dependency >Copy the code

< p style = "max-width: 100%; clear: both; min-width: 1pxCopy the code

8.2 Configuring the Configuration File

spring: mvc: view: prefix: / suffix: .jsp datasource: type: Org.apache.com mons. DBCP. BasicDataSource # specify connection pool type driver - class - name: com. Mysql.. JDBC driver # specifies the url: JDBC: mysql: / / localhost: 3306 / CMFZ # url specified username: root # specifies the user name password: XXXX # password specifiedCopy the code

8.3 Adding The MyBatis Configuration

# configuration file to add the following configuration: : mybatis mapper - locations: the classpath: com/baizhi/mapper / *. # XML mapper configuration file location specified type - aliases - package: Com.xxxx. entity # Specifies the class to aliasCopy the code

// Add the following configuration to the entry class: @springBootApplication @mapperscan ("com.baizhi.dao") // This configuration must be added to the entry class main(String[] args) { SpringApplication.run(Application.class,args); }}Copy the code

Table 8.4 built

8.5 Developing entity classes

8.6 Develop DAO interfaces and Mapper

8.7 Service Development and Implementation

8.8 Introducing test dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
</dependency>
Copy the code

8.9 Writing test Classes

@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class TestClazzService { @Autowired private ClazzService clazzService; @Test public void test(){ List<Clazz> all = clazzService.findAll(); for (Clazz clazz : all) { System.out.println(clazz); }}}Copy the code