一、创建SpringBoot项目
二、导入Jar包(pom.xml)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5
6 <groupId>com.example</groupId>
7 <artifactId>testdemo1</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <packaging>jar</packaging>
10
11 <name>TestDemo1</name>
12 <description>Demo project for Spring Boot</description>
13
14 <parent>
15 <groupId>org.springframework.boot</groupId>
16 <artifactId>spring-boot-starter-parent</artifactId>
17 <version>1.5.4.RELEASE</version>
18 <relativePath/> <!-- lookup parent from repository -->
19 </parent>
20
21 <properties>
22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24 <java.version>1.7</java.version>
25 </properties>
26
27 <dependencies>
28 <dependency>
29 <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-starter-web</artifactId>
31 </dependency>
32
33 <dependency>
34 <groupId>org.springframework.boot</groupId>
35 <artifactId>spring-boot-starter-test</artifactId>
36 <scope>test</scope>
37 </dependency>
38
39 <!--引入配置文件-->
40 <dependency>
41 <groupId>org.springframework.boot</groupId>
42 <artifactId>spring-boot-configuration-processor</artifactId>
43 <optional>true</optional>
44 </dependency>
45
46 <!--添加mysql的组件-->
47 <dependency>
48 <groupId>mysql</groupId>
49 <artifactId>mysql-connector-java</artifactId>
50 </dependency>
51
52 <!--servlet依赖-->
53 <dependency>
54 <groupId>javax.servlet</groupId>
55 <artifactId>javax.servlet-api</artifactId>
56 </dependency>
57
58 <!--jstl依赖-->
59 <dependency>
60 <groupId>javax.servlet</groupId>
61 <artifactId>jstl</artifactId>
62 </dependency>
63
64 <!--使jsp页面生效-->
65 <dependency>
66 <groupId>org.apache.tomcat.embed</groupId>
67 <artifactId>tomcat-embed-jasper</artifactId>
68 </dependency>
69
70 <!--整合mybatis-->
71 <dependency>
72 <groupId>org.mybatis.spring.boot</groupId>
73 <artifactId>mybatis-spring-boot-starter</artifactId>
74 <version>1.1.1</version>
75 </dependency>
76
77
78 </dependencies>
79
80 <build>
81 <plugins>
82 <!--将SpringBoot的Maven插件全部引入 包含tomcat-->
83 <plugin>
84 <groupId>org.springframework.boot</groupId>
85 <artifactId>spring-boot-maven-plugin</artifactId>
86 </plugin>
87 </plugins>
88 </build>
89
90 </project>
三、配置yml文件、启动类
application.yml内容:
1 server:
2 #服务端口
3 port: 8080
4 #项目contextPath,一般在正式发布版本中,不配置,目前配置缺省路径 5 context-path: /
6 # 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
7 #address: 192.168.0.101
8 # 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知
9 # error:
10 # path: /error
11 # session最大超时时间(分钟),默认为30
12 #session:
13 # timeout: 60
14 # tomcat:
15 # tomcat的URI编码
16 #uri-encoding: utf-8
17 # tomcat最大线程数,默认为200
18 #max-threads: 1000
19 # 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)
20 #basedir: D:/springboot-tomcat-tmp
21 # 打开Tomcat的Access日志,并可以设置日志格式的方法:
22 #server.tomcat.access-log-enabled=true
23 #server.tomcat.access-log-pattern=
application.java启动类:
---- spring会扫描该类所在目录下的java类,SpringBoot唯一入口程序
1 package com.example.demo;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6 @SpringBootApplication
7 public class TestDemo1Application {
8 /*
9 Servlet容器默认的Context路径是/DispatherServlet匹配的路径(servlet-mapping中的url-patterns)
10 @ComponentScan路径被默认设置为SampleController的同名package,
11 也就是该package下的所有@Controller,@Service, @Component, @Repository都会被实例化后并加入Spring Context中。
12 */
13 public static void main(String[] args) {
14 SpringApplication.run(TestDemo1Application.class, args);
15 }
16 }
Controller类:
1 package com.example.demo.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.ResponseBody;
6 import org.springframework.web.bind.annotation.RestController;
7
8 /**
9 * Created by Administrator on 2017/7/19.
10 */
11 @Controller
12 public class ControllerDemo {
13 @RequestMapping("/index")
14 // @ResponseBody
15 public String getDemo(){
16 System.out.println(".....");
17 return "index";
18 }
19 }
访问localhost:8080/index 即可在页面中打印出 “index” 字样。
注意:在创建Controller类时,所有的类都得是默认包的子孙包。