数据库:MySQL
IDE:Eclipse Mars2
Spring-Boot:1.4.4
Maven: 3.3.3 (官方声明Springboot 1.4.4版本需要Maven 3.2+)
在pom.xml中添加依赖,效果如下
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.4.RELEASEversion>
parent>
<groupId>com.xxx.springbootgroupId>
<artifactId>xxx-springbootartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
因为这些jar 包被我们刚才加入的spring-boot-starter-web 所引用了,所以添加spring-boot-starter-web后会自动把依赖传递过来。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
publicclass Demo {
publicstaticvoid main(String[] args) {
SpringApplication.run(Demo.class, args);
}
}
需求:使用Spring MVC实现Hello World输出
spring加入配置
<context:component-scanbase-package="com.xxx.springboot.controller"/>
<mvc:annotation-driven/>
web.xml加入配置
<servlet>
<servlet-name>xxx-springbootservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/*.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>xxx-springbootservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
还要编写Controller。。。
我们不需要配置文件,直接编写Controller类即可
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
publicclass HelloWorldController {
@RequestMapping("info")
publicString info() {
return"Hello world!";
}
}
@RestController注解:其实就是@Controller和@ResponseBody注解加在一起
启动方式一:启动之前编写的引导类即可
启动方式二:使用Maven命令spring-boot:run执行即可
选择 Maven Build
在浏览器地址栏输入http://localhost:8080/info 即可看到运行结果
本文来源:https://www.2haoxitong.net/k/doc/bf6e099c1b37f111f18583d049649b6648d70976.html
文档为doc格式