eclipse创建第一个springboot例子

原创
2018/08/27 18:03
阅读数 1.6W

1.环境:eclipse,jdk1.8,tomcat8

一 、eclipse安装springboot插件。

 在菜单栏中选择help-Eclipse marketplace ,在弹出窗口选择popular,找到Spring Tools,安装。

 

3.检查springboot插件是否成功安装。

Window-Show  View --other,如下,出现spring则代表安装成功。

    

二、创建springboot项目

1.File-New-Spring Starter Project

2.选择各项目,如果要导出war包,则Packaging选War,

 

3.next,选择springboot 版本,要引入的组件,我引入了mybatis,mysql,web。完成。

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

 

 

 

 

 

展开阅读全文
打赏
0
2 收藏
分享
加载中
更多评论
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部