SpringBoot 精简笔记
0. Fundamental
a. @SpringBootApplication //启动类
b. pom.xml //Maven基本依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1. application.yml (application-dev.yml, application-prod.yml)
spring:
profiles:
active: prod //设置激活的profile名字 server:
port: 8080
content-path: /demo student:
name: Peter
age: 19
message: My name is ${student.name}, age is ${student.age} spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/demo
username: root
password: 123465
jpa:
hibernate:
dll-auto: update
show-sql: true
2. 注入设置值
a. 直接使用单个值:@Value("${student.name}")
b. 注入配置类:
@Component
@ConfigurationProperties(prefix = "student") //一系列配置属性的前缀
3. Controller
a. @Controller: 处理http请求,返回模板文件名 (旧方式)
b. @RestController:处理请求,返回json
@RequestMapping: 配置URL映射
@GetMapping, @PutMapping, @PostMapping, @DeleteMapping
@PathVariable //e.g. /hello/{id}
@RequsetParam //e.g. ?name=Peter
4. Spring-Data-Jpa
a. @Entity;
b. @Id (添加annotation @GeneratedValue 来产生自动增长的id值);
c. Interface class Repository (findOne,findAll,findByXXX,save);
d. @Service;
e. @Transactional;
5. Validation
a. @Min(value=18, message="Less than minimum value."), 最小值,添加到@Entity的property上。
b. @Valid, BindingResult bindingResult, 绑定验证结果到变量中,如果bindingResult.hasErrors()非空,则获取错误信息:bindingResult.getFieldError()。
6. AOP
a. 添加依赖 spring-boot-starter-aop;
b. @Aspect, @Component;
c. @Pointcut, @Before, @After;
d. 参数注入:JointPoint,@AfterReturning(returning = "object", pointcut = "log()");
7. 统一异常处理
a. @ControllerAdvice, @ExceptionHandler;
b. Self-defined exception, to extend RuntimeException;
c. Self-defined Enum, to list out all the exception cases;
8. 单元测试
a. @RunWith(SpringRunner.class), @SpringBootTest(classes = Application.class), @ActiveProfiles("test");
b. @AutoConfigureMockMvc =>
@Autowired
MockMvc mvc;
mvc.perform(MockMvcRequestBuilders.get("/api")).andExpect(MockMvcResultMatchers.status.isOk());
SpringBoot 精简笔记的更多相关文章
- SpringBoot学习笔记
SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...
- Springboot学习笔记(六)-配置化注入
前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...
- SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用
SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用 Spring Boot Admin是一个管理和监控Spring Boot应用程序的应用程序.本文参考文档: 官 ...
- JavaScript 精简笔记
JavaScript 精简笔记,摘自 廖雪峰的官方网站. [From] https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51 ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- SpringBoot学习笔记(2):引入Spring Security
SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...
- SpringBoot学习笔记(7):Druid使用心得
SpringBoot学习笔记(7):Druid使用心得 快速开始 添加依赖 <dependency> <groupId>com.alibaba</groupId> ...
- SpringBoot学习笔记(4):与前端交互的日期格式
SpringBoot学习笔记(4):与前端交互的日期格式 后端模型Date字段解析String 我们从前端传回来表单的数据,当涉及时间.日期等值时,后端的模型需将其转换为对应的Date类型等. 我们可 ...
- SpringBoot学习笔记(4):添加自定义的过滤器
SpringBoot:学习笔记(4)——添加自定义的过滤器 引入自定义过滤器 SpringBoot提供的前端控制器无法满足我们产品的需求时,我们需要添加自定义的过滤器. SpringBoot添加过滤器 ...
随机推荐
- 【Leetcode009】Palindrome Number
问题链接:https://leetcode.com/problems/palindrome-number/#/description Question:Determine whether an int ...
- PHP学习笔记之continue与break
百度中有人这样解释:break是结束整个循环体,continue是结束单个循环体.昨天看燕十八老师PHP视频,讲到break,continue时,举了一个例子,理解更容易.天龙八部中,西夏国公主选婿, ...
- 如何处理与开发有争议的Bug?
工作中,测试人员有时会遇到类似的问题:提交了一份软件缺陷报告,可由于某种原因,无论是开发人员还是开发经理就是不愿修改程序.应如何处理这类问题呢?我认为,当对报告出现分歧意见后,测试工程师应首先做如下 ...
- Claims Based Authentication and Token Based Authentication和WIF
基于声明的认证方式,其最大特性是可传递(一方面是由授信的Issuer,即claims持有方,发送到你的应用上,注意信任是单向的.例如QQ集成登录,登录成功后,QQ会向你的应用发送claims.另一方面 ...
- 什么是Condition Number(条件数)?
In the field of numerical analysis, the condition number of a function with respect to an argument m ...
- 关于hibernate的查询
为什么建议hibernate查询全部字段 一般而言,要查询什么字段就查询什么字段,不要select * from表,但是在hibernate,我们其实可以不遵循这个规则,建议我们把所有属性都查询出来( ...
- 【转】Android系统概览
这篇文章其实原文叫 <老罗的Android之旅>导读PPT 是罗升阳的博客,我觉得用“Android系统概览”作为标题更贴切些,对于在应用层已经开发了一段时间的人来说,读完之后会有很多体会 ...
- 「CF140C」 New Year Snowmen
题目链接 戳这 贪心+优先队列,只要每次将数量前三大的半径拿出来就好了,用优先队列维护一下 #include<bits/stdc++.h> #define rg register #def ...
- css清除浮动的8种方法以及优缺点
浮动会使当前标签产生上浮的效果,同时会影响到前后的标签.父级标签的位置及width height 属性.而且同样的代码,在各种浏览器中效果可能不同,这样让清除浮动更难了.清除浮动引起的问题有很多的方法 ...
- loj#6436. 「PKUSC2018」神仙的游戏(NTT)
题面 传送门 题解 一旦字符串踏上了通配符的不归路,它就永远脱离了温暖的字符串大家庭的怀抱 用人话说就是和通配符扯上关系的字符串就不是个正常的字符串了比如说这个 让我们仔细想想,如果一个长度为\(le ...