Spring Boot of Java framework

SpringBoot series of teaching materials (1)-Basics-Introduction to Springboot-Create a springboot project that can run in Eclipse, non-STS plug-in way


In the process of learning SSM (H), a lot of configuration work needs to be done. In fact, many configuration actions are only means, not ends. Based on this consideration, to simplify the simplification, the omission of the omission, developers only need to care about providing business functions, this is Spring Boot.

In other words, Spring Boot can be simply regarded as a simplified SSM (H) developed according to the convention. Development speed has been greatly improved. However, it is best to still have the foundation of SSM (H), otherwise Spring MVC, Mybatis, Hibernate are used, or feel puzzled.

Note: (H) in parentheses means Hibernate.
Step 1: About Environment Version
Step 2: About SpringBoot version
Step 3: About IDE
Step 4: plug on STS
Step 5: Run first, to see the effect, then learn
Step 6: imitation and troubleshooting
Step 7: About Tomcat
Step 8: create a project
step 9: enter the project parameters
step 10: pom.xml
step 11: Application.java
step 12: HelloController.java
step 13: running and testing
Step 1: About the environment version
Try to use JDK8 32 version, below 8 will definitely not work, higher than 8 will give Schrodinger BUG, ​​try to use 8, please download JDK8:  download and configure the JDK environment

Eclipse also try to use the webmaster provides, to avoid environmental problems: Eclipse

Note: The 64-bit version of JDK8 is not compatible with the jar package provided by the current project. Please use the 32-bit version provided by the webmaster.
Step 2: About SpringBoot version
This series of tutorials uses 1.5.9.RELEASE, and there is now a newer version of Springboot 2.x.
Their usages are similar, but there are still some differences.

It is hoped that after learning, mastering, and proficiency on the basis of 1.5.9.RELEASE, students will switch to the 2.x version if they are still interested.
It should be emphasized here that if you switch to the 2.x version now, and run the code in the webmaster's current springboot series of textbooks, there will be unpredictable errors, and finally it will create obstacles for yourself to learn.
So the webmaster shouted 3 sentences in his throat: Do not modify the version number at will! Do not modify the version number at will! Do not modify the version number at will!
Step 3: About IDE
The current mainstream development tools are Eclipse and IDEA, so let's talk separately. The current knowledge point uses Eclipse, and the next knowledge point uses IDEA.
Step 4: About STS plugin
Eclipse provides a plug-in specially developed for SpringBoot called Spring Tool Suite. The installation of this plug-in is using a foreign source, the installation is very card, at least tens of minutes to go.
The SpringBoot application is essentially a Java program, and its style is maven, so it is also a Maven project. Next, we will create it according to the maven project. Don't be overshadowed by the bells and whistles.
About STS plugin
Step 5: Run first, see the effect, then learn
The old rules, first download download area (click to enter) can run the project, configuration up and running, after confirming available, re-learning to do what steps to achieve this effect.
This is a maven project, approach to introducing Eclipse maven items, refer to the steps of: introducing in Eclipse maven project
and run classes: primary method of com.how2java.springboot.Application
then access address:
http: //127.0.0.1:8080/hello

You can see the effect as shown.
Note: This maven project uses a lot of jar packages. If the jar package download is slow, you can use the library I am using: the maven repository used by the webmaster , it is a bit big ~
Run first, see the effect, then learn
Step 6: imitate and troubleshoot
After ensuring that the runnable project runs correctly, follow the steps of the tutorial strictly and imitate the code again.
The imitation process will inevitably lead to discrepancies in the code, resulting in the inability to obtain the expected operating results. At this moment, by comparing the correct answer (runnable item) and your own code, you can locate the problem.
In this way, learning is effective and troubleshooting is efficient, which can significantly improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my executable project folder.
This software is very fast hardware, you can know which of the two file folder wrong, and clearly marked
here provides installation and use of green Tutorial: DiffMerge download and tutorial
Step 7: about Tomcat
Students who  run first, see the results, and then learn to run successfully may feel a little strange. This is obviously a web program running, why not start tomcat? Instead of starting the main method of a Java class?
This is because the main method of the com.how2java.springboot.Application class embeds tomcat into it, and there is no need to start tomcat manually.
Step 8: create the project
Then we started to create this project from 0.
First create a maven project
menu -> File -> New -> Other -> Maven -> Maven -> Maven Project -> New Maven Project
hook on the Create a simple project (skip archetype selection ), and see, Springboot is a simple the project maven

Note: before you create a project, we should  first run, to see the effect, re-learning  program started to return to step in, otherwise it will go wrong.
Create project
Step 9: Enter the project parameters
Then enter these values ​​in the red box in the parameter
Enter project parameters
Step 10: pom.xml
Overwrite the pom.xml in the project with the content in the following pom.xml.

After overwriting, right click on the project-> Maven-> Update Project to update the project.

This pom.xml specifies the jar package needed for the current project.
Note: This maven project uses a lot of jar packages. If the jar package download is slow, you can use the library I am using: the maven repository used by the webmaster , it is a bit big ~
pom.xml
<? 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</ modelVersion >
 
   < groupId >com.how2java</ groupId >
   < artifactId >springboot</ artifactId >
   < version >0.0.1-SNAPSHOT</ version >
   < name >springboot</ name >
   < description >springboot</ description >
   
     < parent >
         < groupId >org.springframework.boot</ groupId >
         < artifactId >spring-boot-starter-parent</ artifactId >
         < version >1.5.9.RELEASE</ version >
     </ parent >
 
     < dependencies >
         < dependency >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-web</ artifactId >
         </ dependency >
         < dependency >
               < groupId >junit</ groupId >
               < artifactId >junit</ artifactId >
               < version >3.8.1</ version >
               < scope >test</ scope >
         </ dependency >
     </ dependencies >
 
     < properties >
         < java.version >1.8</ java.version >
     </ properties >
 
     < build >
         < plugins >
             < plugin >
                 < groupId >org.springframework.boot</ groupId >
                 < artifactId >spring-boot-maven-plugin</ artifactId >
             </ plugin >
         </ plugins >
     </ build >
 
</ project >
Step 11: Application.java
Create Application.java, the annotation @SpringBootApplication indicates that this is a SpringBoot application, running its main method will start tomcat, the default port is 8080
package  com.how2java.springboot;
 
import  org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public  class  Application {
 
     public  static  void  main(String[] args) {
         SpringApplication.run(Application. class , args);
     }
 
}
Step 12: HelloController.java
Then create the controller class HelloController, this class is an ordinary controller in Spring MVC.
@RestController is a new annotation in spring4, which is the abbreviation of @ResponseBody and @Controller.
package  com.how2java.springboot.web;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RestController;
 
@RestController
public  class  HelloController {
 
     @RequestMapping ( "/hello" )
     public  String hello() {
         return  "Hello Spring Boot!" ;
     }
 
}
Step 13: run and test
Next, run Application.java , and then visit the address
http: //127.0.0.1:8080/hello

You can see the test effect
Run and test

Learn more about Spring Boot: https://how2j.cn/k/springboot/springboot-eclipse/1640.html?p=144217

Guess you like

Origin www.cnblogs.com/GGLoner/p/12693485.html