SpringBoot是为了简化Spring而生的框架
1.去除了大量的xml配置文件
2.简化复杂的依赖管理
3.配合各种starter使用,基本上可以做到自动化配置
4.快速启动容器
5. 嵌入式Tomcat,Jetty容器,无需部署WAR包,简化Maven或Gradle配置
入门:做简单的SpringBoot项目
先确定你的java的版本在1.8以上以及maven的版本在3.3.9以上
开启一个maven项目(jar项目需要执行main方法)
在pom文件下添加:由父pom指定版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
添加SpringMVC的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
建立一个java类
package cm.pjj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //这个注释包括了 @controller 以及@ResponseBody (作用是将方法的返回值直接返回给用户要是不加的话就是跳转路径)
/**
@SpringBootApplication注解,它包括三个注解
@Configuration:表示将该类作用springboot配置文件类
@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置
@ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类
*/
@SpringBootApplication
public class Test {
@GetMapping("/")//springboot 可以特用GetMapping PostMapping PutMapping DeleteMapping注解
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Test.class, args);//运行的main
}
}
这里注意:
一.@SpringBootApplication注解,它包括三个注解
1.@Configuration:表示将该类作用springboot配置文件类。
2.@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置。
3.@ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类。
二.SpringBootApplication只会扫描该类所在包下的所有包
内嵌了tomcat 运行main方法即可访问8080端口 不需要上下文路径
Springboot 整合 redis
首先引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
这里介绍一个springbook的资源配置文件application.properties 里面可以放置各种资源属性。属于预定大于配置
在Spring官网 中 有SpringBoot 整合redis的介绍 以及在application.properties的约定键;如图:
根据自己的需求写资源文件:
java类中简单测试:
package cm.pjj.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisTest {
@Autowired
StringRedisTemplate srt;//该方法继承了RedisTemplate,确定了Key/Value类型为String类型,并且实现了两个构造器。改用StringRedisSerializer来序列化数据库
@GetMapping("/r")
String tt() {
srt.boundValueOps("ioi").set("niubi");
return srt.boundValueOps("ioi").get();
}
}
Springboot 整合 mysql数据库
首先一样先引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application.properties中配置资源
java类代码:
package cm.pjj.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MysqlTest {
@Autowired
JdbcTemplate jt;//jdbc连接
@GetMapping("/my")
List<Map<String, Object>> ttt() {
List<Map<String, Object>> query = jt.query("Select * from student", new ColumnMapRowMapper());
return query;
}
}