idea to build the Springboot project from scratch

Source: Internet
Author: User

(1) It is the spring upgrade version, the spring container can do things, it can do, and more convenient, from the configuration form, Springboot completely abandoned the cumbersome way of XML file configuration, but instead of annotated way to achieve, although essentially, is similar (like packet scanning, annotation scanning, class loading, and so on).
(2) Springboot integrated plug-in more, thus using a lot of services, all just introduce a dependency, a few annotations and Java classes can be used, specific reference related manuals.
(3) In the Web application development of this piece, the previous application is generally packaged into a war package, and then published to the relevant server container (such as Tomcat), although Springboot can also do so, But the more common form under springboot is to package the Springboot app into an executable jar package file. The reason for this is that you can directly think of the Springboot application as a Java application, whose Web application can have no WebApp directory (not to mention XML), which recommends using HTML pages and using them as static resources.
The following specific record, how to start from scratch under idea, step by step to build Springboot Web application, here is Maven for dependency management, novice start, have any questions, please refer to Springboot official website.
It is necessary to note that Springboot relies on a JDK version of 1.8 and above.
(1) file->new, choose Maven, create an empty project, and go directly to next.

(2) Fill in the project name

(3) Next in the end, the results create a MAVEN-based empty Java project with the directory structure:

(4) Introduction of Springboot dependency in pom file

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
(5) Create a new controller package to hold all controllers, as in the official case, use Samplecontroller as the first test cases. The code is as follows:
/**

  • Created by Song on 2017/2/15.
  • Test code br/>*/in the official sample project
    @Controller

    public class Samplecontroller {br/> @RequestMapping ("/")

    String Home () {
    Return "Hello world!";
    }
public static void main(String[] args) throws Exception {    SpringApplication.run(SampleController.class, args);}

}
Notice that there is a main function, and then it is said that the Springboot application is generally packaged into an executable jar package to publish, and this main function is the entry for the entire project. The reason why you can do this is because springboot even Tomcat8 as a plug-in, so it doesn't have to be the same as the previous SSM architecture, and you need to configure the war package under Tomcat to run. Directly click to run the main function, and then the browser link bar, enter the address http://localhost:8080/, you can see the printed string "Hello world!" The This is one of the most basic springboot-based Web applications available on the website, so it's easy.
Of course, a basic Web application, the structure certainly will not be so simple. The following is how to build a complete Web application with an MVC structure based on the above, in which the database is mysql,orm using spring Data JPA, and the front-end page uses JS+HTML5. (There are, of course, other ways, such as ORM frameworks using MyBatis, etc., which are not covered in this article.) )
(6) Create a new Application.properties file (or yml file) in the resource directory, naming and location is the default configuration file for Springboot. In this file, all the module configuration contents are recorded. For example, Tomcat's port (default 8080) and encoding method, etc.:

server.port=8080
Server.tomcat.uri-encoding=utf-8
(7) Introduction of dependencies required in this project (MySQL connection driver and spring Data jpa,thymeleaf template engine)
<!--Https://mvnrepository.com/artifact/mysql/mysql-connector-java--
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.