SpringBoot学习

SpringBoot

javase:OOP

mysql:持久化

html+css+js+jquery9框架:视图层View

javaweb:原始mvc网站

war:tomcat 运行

ssm:框架,简化开发,配置复杂

再简化,springmvc–>springboot 微服务架构

springboot :内嵌tomcat

  • ​ 配置:yml

  • ​ 自动装配–>简化开发

  • ​ 继承数据库druid

  • ​ 分布式开发:Dubbo+zookeeper

  • ​ swagger:接口文档

  • ​ SpringSecurity / shiro(拦截器功能,方便)

  • ​ Linux

  • ​ SpringCloud :真正涉及到微服务,Restful,Eureka,Ribbon,Feign,HyStrix

    • SpringCloud:config : git 上远程操作

    JVM:常见内容知道就好

原理:

spring-boot-starter-web : 帮忙导入各种web所需依赖

用什么功能,找到对应启动器

spring-boot-autoconfigure-2.2.0.RELEASE.jar ,整合javaEE

Spring-Boot-DevTools 热部署

Spring Boot的主要优点:

  • 为所有Spring开发者更快的入门
  • 开箱即用,提供各种默认配置来简化项目配置
  • 内嵌式容器简化Web项目
  • 没有冗余代码生成和XML配置的要求

YML

yml配置优先级顺序
  • ​ 项目下config文件下

    • 项目下
      • classpath(就是resource文件夹下)\config\文件下
        • classpath下
  • yml的@ConfigurationProperties

  • 松散绑定 yml 中last-name 与 lastName一样

  • JSR303 数据效验类似于一层过滤器验证,保证数据合法

  • 复杂类型封装,利用yml封装对象,使用@value就不支持了

注解集

  • @RestController = @Controller + @RespnseBody(用于返回Json数据,异步处理Ajax会用)

  • @RequsetMapping 通过返回跳转路径 + @RespnseBody返回值会写入到HTTP response body中,

Springboot Web开发

  • static资源
  • 首页
  • jsp,模板引擎Themeleaf
  • 装配扩展
  • crud
  • 拦截器
  • 国际化(中英文切换)
@PathVariable,@RequestParam,@RequestBody三者比较
注解 	支持的类型 	支持的请求类型 	支持的Content-Type 	请求示例

@PathVariable 	url 	GET 	所有 	/test/{
   id}
@RequestParam 	url 	GET 	所有 	/test?id=1
                Body 	POST/PUT/DELETE/PATCH 	form-data或x-www.form-urlencoded 	id:1
@RequestBody 	Body 	POST/PUT/DELETE/PATCH 	json 	{
   "id":1}

将接口改成以@RequestBody注解方式接受json请求数据,而后将接收到的json数据转化为json对象,可以使用json对象的get()方法取得参数值,代码如下:

@PostMapping("/account")
public Object insertAccount(@RequestBody JSONObject jsonParam) {
   
	String userName=jsonParam.get("userName").toString()
	...
静态资源如何导入?

spring.mvc.static.path

​ 资源访问的优先级来说,1.resources 2.static 3. public 可以直接访问 , 但若设置过static.path的话,直接就return了,不会看后面的了

源码如下

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
    "classpath:/META-INF/resources/",
      "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

webjars:

  • ​ springboot可以以maven方式导入jquery

  • 在templates的页面一般只通过controller来访问

  • 改变首页路径需要用模板引擎thymeleaf

使用hashmap<>模拟数据完成Dao层功能
private static Map<Integer, Department> departments = null;

static{
   
    departments = new HashMap<Integer,Department>();

    departments.put(101,new Department(101,"销售部"));
    departments.put(102,new Department(102,"采购部"));
    departments.put(103,new Department(103,"运营部"));
    departments.put(104,new Department(104,"教育部"));
    departments.put(105,new Department(105,"工商部"));
}

以此为参考

Thymeleaf的使用

  • html标签加入

    <html lang="en" xmlns:th="http://www.thymeleaf.org">
        
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
    
#关闭缓存
spring.thymeleaf.cache = false

关于前端模板的使用,

一般顶部导航栏topbar和侧边栏sidebar写在commons中

<!--导航栏-->
<div th:insert="~{common/commons::topbar(active='list.html')}"></div>
<!--侧边栏-->
				<div th:insert="~{common/commons::sidebar(active='list.html')}"></div>
Durid

监控数据的,springboot数据池用的HikariDataSource (号称java WEB最快的连接池)

!!!别忘了加log4j的依赖

对应一个个的Config

将springboot的datasource的type设置为Druid,数据库底层仍然是jdbc

  • filter 过滤 属性为字符串,stat为监控,log4j为日志,wall为防止sql注入

    Map<String,String>里加配置

    exclusions里*.js,*.css,/druid/*不进行统计

  • mock 测试

  • pool 测试

  • proxy 代理

  • sql ,Utils封装了一些东西可以toSting

  • support 支持,ibatis,hibernate,spring

配置:

  • 登陆的人,密码,可以访问权限人,不要忘记加上@Bean
  • springboot内置serlvet,想使用可以用替代方法
配置
/*
   将自定义的 Druid数据源添加到容器中,不再让 Spring Boot 自动创建
   绑定全局配置文件中的 druid 数据源属性到 com.alibaba.druid.pool.DruidDataSource从而让它们生效
   @ConfigurationProperties(prefix = "spring.datasource"):作用就是将 全局配置文件中
   前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中
 */
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource() {
   
    return new DruidDataSource();
}


//配置 Druid 监控管理后台的Servlet;
//内置 Servlet 容器时没有web.xml文件,所以使用 Spring Boot 的注册 Servlet 方式
//后台监控:相当于web.xml,ServletRegistrationBean
@Bean
public ServletRegistrationBean statViewServlet() {
   
    ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

    // 这些参数可以在 com.alibaba.druid.support.http.StatViewServlet
    // 的父类 com.alibaba.druid.support.http.ResourceServlet 中找到
    //后台需要有人登陆,账号密码配置
    Map<String, String> initParams = new HashMap<>();
    initParams.put("loginUsername", "admin"); //后台管理界面的登录账号,key是固定的
    initParams.<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值