eclipse建立第一個springboot例子

1.環境:eclipse,jdk1.8,tomcat8html

一 、eclipse安裝springboot插件。java

 在菜單欄中選擇help-Eclipse marketplace ,在彈出窗口選擇popular,找到Spring Tools,安裝。mysql

 

3.檢查springboot插件是否成功安裝。git

Window-Show  View --other,以下,出現spring則表明安裝成功。web

    

2、建立springboot項目spring

1.File-New-Spring Starter Projectsql

2.選擇各項目,若是要導出war包,則Packaging選War,apache

 

3.next,選擇springboot 版本,要引入的組件,我引入了mybatis,mysql,web。完成。windows

4.建立完成,看一下目錄結構。能夠看到spring的啓動類和另外一個類ServletInitializer,他的做用是相似於web.xml的配置方式來啓動spring上下文。瀏覽器

5.看一下java build path

6.在目錄中加上mybaits的mapper映射

7.application.properties的內容看一下,若須要把項目部署到tomcat裏並在瀏覽器訪問,則須要設置server.servlet.context-path=/

#\u8BBE\u7F6ETomcat\u7AEF\u53E3\uFF0C\u9ED8\u8BA48080
server.port=8080
#\u8BBE\u7F6E\u9879\u76EEContextPath
server.servlet.context-path=/
#\u8BBE\u7F6ETomcat\u7F16\u7801
server.tomcat.uri-encoding=UTF-8
#\u8BBE\u7F6E\u89C6\u56FE\u89E3\u6790\u5668\u8DEF\u5F84
spring.mvc.view.prefix=/WEB-INF/views/
#\u8BBE\u7F6E\u89C6\u56FE\u89E3\u6790\u5668\u540E\u7F00
spring.mvc.view.suffix=.jsp
 
#\u6570\u636E\u5E93\u914D\u7F6E
spring.datasource.url=jdbc:mysql://localhost:3306/85paipai?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 
#\u914D\u7F6E.xml\u6587\u4EF6\u8DEF\u5F84
mybatis.mapper-locations=classpath:mapper/*.xml
#\u914D\u7F6E\u6A21\u578B\u8DEF\u5F84
#mybatis.type-aliases-package=com.imgServer.domain

8.看一下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.example</groupId>
	<artifactId>springDemo-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>springDemo-1</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
	<finalName>demo</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

9.啓動springboot

運行SpringDemo1Application.java 爲main函數。運行成功,控制檯輸出以下信息

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2018-08-27 17:37:56.021  INFO 30844 --- [           main] com.example.SpringDemo1Application       : Starting SpringDemo1Application on LENOVO-G50 with PID 30844 (E:\workspace-eclipse\springDemo-1\target\classes started by lenovo in E:\workspace-eclipse\springDemo-1)
2018-08-27 17:37:56.025  INFO 30844 --- [           main] com.example.SpringDemo1Application       : No active profile set, falling back to default profiles: default
2018-08-27 17:37:56.168  INFO 30844 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@17c386de: startup date [Mon Aug 27 17:37:56 CST 2018]; root of context hierarchy
2018-08-27 17:37:59.161  INFO 30844 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-08-27 17:37:59.224  INFO 30844 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-08-27 17:37:59.225  INFO 30844 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-08-27 17:37:59.254  INFO 30844 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_151\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\NetSarang;D:\app\lenovo\product\11.2.0\dbhome_1\bin;C:\Program Files (x86)\Lenovo\FusionEngine;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;%TOMCAT_ HOME%\bin;D:\apache-ant-1.9.6\bin;C:\Program Files\mysql-5.6.24-win32\bin;D:\cygwin\bin;E:\work\hadoop-2.6.0\bin;M2_HOME;%HADOOP_HOME%\bin;%HADOOP_HOME%\lib;D:\Users\lenovo\Anaconda3;D:\Users\lenovo\Anaconda3\Scripts;D:\Users\lenovo\Anaconda3\Library\bin;D:\Users\lenovo\Anaconda3\MinGW\bin;D:\Users\lenovo\Anaconda3\MinGW\x86_64-w64-mingw32\lib;C:\WINDOWS\System32\OpenSSH\;C:\Users\lenovo\AppData\Local\Microsoft\WindowsApps;E:\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin;C:\Program Files\Java\jdk1.8.0_151\lib;C:\Program Files\Java\jdk1.8.0_151\lib\tools.jar;;.]
2018-08-27 17:37:59.664  INFO 30844 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-08-27 17:37:59.665  INFO 30844 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3501 ms
2018-08-27 17:37:59.815  INFO 30844 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-08-27 17:37:59.824  INFO 30844 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-08-27 17:37:59.825  INFO 30844 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-08-27 17:37:59.825  INFO 30844 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-08-27 17:37:59.825  INFO 30844 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-08-27 17:38:00.562  INFO 30844 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-27 17:38:00.890  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@17c386de: startup date [Mon Aug 27 17:37:56 CST 2018]; root of context hierarchy
2018-08-27 17:38:01.007  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.example.controller.HelloWorldController.index()
2018-08-27 17:38:01.008  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello1]}" onto public java.lang.String com.example.controller.ImgServerController.index()
2018-08-27 17:38:01.009  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/uploadImg],methods=[POST]}" onto public com.example.util.ResultData com.example.controller.ImgServerController.searchImgByImg(org.springframework.web.multipart.MultipartFile,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException
2018-08-27 17:38:01.009  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/searchImg]}" onto public com.example.util.ResultData com.example.controller.ImgServerController.searchImg(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException
2018-08-27 17:38:01.013  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-08-27 17:38:01.014  INFO 30844 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-08-27 17:38:01.063  INFO 30844 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-27 17:38:01.063  INFO 30844 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-27 17:38:01.585  INFO 30844 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-27 17:38:01.586  INFO 30844 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-08-27 17:38:01.594  INFO 30844 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-08-27 17:38:01.741  INFO 30844 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-08-27 17:38:01.746  INFO 30844 --- [           main] com.example.SpringDemo1Application       : Started SpringDemo1Application in 6.251 seconds (JVM running for 6.824)

10.導出war包,

導出war包以前,項目右鍵Run 選擇Maven-clean, 而後項目右鍵Maven -updata project ,當項目更新完以後,build path中的jdk會換成eclipse自帶的javaSe-1.8,把它換成本身安裝的jdk1.8,最後項目右鍵Run As ,選擇Maven build

10.Goals 寫package,勾選Skip Tests,點擊Run

11.將war包放入tomcat 的webapp下,啓動tomcat.瀏覽器輸入http://localhost:8080/demo/hello,其中demo是在pom.xml中設置的,設置好finalName後,導出的war包名稱即爲demo.war。訪問路徑:http://localhost:8080/war包名/hello

12.項目已上傳到碼雲,訪問地址:https://gitee.com/sunweiwest/springboot

相關文章
相關標籤/搜索