gpt4 book ai didi

java - 如何在每个事务开始时运行自定义查询?

转载 作者:行者123 更新时间:2023-11-30 05:38:25 27 4
gpt4 key购买 nike

我在项目中使用 Firebird 数据库,并且 DB 中的存储过程使用自定义 context variables属于事务范围。

我必须在每个事务开始时将一些变量设置为上下文。如何在每个 @Transactional 注释方法中不重复代码的情况下实现这一点?

示例:

Controller

@RestController
@RequestMapping({TBL_EMPLOYEE, TBL_EMP})
public class EmployeeController extends EmployeeCommonController<Employee> {

@GetMapping(PATH_LASTLOGIN)
public List<UserLastLoginWrapper> getUserLastLoginWrapper(Long userid, tring appname) {
return getService().getUserLastLoginWrapper(userid, appname);
}
}

服务

@Transactional
public class EmployeeService{
public List<UserLastLoginWrapper> getUserLastLoginWrapper(Long userid, String appname) {
return ((EmployeeRepository) getRepository()).getUserLastLoginWrapper(null, userid, appname);
}
}

存储库

@NamedNativeQuery(name = "Employee.getUserLastLoginWrapper", query = "select * from SP_USER_LAST_LOGIN(:userid, :appname)", resultSetMapping = UserLastLoginWrapper.USERLASTLOGINWRAPPER)

大多数存储过程都试图从上下文变量中获取 hotelrefno 信息,因此我必须调用 execute procedure SP_CTX_SET_LOGIN_INFO(:hotelrefno, :userid)每笔交易开始时的过程。

最佳答案

您可以创建一个 Before Aspect

@Aspect
public class ProdcedureAspect {

@Before("execution(* **.*Service.*(..))")
public void doBefore(JoinPoint joinPoint) {
// execute procedure SP_CTX_SET_LOGIN_INFO(:hotelrefno, :userid)
}
}

在这里阅读有关 Spring AOP 的更多信息:

https://docs.spring.io/spring/docs/5.1.7.RELEASE/spring-framework-reference/core.html#aop-api

关于java - 如何在每个事务开始时运行自定义查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56182755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com