一.springboot 常用地址
翻译工具:https://translate.google.cn/
springbootGithub的地址:https://github.com/spring-projects/spring-boot
springboot的官方文档:https://spring.io/guides/gs/spring-boot/
二.springboot 核心jar
spring-boot-starter-web
所谓的 springBoot 启动器其实就是一些 jar 包的集合。SprigBoot 一共提供 44 启动器。
4.1 spring-boot-starter-web
支持全栈式的 web 开发,包括了 tomcat 和 springMVC 等 jar
4.2 spring-boot-starter-jdbc
支持 spring 以 jdbc 方式操作数据库的 jar 包的集合
4.3 spring-boot-starter-redis
支持 redis 键值存储的数据库操作
三.编写一个helloworld的程序
1.新建一个maven工程:springboot-helloworld
2.配置pom文件
<?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.ljf.springboot.helloworld</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springboot-helloworld</name>
<!-- 引入springboot 的父类-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring boot的启动类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
</build>
</project>
3.编写controller:
package com.ljf.springboot.helloworld.controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("/api")
@ResponseBody
public String helloWorld(){
return "hello world! made by ljf!";
}
}
4.编写启动类:
package com.ljf.springboot.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class);
System.out.println( "Hello World!" );
}
}
5.启动,通过网页访问
可以看到端口号使用的是tomcat默认端口号:8080
启动器存放的位置。启动器可以和 controller 位于同一个包下,或者位于 controller 的上一级
包中,但是不能放到 controller 的平级以及子包下。