开始学习神秘的springboot,通过helloworld学习,感觉并不难,相比springmvc,简化了很多配置,myeclipse对springboot有很好的支持。
首先,需要安装springboot的插件。目录为help->Intall from Catalog->propular
点击安装。安装完成后,重启Myeclipse
注:为了下载安装,使用阿里云maven镜像,具体操作见https://blog.csdn.net/cuipeng1019/article/details/80235063
新建项目,如图所示
完成后,即可看到新建的项目
修改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.springboot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot01</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
新建包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()+"</br>"
+"username:"+mysqlProperties.getUsername()+"</br>"
+"password:"+mysqlProperties.getPassword()+"</br>"
+"driver:"+mysqlProperties.getDriver()+"</br>";
}
}
新建包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文件