SpringBoot爬坑记录

本文深入探讨了SpringBoot中事务回滚机制,通过注解实现异常时的回滚操作,并介绍了如何解决配置类序列化失败的问题。此外,还详细讲解了SpringBoot下跨域请求的配置方法,以及JPA查询时的参数传递技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Transactional事务回滚

//方法上增加注解
@Transactional(rollbackOn = Exception.class)
public void example() {
	try {

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

配置类序列化失败

问题描述:

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

SpringBoot请求跨域

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. 使用占位符?
@Query(value = "select account_id,user_name,validity from t_account where account_id = ?", nativeQuery = true)
User findByAccount_id(Long account_id);
  1. 使用命名化参数:name
@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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值