當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot学习:在Interillj Idea上快速搭建SpringBoot项目
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot学习:在Interillj Idea上快速搭建SpringBoot项目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、創建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: 80804 #項目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類時,所有的類都得是默認包的子孫包。
?
轉載于:https://www.cnblogs.com/tongxuping/p/7206875.html
總結
以上是生活随笔為你收集整理的SpringBoot学习:在Interillj Idea上快速搭建SpringBoot项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云ECS服务器磁盘空间异常,或者爆满
- 下一篇: SpringMVC札集(05)——Spr