建筑工程网站免费,网站开发的岗位与分工,网站访问量排行榜,尚硅谷培训机构官网文章目录 一、引言1.1 初始化配置1.2 整合第三方框架1.3 后期维护1.4 部署工程1.5 敏捷式开发 二、SpringBoot介绍三、SpringBoot快速入门【重点】3.1 快速构建SpringBoot3.1.1 选择构建项目的类型3.1.2 项目的描述3.1.3 指定SpringBoot版本和需要的依赖3.1.4 导入依赖3.1.5 编… 文章目录 一、引言1.1 初始化配置1.2 整合第三方框架1.3 后期维护1.4 部署工程1.5 敏捷式开发 二、SpringBoot介绍三、SpringBoot快速入门【重点】3.1 快速构建SpringBoot3.1.1 选择构建项目的类型3.1.2 项目的描述3.1.3 指定SpringBoot版本和需要的依赖3.1.4 导入依赖3.1.5 编写了Controller3.1.6 测试 3.2 SpringBoot的目录结构3.2.1 pom.xml文件3.2.2 .gitignore文件3.2.3 src目录 3.3 SpringBoot三种启动方式3.3.1 运行启动类的main方法3.3.2 maven命令3.3.3 采用jar包的方式运行 四、SpringBoot常用注解【重点】4.1 Configuration和Bean4.2 SpringBootApplication 五、SpringBoot常用配置【重点】5.1 SpringBoot的配置文件格式5.2 多环境配置5.3 引入外部配置文件信息5.4 热加载5.4.1 导入依赖5.4.2 settings配置5.4.3 重新构建工程 六、SpringBoot整合Mybatis【重点】6.1 xml方式整合Mybatis6.1.1 导入依赖。6.1.2 编写配置文件6.1.3 准备Mybatis6.1.4 测试。 6.2 注解方式整合Mybatis6.2.1 创建District的Mapper接口6.2.2 添加Mybatis注解6.2.3 添加配置6.2.4 测试查看日志 6.3 SpringBoot整合分页助手6.3.1 导入依赖6.3.2 测试使用 七、SpringBoot整合JSP7.1 需要导入依赖7.2 创建JSP页面7.3 创建Contorller7.4 配置前缀和后缀 八、SpringBoot练习九 静态资源十 ControllerAdvice十一、异常处理十二、跨域的三种解决方案十三、过滤器的三种配置方式十四、构建 RESTful 一、引言 1.1 初始化配置 为了使用SSM框架去开发准备SSM框架的模板配置。 1.2 整合第三方框架 为了Spring整合第三方框架单独的去编写xml文件。 1.3 后期维护 后期SSM项目后期xml文件特别多维护xml文件的成本是很高的 1.4 部署工程 SSM工程部署也是很麻烦依赖第三方的容器 1.5 敏捷式开发 基于Java的SSM开发方式是很笨重而现在的pythonphpNodeJS的敏捷式开发已经盖过Java一头 二、SpringBoot介绍 SpringBoot是由Pivotal团队研发的SpringBoot并不是一门新技术只是将之前常用的SpringSpringMVCdata-jpa等常用的框架封装到了一起帮助你隐藏这些框架的整合细节实现敏捷开发。 SpringBoot就是一个工具集。 SpringBoot特点 SpringBoot项目不需要模板化的配置。SpringBoot中整合第三方框架时只需要导入相应的starter依赖包就自动整合了。SpringBoot默认只有一个.properties的配置文件不推荐使用xml后期会采用.java的文件去编写配置信息。SpringBoot工程在部署时采用的是jar包的方式内部自动依赖Tomcat容器提供了多环境的配置。后期要学习的微服务框架SpringCloud需要建立在SpringBoot的基础上。 三、SpringBoot快速入门【重点】 3.1 快速构建SpringBoot
3.1.1 选择构建项目的类型
选择构建项目的类型
3.1.2 项目的描述
项目的描述
3.1.3 指定SpringBoot版本和需要的依赖
指定SpringBoot版本和需要的依赖
3.1.4 导入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId
/dependency
!-- 将上述内容修改为下面的效果 --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency3.1.5 编写了Controller
RestController
public class TestController {GetMapping(/test)public String test(){return Hello SpringBoot!;}}3.1.6 测试
效果
3.2 SpringBoot的目录结构
3.2.1 pom.xml文件 指定了一个父工程 指定当前工程为SpringBoot帮助我们声明了starter依赖的版本。项目的元数据包名项目名版本号。指定了properties信息指定了java的版本为1.8导入依赖默认情况导入spring-boot-starterspring-boot-starter-test插件spring-boot-maven-plugin 3.2.2 .gitignore文件 默认帮我们忽略了一些文件和目录避免提交到Git仓库中 3.2.3 src目录
-src-main -java-包名启动类.java # 需要将controller类放在启动类的子包中或者同级包下-resources-static # 存放静态资源的-templates # 存储模板页面的application.properties # SpringBoot提供的唯一的配置文件-test # 只是为了测试用的3.3 SpringBoot三种启动方式
3.3.1 运行启动类的main方法 运行main方法即可 3.3.2 maven命令
mvn spring-boot:run3.3.3 采用jar包的方式运行 将当前项目打包成一个jar文件并通过java -jar jar文件 四、SpringBoot常用注解【重点】 4.1 Configuration和Bean 之前使用SSM去开发时在xml文件中编写bean标签但是SpringBoot不推荐使用xml文件。 Configuration注解相当于beans标签 Bean注解相当于bean标签 id“方法名 | 注解中的name属性优先级更高” class“方法的返回结果” Configuration // 代表当前类是一个配置类
public class UserConfig {Bean(name user1) // 构建一个实例放到spring容器中public User user(){User user new User();user.setId(1);user.setName(张三);return user;}/*beans .... Configurationbean iduser1 classcom.qf.firstspringboot.entity.User //beans*/
}4.2 SpringBootApplication SpringBootApplication就是一个组合注解 SpringBootConfiguration就是Configuration注解代表启动类就是一个配置类。EnableAutoConfiguration帮你实现自动装配的SpringBoot工程启动时运行一个SpringFactoriesLoader的类加载META-INF/spring.factories配置类已经开启的通过SpringFactoriesLoader中的load方法以for循环的方式一个一个加载。 好处无需编写大量的整合配置信息只需要按照SpringBoot提供好了约定去整合即可。坏处如果说你导入了一个starter依赖那么你就需要填写他必要的配置信息。手动关闭自动装配指定内容SpringBootApplication(exclude QuartzAutoConfiguration.class) ComponentScan就相当于context:component-scan basePackage“包名” /帮助扫描注解的。 五、SpringBoot常用配置【重点】 5.1 SpringBoot的配置文件格式 SpringBoot的配置文件支持properties和yml甚至他还支持json。 更推荐使用yml文件格式 yml文件会根据换行和缩进帮助咱们管理配置文件所在位置 yml文件相比properties更轻量级一些 yml文件的劣势 严格遵循换行和缩进 在填写value时一定要在: 后面跟上空格 类型安全的属性注入。
传统 Spring 中的属性注入有两种
xml
context:property-placeholder locationclasspath:userinfo.properties/2.java
Component
//PropertySource 注解的作用等价于 context:property-placeholder location/
PropertySource(classpath:book.properties)
public class Book {Value(${book.name})private String name;Value(${book.author})private String author;两种方式都是通过 Value 注解将值注入到具体的属性上。
这种方式有一个缺陷属性名千万不能写错。要是属性很多就容易写错。
Spring Boot 中推出了类型安全的属性注入这种方式可以自动识别属性名称然后自动注入。
容器配置
5.2 多环境配置 在application.yml文件中添加一个配置项 spring:profiles:active: 环境名在resource目录下创建多个application-环境名.yml文件即可 在部署工程时通过 java -jar jar文件 --spring.profiles.active环境 5.3 引入外部配置文件信息 和传统的SSM方式一样通过Value的注解去获取properties/yml文件中的内容。 如果在yml文件中需要编写大量的自定义配置并且具有统一的前缀时采用如下方式 // Java程序
ConfigurationProperties(prefix aliyun)
Component
Data
public class AliyunProperties {private String xxxx;private ... ...;
}// 配置文件
aliyun:xxxx: xxxxxxxxx...5.4 热加载
5.4.1 导入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdoptionaltrue/optional
/dependency5.4.2 settings配置
修改settings中的配置
5.4.3 重新构建工程
build
六、SpringBoot整合Mybatis【重点】 6.1 xml方式整合Mybatis xml方式在编写复杂SQL时更适合 6.1.1 导入依赖。
!-- mysql驱动--
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId
/dependency!-- druid连接--
dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.1.10/version
/dependency!-- mybatis--
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.3.2/version
/dependency6.1.2 编写配置文件
// 准备实体类
Data
public class Air implements Serializable {private Integer id;private Integer districtId;private java.util.Date monitorTime;private Integer pm10;private Integer pm25;private String monitoringStation;private java.util.Date lastModifyTime;}
//
Data
public class District implements Serializable {private Integer id;private String name;}6.1.3 准备Mybatis
// 1. 接口
public interface AirMapper {ListAir findAll();}// 2. 在启动类中添加直接扫描Mapper接口所在的包
MapperScan(basePackages com.qf.firstspringboot.mapper)// 3. 准备映射文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.qf.firstspringboot.mapper.AirMapper!-- ListAir findAll();--select idfindAll resultTypeAirselect * from air/select/mapper//4. yml文件
!-- 添加yml文件配置信息 --
# mybatis配置
mybatis:# 扫描映射文件mapper-locations: classpath:mapper/*.xml# 配置别名扫描的包type-aliases-package: com.qf.firstspringboot.entityconfiguration:# 开启驼峰映射配置map-underscore-to-camel-case: true
# 连接数据库的信息
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///air?serverTimezoneUTCusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource 6.1.4 测试。
class AirMapperTest extends FirstSpringbootApplicationTests {Autowiredprivate AirMapper airMapper;Testvoid findAll() {ListAir list airMapper.findAll();for (Air air : list) {System.out.println(air);}}
}6.2 注解方式整合Mybatis 注解方式在编写配置简单简单SQL推荐使用 6.2.1 创建District的Mapper接口
public interface DistrictMapper {ListDistrict findAll();
}6.2.2 添加Mybatis注解 针对增删改查InsertDeleteUpdateSelect 还是需要在启动类中添加MapperScan注解 Select(select * from district)
ListDistrict findAll();Select(select * from district where id #{id})
District findOneById(Param(id) Integer id);6.2.3 添加配置
// yml文件
logging:level:com.qf.firstspringboot.mapper: DEBUG6.2.4 测试查看日志
class DistrictMapperTest extends FirstSpringbootApplicationTests {Autowiredprivate DistrictMapper mapper;Testvoid findAll() {ListDistrict list mapper.findAll();for (District district : list) {System.out.println(district);}}Testvoid findOneById() {District district mapper.findOneById(5);System.out.println(district);}
}6.3 SpringBoot整合分页助手
6.3.1 导入依赖
!-- pageHelper依赖--
dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper-spring-boot-starter/artifactIdversion1.2.5/version
/dependency6.3.2 测试使用
Test
public void findByPage(){//1. 执行分页PageHelper.startPage(1,5);//2. 执行查询ListAir list airMapper.findAll();//3. 封装PageInfo对象PageInfoAir pageInfo new PageInfo(list);//4. 输出for (Air air : pageInfo.getList()) {System.out.println(air);}
}七、SpringBoot整合JSP jsp、freemarker、thymeleaf
7.1 需要导入依赖
!-- JSP核心引擎依赖--
dependencygroupIdorg.apache.tomcat.embed/groupIdartifactIdtomcat-embed-jasper/artifactId
/dependency
!-- JSTL--
dependencygroupIdjavax.servlet/groupIdartifactIdjstl/artifactId
/dependency7.2 创建JSP页面
创建webapp以及WEB-INF去存放JSP页面
7.3 创建Contorller
// Controller
Controller
public class JspController {GetMapping(/index)public String index(Model model){model.addAttribute(name,张三);return index;}
}7.4 配置前缀和后缀
spring:mvc:# 视图的前缀和后缀view:prefix: /WEB-INF/suffix: .jsp八、SpringBoot练习 页面查询客户信息从ES中查询 完成客户模块的增删改并且同步到ES中。 练习业务图
九 静态资源
Spring Boot 中默认提供了五个静态资源存储路径
classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public//webapp
五个位置优先级依次降低。
如果需要自定义静态资源位置有两种方式 application.properties 中进行配置 spring.mvc.static-path-pattern/static/**
spring.web.resources.static-locationsclasspath:static/写代码配置 Configuration
public class WebMvcConfig implements WebMvcConfigurer {Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/static/**).addResourceLocations(classpath:static/);}
}十 ControllerAdvice
三种用法
全局异常处理定义全局数据请求参数预处理
十一、异常处理
整体来说两种方式
静态页面展示异常 明确展示推荐模糊展示 动态页面展示异常 明确展示模糊展示推荐
具体查找方式
先找明确的再找模糊的先找动态的再找静态的
十二、跨域的三种解决方案
CrossOrigin 注解全局配置配置过滤器
十三、过滤器的三种配置方式
通过 Component 注解注入到 Spring 容器中。这种方式有一个缺陷无法配置拦截地址。但是这种方式可以通过 Order 注解配置优先级。使用 WebFilterServletComponentScan 注解的方式这种方式可以配置拦截路径但是无法配置优先级。FilterRegistrationBean这种方式既可以配置拦截路径也可以配置优先级。
十四、构建 RESTful
Spring Boot 中提供了一个快速构建 RESTful 服务的工具就是
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-rest/artifactId
/dependency该工具可以配合 Jpa、MongoDB 以及 ElasticSearch 一起使用。
以 Jpa 为例使用时只需要三步
在 application.properties 文件中配置数据库和 JPA的基本信息。提供一个实体类。提供一个空接口。
然后启动项目系统会自动生成如下接口。
GET http://localhost:8080/users 这是一个分页查询接口默认查询第一页每页20条数据。users 是实体类名首字母小写后面加上 s。可以自行添加分页参数http://localhost:8080/users?size3page0GET http://localhost:8080/users/1这个是根据id查询数据的接口POST http://localhost:8080/users这个是添加数据的接口添加的参数形式是 JSON。PUT http://localhost:8080/users/6这个是根据 id 修改数据的接口参数的提交方式也是 JSON。DELETE http://localhost:8080/users/6这个是根据 id 删除数据。 定制请求路径。 exported是否暴露当前接口。collectionResourceRel生成的数据集合的名字默认是 users。itemResourceRel生成的每一项数据的名字默认是 user。 RepositoryRestResource(exported true,path us,collectionResourceRel us,itemResourceRel u)
public interface UserDao extends JpaRepositoryUser, Long {
}定制请求方法。 RepositoryRestResource(exported true,path us,collectionResourceRel us,itemResourceRel u)
public interface UserDao extends JpaRepositoryUser, Long {ListUser findUserByUsernameStartingWith(Param(username) String username);
}此时可以通过如下地址查看所有的查询接口。http://localhost:8080/us/search。 可以看到自定义的接口调用方式如下http://localhost:8080/us/search/findUserByUsernameStartingWith?username王 还可以定制方法名 RepositoryRestResource(exported true,path us,collectionResourceRel us,itemResourceRel u)
public interface UserDao extends JpaRepositoryUser, Long {RestResource(path byname)ListUser findUserByUsernameStartingWith(Param(username) String username);
}此时的查询路径http://localhost:8080/us/search/byname?username王 其他配置 # 配置统一前缀
spring.data.rest.base-path/api
# 配置默认的页数
spring.data.rest.default-page-size0
# 每页查询的记录数
spring.data.rest.max-page-size20
# 分页参数的 key
spring.data.rest.page-param-namepage
# 分页参数 size 的key
spring.data.rest.limit-param-namesize
# 排序的参数的 key
spring.data.web.sort.sort-parametersort
# 创建成功时是否返回数据
spring.data.rest.return-body-on-createtrue
# 更新成功时是否返回数据
spring.data.rest.return-body-on-updatetrue页面模板技术
JspFreemarkerSpringBoot2.2之前 .ftl之后是 .ftlhThymeleaf
JPAJava Persistence API
Hibernate/OpenLink/EclipseLink。。。
JPA