refactor(mybatis):重构类型处理器实现以提升可维护性

移除 Kotlin 编写的 DateTimeTypeHandler 和 UUID 相关类型处理器,
新增 Java 编写的通用 QuickBaseTypeHandler 抽象类及辅助类,统一处理 MyBatis 类型转换逻辑。同时更新 UUID 类的 toString 方法,
增强不同版本 UUID 的信息展示。升级项目版本至 4.2.4。
This commit is contained in:
2025-09-21 21:55:43 +08:00
parent ac92f62967
commit a081744f14
8 changed files with 249 additions and 503 deletions
@@ -0,0 +1,47 @@
/*
* Copyright 2025 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile CallType.java
* LastUpdate 2025-09-21 21:06:52
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.mybatis;
/**
* CallType枚举类定义了不同的调用类型
* 用于标识数据库操作中结果集和可调用语句的访问方式
*/
public enum CallType {
/**
* 通过索引访问结果集
* 使用数字索引位置来获取结果集中的数据
*/
RESULTSET_INDEX,
/**
* 通过名称访问结果集
* 使用列名来获取结果集中的数据
*/
RESULTSET_NAME,
/**
* 通过索引访问可调用语句
* 使用数字索引位置来获取可调用语句中的数据
*/
CALLABLE_STATEMENT_INDEX
}
@@ -0,0 +1,126 @@
/*
* Copyright 2025 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile QuickBaseTypeHandler.java
* LastUpdate 2025-09-21 21:10:36
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.mybatis;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 抽象类 QuickBaseTypeHandler 是 MyBatis 的 BaseTypeHandler 的扩展,
* 提供了统一处理数据库字段与 Java 类型之间转换的抽象方法。
* 子类需要实现 getValue 和 setValue 方法来完成具体的类型转换逻辑。
*
* @param <T> 要处理的 Java 类型
*/
public abstract class QuickBaseTypeHandler<T> extends BaseTypeHandler<T> {
/**
* 抽象方法,用于从数据库结果中获取并转换为 Java 类型 T。
*
* @param vg 值获取器,封装了 ResultSet 或 CallableStatement
* @param ct 调用类型,标识当前是从 ResultSet 还是 CallableStatement 获取数据
* @param ci 列索引(可为 null)
* @param cn 列名(可为 null)
* @return 转换后的 Java 类型 T 实例
* @throws SQLException SQL 执行异常时抛出
*/
public abstract T getValue(
QuickBaseTypeHandlerValueGetter vg,
CallType ct,
Integer ci,
String cn
) throws SQLException;
/**
* 抽象方法,用于将 Java 类型 T 设置到 PreparedStatement 中。
*
* @param ps PreparedStatement 对象
* @param index 参数索引位置
* @param parameter Java 类型 T 的实例
* @param jdbcType JDBC 类型
* @throws SQLException SQL 执行异常时抛出
*/
public abstract void setValue(PreparedStatement ps, int index, T parameter, JdbcType jdbcType) throws SQLException;
/**
* 实现 BaseTypeHandler 的 setNonNullParameter 方法,
* 将非空参数设置到 PreparedStatement 中。
*
* @param ps PreparedStatement 对象
* @param i 参数索引位置
* @param parameter Java 类型 T 的实例(非空)
* @param jdbcType JDBC 类型
* @throws SQLException SQL 执行异常时抛出
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
setValue(ps, i, parameter, jdbcType);
}
/**
* 实现 BaseTypeHandler 的 getNullableResult 方法,
* 通过列名从 ResultSet 中获取可能为 null 的结果。
*
* @param rs ResultSet 对象
* @param columnName 数据库列名
* @return 转换后的 Java 类型 T 实例(可能为 null)
* @throws SQLException SQL 执行异常时抛出
*/
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
return getValue(new QuickBaseTypeHandlerValueGetter(null, rs), CallType.RESULTSET_NAME, null, columnName);
}
/**
* 实现 BaseTypeHandler 的 getNullableResult 方法,
* 通过列索引从 ResultSet 中获取可能为 null 的结果。
*
* @param rs ResultSet 对象
* @param columnIndex 数据库列索引
* @return 转换后的 Java 类型 T 实例(可能为 null)
* @throws SQLException SQL 执行异常时抛出
*/
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return getValue(new QuickBaseTypeHandlerValueGetter(null, rs), CallType.RESULTSET_NAME, columnIndex, null);
}
/**
* 实现 BaseTypeHandler 的 getNullableResult 方法,
* 通过列索引从 CallableStatement 中获取可能为 null 的结果。
*
* @param cs CallableStatement 对象
* @param columnIndex 数据库列索引
* @return 转换后的 Java 类型 T 实例(可能为 null)
* @throws SQLException SQL 执行异常时抛出
*/
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return getValue(new QuickBaseTypeHandlerValueGetter(cs, null), CallType.CALLABLE_STATEMENT_INDEX, columnIndex, null);
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2025 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile QuickBaseTypeHandlerValueGetter.java
* LastUpdate 2025-09-21 21:07:10
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.mybatis;
import java.sql.CallableStatement;
import java.sql.ResultSet;
/**
* QuickBaseTypeHandlerValueGetter类用于封装CallableStatement和ResultSet对象的获取操作
* 该类提供了对存储过程调用结果和查询结果集的统一访问接口
*/
public class QuickBaseTypeHandlerValueGetter {
private final CallableStatement callableStatement;
private final ResultSet resultSet;
/**
* 构造函数,初始化QuickBaseTypeHandlerValueGetter实例
*
* @param callableStatement 存储过程调用语句对象,用于执行存储过程
* @param resultSet 查询结果集对象,用于获取查询结果
*/
public QuickBaseTypeHandlerValueGetter(CallableStatement callableStatement, ResultSet resultSet) {
this.callableStatement = callableStatement;
this.resultSet = resultSet;
}
/**
* 获取结果集对象
*
* @return ResultSet 查询结果集对象
*/
public ResultSet getResultSet() {
return resultSet;
}
/**
* 获取存储过程调用语句对象
*
* @return CallableStatement 存储过程调用语句对象
*/
public CallableStatement getCallableStatement() {
return callableStatement;
}
}