Class AbstractRelationalDataOperator<T extends BaseDataEntity<String>>

java.lang.Object
com.ultikits.ultitools.interfaces.impl.data.AbstractRelationalDataOperator<T>
Type Parameters:
T - the entity type
All Implemented Interfaces:
DataOperator<T>
Direct Known Subclasses:
MysqlDataOperator, SQLiteDataOperator

public abstract class AbstractRelationalDataOperator<T extends BaseDataEntity<String>> extends Object implements DataOperator<T>
Abstract base class for relational database data operators.

This class extracts common logic from MySQL and SQLite implementations, providing a unified implementation for CRUD operations. Only the database-specific SQL dialect differences need to be overridden.

Since:
6.2.0
Author:
wisdomme
  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • GSON

      private static final com.google.gson.Gson GSON
    • type

      protected final Class<T extends BaseDataEntity<String>> type
    • dataSource

      protected final DataSource dataSource
    • tableName

      protected final String tableName
    • queryRunner

      protected final org.apache.commons.dbutils.QueryRunner queryRunner
    • transactionManager

      protected TransactionManager transactionManager
  • Constructor Details

    • AbstractRelationalDataOperator

      protected AbstractRelationalDataOperator(DataSource dataSource, Class<T> type)
      Creates a new data operator.
      Parameters:
      dataSource - the data source to use
      type - the entity class
  • Method Details

    • setTransactionManager

      public void setTransactionManager(TransactionManager transactionManager)
      Sets the transaction manager for transaction-aware operations.
      Parameters:
      transactionManager - the transaction manager
    • initializeTable

      private void initializeTable()
      Initializes the database table.
    • getListHandler

      protected org.apache.commons.dbutils.ResultSetHandler<List<T>> getListHandler()
    • exist

      public boolean exist(T object)
      Description copied from interface: DataOperator
      Check if the data record exists.

      查询记录数据是否存在。

      Specified by:
      exist in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      object - Data record entity
      数据记录实体
      Returns:
      Whether the record exists
      记录是否存在
    • exist

      public boolean exist(WhereCondition... whereConditions)
      Description copied from interface: DataOperator
      Check if the data record exists.

      使用条件查询记录是否存在

      Specified by:
      exist in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      whereConditions - Conditions
      条件参数
      Returns:
      Whether the record exists
      记录是否存在
    • getById

      public T getById(Object id)
      Description copied from interface: DataOperator
      Get data record by ID.

      使用ID获取记录

      Specified by:
      getById in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      id - Record ID
      记录ID
      Returns:
      Data record
      数据记录
    • getAll

      public List<T> getAll()
      Description copied from interface: DataOperator
      Get all data record.

      查询所有记录

      Specified by:
      getAll in interface DataOperator<T extends BaseDataEntity<String>>
      Returns:
      Data record list
      数据记录列表
    • getAll

      public List<T> getAll(WhereCondition... whereConditions)
      Description copied from interface: DataOperator
      Get all data record by conditions.

      查询所有符合条件的记录

      Specified by:
      getAll in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      whereConditions - Conditions
      条件参数
      Returns:
      Data record list
      数据记录列表
    • getLike

      public List<T> getLike(String column, String value, DataOperator.LikeType likeType)
      Description copied from interface: DataOperator
      Fuzzy Query

      模糊查询

      Specified by:
      getLike in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      column - Column name
      列名
      value - Query value
      查询值
      likeType - Like type
      模糊查询类型
      DataOperator.LikeType
      Returns:
      Data record list
      数据记录列表
    • page

      public List<T> page(int page, int size, WhereCondition... whereConditions)
      Description copied from interface: DataOperator
      Get data record by page.

      分页查询

      Specified by:
      page in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      page - Page number
      页码
      size - Page size
      页大小
      whereConditions - Conditions
      条件参数
      Returns:
      Data record list
      数据记录列表
    • insert

      public void insert(T obj)
      Description copied from interface: DataOperator
      Insert data record.

      新增记录

      Specified by:
      insert in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      obj - Data record
      数据记录
    • del

      public void del(WhereCondition... whereConditions)
      Description copied from interface: DataOperator
      Delete data record by conditions.

      按照条件删除记录

      Specified by:
      del in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      whereConditions - Conditions
      条件参数
    • delById

      public void delById(Object id)
      Description copied from interface: DataOperator
      Delete data record by ID.

      按照ID删除记录

      Specified by:
      delById in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      id - Record ID
      记录ID
    • update

      public void update(String column, Object value, Object id)
      Description copied from interface: DataOperator
      Update one field of one record.

      更新某个记录的某个值

      Specified by:
      update in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      column - Column name
      列名
      value - New value
      新值
      id - Record ID
      记录ID
    • update

      public void update(T obj) throws IllegalAccessException
      Description copied from interface: DataOperator
      Update data record by object. It will not update the fields that the incoming entity does not have.

      使用实体更新记录。不会更新传入实体没有的字段。

      Specified by:
      update in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      obj - Data record
      数据记录
      Throws:
      IllegalAccessException - Please referIllegalAccessException
    • transaction

      public <R> R transaction(Callable<R> action) throws Exception
      Description copied from interface: DataOperator
      Execute operations within a transaction. All operations commit together or roll back on exception. For SQL backends, uses database transactions. For JSON, uses snapshot-based rollback.

      在事务中执行操作。所有操作一起提交或在异常时回滚。

      Specified by:
      transaction in interface DataOperator<T extends BaseDataEntity<String>>
      Type Parameters:
      R - the return type
      Parameters:
      action - the operations to execute
      Returns:
      the result of the action
      Throws:
      Exception - if the action fails
    • transaction

      public void transaction(Runnable action)
      Description copied from interface: DataOperator
      Execute operations within a transaction (void variant).

      在事务中执行操作(无返回值)。

      Specified by:
      transaction in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      action - the operations to execute
    • insertAll

      public void insertAll(List<T> entities)
      Description copied from interface: DataOperator
      Insert multiple entities atomically. Uses JDBC batch for SQL backends.

      批量原子插入。SQL后端使用JDBC批处理。

      Specified by:
      insertAll in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      entities - the entities to insert
    • updateAll

      public void updateAll(List<T> entities) throws IllegalAccessException
      Description copied from interface: DataOperator
      Update multiple entities atomically. Uses JDBC batch for SQL backends.

      批量原子更新。SQL后端使用JDBC批处理。

      Specified by:
      updateAll in interface DataOperator<T extends BaseDataEntity<String>>
      Parameters:
      entities - the entities to update
      Throws:
      IllegalAccessException - if field access fails
    • getColumnMappings

      private List<AbstractRelationalDataOperator.ColumnMapping> getColumnMappings()
    • createTableSqlFromClazz

      protected abstract String createTableSqlFromClazz(Class<T> type)
      Creates the SQL statement to create the table.

      Override this method to customize table creation for different databases.

      Parameters:
      type - the entity class
      Returns:
      the CREATE TABLE SQL statement
    • buildColumnDefinitions

      protected String buildColumnDefinitions(Class<T> type)
      Builds the column definitions portion of CREATE TABLE.