SpringBoot概述
随着技术的发展,Java EE使用 Spring 逐渐变得笨重起来,大量的 XML 文件存在于项目之中。繁琐的配置,整合第三方框架的配置问题,导致了开发和部署效率的降低。
Spring Boot 是伴随着 Spring 4.0 诞生的,从字面理解,Boot是引导的意思,因此 Spring Boot 旨在帮助开发者快速搭建 Spring 框架。Spring Boot 继承了原有 Spring 框架的优秀基因,使 Spring 在使用中更加方便快捷。
SpringBoot简介
Spring 、Spring MVC和Spring Boot、
Spring Boot核心特点
新建Spring Boot项目演示
Spring官网 start.spring.io
- 访问 http://start.spring.io/。
- 在页面上输入相应的 Spring Boot 版本、Group 和 Artifact 信息以及项目依赖,然后创建项目。
IDEA集成的Spring Initializr
IDEA 中可以通过File->New->Project来快速构建 Spring Boot 工程。如下,选择 Spring Initializr,在 Project SDK 中选择刚刚我们导入的 jdk,点击 Next,到了项目的配置信息。
Group:填企业域名,
Artifact:填项目名称,
Dependencies:可以添加我们项目中所需要的依赖信息,根据实际情况来添加,本课程只需要选择 Web 即可。
Spring Boot版本介绍
GA:General Available:通用可使用,官方推荐使用此版本
PRE:预览版,内测版
SNAPSHOT
RC:Release
稳定性(由小到大):
PRE<SNAPSHOT<RC<GA
配置文件简介
yml:分层级,冒号后需要空格
server:
#更改端口号
port: 8081
servlet:
#公共前缀
context-path: /first
#配置自定义属性
school:
grade: 3
classnum: 6
# 配置数据库地址
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/springbootlearn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
配置自定义属性案例
配置类
package com.imooc.springbootleam;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* School配置类
*/
// @Component (把普通pojo实例化到spring容器中,相当于配置文件中的 )
@Component
// @ConfigurationProperties为Springboot中的一个注解,用于绑定实体类与配置文件,用来把properties或者yml配置文件转化为bean来使用的。
@ConfigurationProperties(prefix = "school")
public class SchoolConfig {
Integer grade;
Integer classnum;
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
public Integer getClassnum() {
return classnum;
}
public void setClassnum(Integer classnum) {
this.classnum = classnum;
}
}
读取配置类
package com.imooc.springbootleam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 读取配置类
*/
@RestController
public class ConfigController {
@Autowired
SchoolConfig schoolConfig;
@GetMapping("/gradefromconfig")
public String gradeClass(){
return schoolConfig.grade+" : "+schoolConfig.classnum;
}
}
Web项目的三层结构
Domain层
package com.imooc.springbootleam;
public class Student {
Integer id;
String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Mapper层
package com.imooc.springbootleam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/**
* mapper接口,操作db
*/
@Mapper
@Repository
public interface StudentMapper {
@Select("select * from student where id = #{id}")
Student findByID(Integer id);
}
Service层
package com.imooc.springbootleam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* service层
*/
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
public Student findStudent(Integer id){
return studentMapper.findByID(id);
}
}
Controller层
package com.imooc.springbootleam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 学生类controller层
*/
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@GetMapping("/student")
public String student(@RequestParam Integer num){
Student student = studentService.findStudent(num);
return student.toString();
}
}