更改项目的端口号
server.port=8081
https://www.bootschool.net/ascii banner(横幅广告)
更改项目的端口号
server.port=8081
k=v
对空格的要求十分高
普通的key-value
person:
name: sunjina{random.int}
happy: false
birth: 2020/08/06
maps: {k1: sun,k2: jian}
hello: 小狗
lists:
- code
- music
- girl
dog:
name: ${person.hello:小皮}_哈哈
age: 2
@Component注解有什么作用----Springboot
为了妹子而奋斗 2018-04-24 20:53:49
分类专栏: Springboot学习与实战
用一句话概括:
被@Component注解标识的类,会被纳入Spring容器中统一管理,好处是什么?一句话概括:你不用自己new了!嘿嘿嘿
- @component (把普通pojo实例化到spring容器中,相当于配置文件中的
)
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
未使用yaml配置文件
1.直接在dog.java中赋值
2.读取配置文件中的值
使用yaml配置文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
使用properties配置文件怎么弄?
松散绑定:
JSR303校验
如果@email注解没用,就导入
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能超过120")
private int age;
@Email(message="邮箱格式错误")
private String email;
-----------------------------------------------------------上面常用,下面了解即可
空检查
@Null 验证对象是否为null
@NotNull 验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
Booelan检查
@AssertTrue 验证 Boolean 对象是否为 true
@AssertFalse 验证 Boolean 对象是否为 false
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内
@Length(min=, max=) string is between min and max included.
日期检查
@Past 验证 Date 和 Calendar 对象是否在当前时间之前
@Future 验证 Date 和 Calendar 对象是否在当前时间之后
@Pattern 验证 String 对象是否符合正则表达式的规则
.......等等
除此以外,我们还可以自定义一些数据校验规则
配置文件位置以及多环境配置
yaml中端口号优先级目录
在同级目录遇到多个配置文件.properties中都有端口号该如何选择呢,
在配置文件中添加 spring.profiles.active= test选择
那yaml中该怎么解决上面的问题呢?
很简单,只需要一个yaml文件,就不用创建多个propeties文件
spring:
profiles:
active: dev2
通过active选择自己所写的端口号
server:
port: 8081
spring:
profiles:
active: dev2
---
server:
port: 8082
spring:
profiles: dev
---
server:
port: 8083
spring:
profiles: dev2
Spring注解@Controller和的RestController的区别
@RestController
注解相当于控制器前加@Controller
,方法前加@ResponseBody
共同的作用效果。
如果第二段代码不加@ResponseBody
,返回的就是success.jsp
页面,加上之后返回的就是success
字符串;而第一段代码返回的也是success
,不再经过视图解析器解析了。
连续点击两次shift 弹出search everywhere窗口即可搜索到WebMvc
导入jquery jar包 https://www.webjars.org/
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
http://localhost:8080/webjars/jquery/3.5.1/jquery.js 查看
resources>static>public 包下放资源优先级
spring.mvc.static-path-pattern=/hello/,classpath:/kuang/ 在配置文件中加了这个,静态资源则访问不到
总结:
2.优先级
resources>static>public
spring.mvc.favicon.enabled=false 没有 不搞了
首页和图标定制
图标定制没成功,应该springboot的版本问题。算了,不搞了,反正也实用性不大
然后首页的话就很简单了,直接在resources文件夹下建立一个public文件夹,然后新建一个.html文件
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#using-boot-starter
| <dependency> | |
| ------------ | --------------------------------------------------- |
| | <groupId>org.thymeleaf</groupId> |
| | <artifactId>thymeleaf-spring5</artifactId> |
| | </dependency> |
| | <dependency> |
| | <groupId>org.thymeleaf.extras</groupId> |
| | <artifactId>thymeleaf-extras-java8time</artifactId> |
| | </dependency> |
我导入这个两个依赖还是找不到templates文件夹中的.html
怎么解决呢?
导入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
ctrl+i 快速提出方法
http://sc.chinaz.com/moban/200511241330.htm 模板查找
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
@Repository注解作用/?
dao层的注解,表示数据访问,对应存储层的bean,可以将标注层中类抛出的数据访问异常封装为 Spring 的数据访问异常类型。
@Component, @Repository, @Service的三个spring注解的区别
https://blog.csdn.net/RAVEEE/article/details/89879698
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
springMVC的注解@RequestParam与@PathVariable的区别
薛宇Chelly 2017-09-29 21:54:45
收藏 1
分类专栏: Java
版权
<article class="baidu_pl" style="box-sizing: inherit; outline: 0px; margin: 0px; padding: 16px 0px 0px; display: block; position: relative; color: rgba(0, 0, 0, 0.75); font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
1、在SpringMVC后台控制层获取参数的方式主要有两种,
一种是request.getParameter(“name”),另外一种是用注解@RequestParam直接获取。
这里主要讲这个注解 @RequestParam
接下来我们看一下@RequestParam注解主要有哪些参数:
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:
public List getItemTreeNode(@RequestParam(value=”id”,defaultValue=”0”)long parentId)
2、@PathVariable绑定URI模板变量值
@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。
//配置url和方法的一个关系
@RequestMapping(“item/{itemId}”)
/* @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的.action请求-
- @responsebody表示该方法的返回结果直接写入HTTP response body中
一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
比如异步获取json数据,加上@responsebody后,会直接返回json数据。*
@Pathvariable注解绑定它传过来的值到方法的参数上
用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
*/
@ResponseBody
public TbItem getItemById(@PathVariable Long itemId){
</article>
谈谈 Spring IOC 的 @Primary、@Qualifier 注解用法和区别。
https://mp.weixin.qq.com/s?src=11×tamp=1597219007&ver=2517&signature=LZHkUeP54cxY0Z2O4z1iGeSZI8lM0JW4smMgr5GyxUyZu4MiB4vbU6oLI5DiqGLvQTQnC4eOx1StSjCWPL8OafhTCZC6d3YxkHboW45z04oNGm4pLQs*ekrEJu&new=1
解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
https://blog.csdn.net/sundacheng1989/article/details/81630370?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-1&spm=1001.2101.3001.4242