SpringBoot Log

SpringBoot Log

Transactional事务回滚

1
2
3
4
5
6
7
8
9
10
//方法上增加注解
@Transactional(rollbackOn = Exception.class)
public void example() {
try {

} catch (Exception e) {
// 设置异常时执行回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
}

配置类序列化失败

问题描述:

将配置类使用 JackSon 序列化时,出现序列化异常

SpringBoot请求跨域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.learnning.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
*
* @ClassName: CustomCORSConfiguration
* @Description: 配置跨域请求访问失败的问题
* @author time
* @date 2018/11/21
*/
@Configuration
public class CustomCORSConfiguration {

private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}

@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}

}

SpringBoot JPA查询传参

  1. 使用占位符?
1
2
@Query(value = "select account_id,user_name,validity from t_account where account_id = ?", nativeQuery = true)
User findByAccount_id(Long account_id);
  1. 使用命名化参数:name
1
2
@Query(value = "select account_id,user_name,validity from t_account where account_id =:id", nativeQuery = true)
User findByAccount_id(@Param("id") Long account_id);

自定义SpringBoot web页面图标

  1. 将图标放在/src/main/resources/static/目录下(推荐使用favicon.ico名字,符合*.ico即可)

  2. 在需要替换图标的页面加入以下标签

    1
    <link rel="icon" type="image/x-icon" th:href="@{/favicon.ico}">

注:该方式为最简单的实现,可以根据自身情况做调整。更改相应的相对路径即可,可自行尝试。