前言
近来,在框架中获取数据库执行器时,一直通过参数传递,颇觉繁琐,且不符合设计常理。
故而,思虑片刻,欲将其融入至笔者框架内置的“RequestAttributes”对象中。
此属性对象采用“ThreadLocal”作为线程局部变量同步。
以下是具体的流转思路图
基于以上思路,笔者先创建“ExecutorAttributes”类,将其定义为数据库执行器属性类,并定义“泛型”用于子类实现后可以通过“链式”调用其中内置函数。
其中,内置“数据库执行器”,“是否打开事务”,“是否提交事务”等一些基础函数。
以下,是具体的实现代码
package com.threeox.drivenlibrary.context.attr;
import com.threeox.dblibrary.executor.inter.ISqlExecutor;
import com.threeox.drivenlibrary.enums.ResponseResult;
import com.threeox.drivenlibrary.exception.ResponseException;
import com.threeox.utillibrary.logger.LoggerFactory;
/**
* 数据库执行器属性
*
* @author 赵屈犇
* @version 1.0
* @date 创建时间: 2022/6/29 21:27
*/
public class ExecutorAttributes<T> {
protected static final LoggerFactory logger = LoggerFactory.getLogger(ExecutorAttributes.class.getName());
/**
* 数据库执行器
*/
private ISqlExecutor executor;
/**
* 是否打开事务
*/
private boolean isStartTransaction = false;
/**
* 是否提交事务
*/
private boolean isSubmitTransaction = true;
/**
* 设置是否开启事务
*
* @param startTransaction
* @return a
* @author 赵屈犇
* @date 创建时间: 2022/6/29 21:33
* @version 1.0
*/
public T setStartTransaction(boolean startTransaction) {
isStartTransaction = startTransaction;
return (T) this;