SpringBoot笔记

Java
269
0
0
2024-02-26
标签   SpringBoot

IDEA打开Run Dashboard窗口

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

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

SpringBoot加入webjars依赖

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

1)访问 WebJars - Web Libraries in Jars

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

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

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

http://localhost:8080/swagger-ui.html

SpringBoot定制错误页面

1)没有模板引擎

2)有模板引擎

error文件夹放在templates文件下

SpringBoot更改项目图标

步骤

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

启动项目即可

依据

SpringBoot获得application.properties中数据

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 "处理错误!";
	}
}

SpringBoot使用自定义的properties

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

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

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 {

}