SpringBoot笔记

简介: SpringBoot笔记

IDEA打开Run Dashboard窗口


修改D:\work\IntelliJ IDEA 2018.2.4Workspace\SpringCloudDemoHoxton\.idea下的workspace.xml


 <option name="configurationTypes">
      <set>
        <option value="SpringBootApplicationConfigurationType" />
      </set>
    </option>


20200408004756304.png


SpringBoot加入webjars依赖


将原来的BootStrap.js用依赖的方式加入到项目中(以BootStrap为例)


1)访问  WebJars - Web Libraries in Jars  


2)选择好  版本 和 引入的方式


20190628103330371.png


3)在依赖中查看静态资源的路径


333.png


4)开启服务器并且访问


http://localhost:8080/webjars/bootstrap/4.0.0/js/bootstrap.js


5)webjars在html中的使用


注意:引入的是css文件而不是js文件,引入的是css文件而不是js文件,引入的是css文件而不是js文件


<link rel="stylesheet"  href="/webjars/bootstrap/4.0.0/css/bootstrap.css" />


thymeleaf中使用webjars


<link rel="stylesheet"  th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" />


SpringBoot整合Thymeleaf


整合


1)加入依赖


<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>


2)在hml中引入命名空间


<html xmlns:th="http://www.thymeleaf.org"  lang="en">  


3)关闭缓存


#开发时关闭缓存
spring.thymeleaf.cache=false


4)开始使用


<div th:text="${name}"></div>


语法


取一个普通的值


<div th:text="${name}"></div>


foreach循环


<div> 
       <p th:text="${message}" th:each="message : ${userlist}" />
  </div>


if语句


  <p th:text="男" th:if="${sex==1}"></p> 
   <p th:text="女" th:if="${sex==0}"></p> 


URL


<a th:href="@{'/www.baidu.com?sex='+${sex}}">click me</a>
<form  method="post"  th:action="@{'/hello?sex='+${sex}}">


@RequestMapping("/success")
  public String hello(Map<String,Object> map){
    map.put("name", "张三");
    List<String> list=new ArrayList<>();
    list.add("zhangsan");
    list.add("lisi");
    list.add("wangwu");
    map.put("userlist", list);
    map.put("sex", 0);
    return "success";
  }


SpringBoot整合swagger


<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
    </dependency>


@EnableSwagger2


SpringBoot定制错误页面


1)没有模板引擎


331.png


2)有模板引擎


error文件夹放在templates文件下


SpringBoot更改项目图标


步骤


在项目的静态资源文件夹下放一个favicon.ico图标,注意:名字为favicon.ico   favicon.ico  favicon.ico


332.png


启动项目即可


333.png


依据


334.png


SpringBoot获得application.properties中数据


335.png


package com.example.demo.entity;  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.context.annotation.PropertySource;  
import org.springframework.stereotype.Component;  
@Component  
@PropertySource("classpath:jdbc.properties")//如果是application.properties,就不用写@PropertyScource("application.properties"),其他名字用些  
public class Jdbc {  
    @Value("${jdbc.user}")
    private String user;  
    @Value("${jdbc.password}") 
    private String password;  
    public void speack(){  
        System.out.println("username:"+user+"------"+"password:"+password);  
    }  
}


SpringBoot junit 测试


package com.example.demo.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.example.demo.SpringBootTestApplication;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=SpringBootTestApplication.class)//制定SpinrgBoot的启动类
@WebAppConfiguration//由于是web项目,所以要模拟Servletontext
public class TestHelloService {
  @Autowired
  private HelloService service;
  @Test
  public void test() {
    service.speak();
  }
}


SpringBoot全局异常捕捉


@ControllerAdvice  @ExceptionHandler(value=Exception.class)    @ResponseBody  一起使用

作用:当遇到一个错误时,可以捕捉到并且返回相应信息


@ControllerAdvice
public class AllException { 
  @ExceptionHandler(value=Exception.class)
  @ResponseBody
  public String exception(Exception e){
    return "处理错误!";
  }
}


336.png


338.png


SpringBoot使用自定义的properties


自定义的文件名是demo.properties,其中demo.name=李四


demo.id=1
demo.name=\u674E\u56DB


337.png


package com.example.demo.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component//加入到ioc容器
@PropertySource("classpath:demo.properties")//引入自己的properties
public class Demo {
  @Value("${demo.id}")
  private Integer id;
  @Value("${demo.name}")
  private String name;
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return "Demo [id=" + id + ", name=" + name + "]";
  }
}


SpringBoot导入XML


package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/** 
  * 
  * classpath路径:locations={"classpath:classpath:context.xml","classpath:classpath:context2.xml"} 
  * file路径: locations = {"file:d:/test/classpath:context.xml"};
 */ 
@Configuration 
@ImportResource(locations={"classpath:context.xml"}) 
public class Config {
}
目录
相关文章
|
3月前
|
运维 监控 Java
SpringBoot入门知识
SpringBoot入门知识
52 0
|
5月前
|
缓存 Java 数据库
|
7月前
|
开发框架 Java Maven
01SpringBoot入门
01SpringBoot入门
21 0
|
7月前
|
Cloud Native Java Go
gPRC与SpringBoot整合教程
gPRC与SpringBoot整合教程
57 0
|
8月前
|
JSON Java 应用服务中间件
|
8月前
|
Java 数据库
SpringBoot笔记
SpringBoot笔记
39 0
|
10月前
|
XML 前端开发 JavaScript
SpringBoot入门到精通-SpringBoot入门(二)
SpringBoot入门到精通-SpringBoot入门
SpringBoot入门到精通-SpringBoot入门(二)
|
Java Maven Spring
02、SpringBoot2入门
02、SpringBoot2入门
|
Java Maven Spring
SpringBoot2入门
SpringBoot2入门
97 0
|
XML 监控 Java
如何学习SpringBoot
如何学习SpringBoot
98 0
如何学习SpringBoot