SpringBoot学习总结

SpringBoot

区别

Springboot是springMVC的升级版,两者没有必然联系,可直接学习springBoot,springBoot是伴随着Spring4.0诞生的

SpringBoot的特点 1.化繁为简,简化配置 2.备受关注,是下一代框架

3.微服务的入门级框架(微服务是当下比较流行的架构spring为微服务架构提供了一整套组件,统称为SrpingCloud,,springcloud是建立在springBoot的基础之上的) 4.使编码变得简单,使配置变得简单,使部署变得简单,使监控变得简单

程序配置

引入jar包 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat provided org.apache.tomcat.embed

tomcat-embed-jasper provided 启动类上班必须有@SpringBootApplication注解,controller类要加@RestController注解,方法上配置@RequestMaping注解 @SpringBootApplication public class Application{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } 注解与模板

讲解:

@Controller注解:处理http请求

@RestController注解:Spring4之后新加的注解,等同于返回json注解的@ResponseBody+controller注解的组合注解 @RequestMapping注解:配置url配置 @PathVariable 获取url中的数据 @RequestParam 获取请求参数的值

如果仅仅使用@Controller,返回的字符串就是页面的名称,需要加入srping官方的模板引擎,在pom.xml中配置, org.springframework.boot spring-boot-starter-thymeleaf

属性配置

application.properties为springboot的默认配置文件,可配置端口server.port=8081可配置项目路径server.context-path=/gril ,访问路径时的路径就变成localhost:8081/gril/controller名称 application.yml文件是springboot的另一种配置文件,这种文件可以减少重复值(推荐,注意:冒号后要加空格) server: port: 8080 context-path: /gril

在yml文件中自定义属性时,在controller中使用@Value(“${gril}”) yml: gril:liuyandeng controller: @Value(“${gril}”) private String gril;(配置文件中部分类型,如果是数字类型可以写成private integer lyd;)

属性映射到bean

gril: name: liuyandeng age: 25 创建Gril.java的java文件,类上加@Component和@ConfigurationProperties(prefix=\创建private String name;和private Integer age;属性 创建get和set方法,获取配置文件前缀是gril的配置映射到javabean属性中,

在controller中注入这个bean @Autowired Girl gril ;

如果有多个配置文件比如application.yml,application-A.yml和application-B.yml,如果想使用B配置文件,可在application.yml文件中配置 spring: profiles: active: B

数据库操作

server: port: 8080 spring: thymeleaf: prefix: /WEB-INF/views/ suffix: .html mode: HTML5 encoding: UTF-8 content-type: text/html cache: false datasource: name: test

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4