我是靠谱客的博主 清脆绿茶,最近开发中收集的这篇文章主要介绍eclipse创建第一个springboot例子,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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

一 、eclipse安装springboot插件。

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

40946d0e1ff1af8aef03e6a6339af07be54.jpg

 

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

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

7e9d46d31aacd1c675710cc8962f942dd0b.jpg    

二、创建springboot项目

1.File-New-Spring Starter Project

b3d0f3b037dc2ba75b75e16c4d848f13cff.jpg

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

75a33ac9fa3bedb34f4ea82d86666373bb8.jpg

 

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

b114b30bc04d9d2b16255bc2356368cc086.jpg

4.创建完成,看一下目录结构。可以看到spring的启动类和另一个类ServletInitializer,他的作用是类似于web.xml的配置方式来启动spring上下文。

872f0eb01bd35cbe1fcd66ebf0a134bbbee.jpg

5.看一下java build path

c6439a356d1c5e52d5b8fe1bd7a73d17315.jpg

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

f3dd96294fc0dec9dc756fbc193aa44ba17.jpg

7.application.properties的内容看一下,若需要把项目部署到tomcat里并在浏览器访问,则需要设置server.servlet.context-path=/

#u8BBEu7F6ETomcatu7AEFu53E3uFF0Cu9ED8u8BA48080
server.port=8080
#u8BBEu7F6Eu9879u76EEContextPath
server.servlet.context-path=/
#u8BBEu7F6ETomcatu7F16u7801
server.tomcat.uri-encoding=UTF-8
#u8BBEu7F6Eu89C6u56FEu89E3u6790u5668u8DEFu5F84
spring.mvc.view.prefix=/WEB-INF/views/
#u8BBEu7F6Eu89C6u56FEu89E3u6790u5668u540Eu7F00
spring.mvc.view.suffix=.jsp
 
#u6570u636Eu5E93u914Du7F6E
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
 
#u914Du7F6E.xmlu6587u4EF6u8DEFu5F84
mybatis.mapper-locations=classpath:mapper/*.xml
#u914Du7F6Eu6A21u578Bu8DEFu5F84
#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-eclipsespringDemo-1targetclasses started by lenovo in E:workspace-eclipsespringDemo-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 FilesJavajdk1.8.0_151bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:ProgramDataOracleJavajavapath;C:Program Files (x86)Common FilesNetSarang;D:applenovoproduct11.2.0dbhome_1bin;C:Program Files (x86)LenovoFusionEngine;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)ATI TechnologiesATI.ACECore-Static;%TOMCAT_ HOME%bin;D:apache-ant-1.9.6bin;C:Program Filesmysql-5.6.24-win32bin;D:cygwinbin;E:workhadoop-2.6.0bin;M2_HOME;%HADOOP_HOME%bin;%HADOOP_HOME%lib;D:UserslenovoAnaconda3;D:UserslenovoAnaconda3Scripts;D:UserslenovoAnaconda3Librarybin;D:UserslenovoAnaconda3MinGWbin;D:UserslenovoAnaconda3MinGWx86_64-w64-mingw32lib;C:WINDOWSSystem32OpenSSH;C:UserslenovoAppDataLocalMicrosoftWindowsApps;E:phantomjs-2.1.1-windowsphantomjs-2.1.1-windowsbin;C:Program FilesJavajdk1.8.0_151lib;C:Program FilesJavajdk1.8.0_151libtools.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

381ca0d4e0881c51e379cb96723060d6d17.jpg

10.Goals 写package,勾选Skip Tests,点击Run

dfccb42c896b31d91a66bf6165826a1eb1e.jpg

11.将war包放入tomcat 的webapp下,启动tomcat.浏览器输入http://localhost:8080/demo/hello,其中demo是在pom.xml中设置的,设置好finalName后,导出的war包名称即为demo.war。访问路径:http://localhost:8080/war包名/hello

8e62649880ddd58ad39617da65a126a28a1.jpg

12.项目已上传到码云,访问地址:https://gitee.com/sunweiwest/springboot

 

 

 

 

 

转载于:https://my.oschina.net/u/2842177/blog/1934779

最后

以上就是清脆绿茶为你收集整理的eclipse创建第一个springboot例子的全部内容,希望文章能够帮你解决eclipse创建第一个springboot例子所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部