SpringBoot学习一:环境及HelloWorld

开始学习神秘的springboot,通过helloworld学习,感觉并不难,相比springmvc,简化了很多配置,myeclipse对springboot有很好的支持。

SpringBoot学习一:环境及HelloWorld_第1张图片

首先,需要安装springboot的插件。目录为help->Intall from Catalog->propular

SpringBoot学习一:环境及HelloWorld_第2张图片


点击安装。安装完成后,重启Myeclipse

注:为了下载安装,使用阿里云maven镜像,具体操作见https://blog.csdn.net/cuipeng1019/article/details/80235063


新建项目,如图所示

SpringBoot学习一:环境及HelloWorld_第3张图片


SpringBoot学习一:环境及HelloWorld_第4张图片


完成后,即可看到新建的项目

SpringBoot学习一:环境及HelloWorld_第5张图片

修改pom.xml配置



	4.0.0

	com.springboot
	springboot
	0.0.1-SNAPSHOT
	war

	springboot01
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.6.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



新建包controller包,新建类HelloController

package com.sm.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sm.entity.MysqlProperties;



@RestController
public class HelloController {

	@Value("${hello}")
	private String value;
	
	@Resource
	private MysqlProperties mysqlProperties;
	
	@RequestMapping("hello")
	public String say() {
		return value;
	}
	@RequestMapping("showJdbc")
	public String showJdbc() {
		return "url:"+mysqlProperties.getUrl()+"
" +"username:"+mysqlProperties.getUsername()+"
" +"password:"+mysqlProperties.getPassword()+"
" +"driver:"+mysqlProperties.getDriver()+"
"; } }

新建包com.sm.entity,新建类MysqlProperties


package com.sm.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="jdbc")
public class MysqlProperties {

	
	
	private String url;
	
	private String username;
	
	private String password;
	
	private String driver;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}
	
	
}

配置applacation.properties

server.port=8081
server.context-path=/springboot01

hello=\u6211\u7231\u4F60

jdbc.url=jdbc://mysql:localhost11
jdbc.username=root
jdbc.password=sa
jdbc.driver=com.mysql.driver

项目搭建完成,分两个知识点,一是配置applacation.properties,二是取applacation.properties的两种方式取值


运行Springboot01Application.java文件

SpringBoot学习一:环境及HelloWorld_第6张图片



你可能感兴趣的:(springboot,SpringBoot入门教程)