All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.edgar615.util.spring.jdbc.JdbcOperation Maven / Gradle / Ivy

package com.github.edgar615.util.spring.jdbc;

import com.github.edgar615.util.db.Persistent;
import com.github.edgar615.util.search.Example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JDBC的操作
 */
public interface JdbcOperation {

  /**
   * 新增数据.
   *
   * @param persistent 持久化对象
   * @param  主键类型
   */
   void insert(Persistent persistent);

  /**
   * insert一条数据,并返回自增主键
   *
   * @param persistent 持久化对象
   * @param  主键类型
   */
   ID insertAndGeneratedKey(Persistent persistent);

  /**
   * 批量插入
   *
   * @param persistentList 持久化对象的集合
   * @param  主键列席
   * @param  持久化对象
   */
  > void batchInsert(List persistentList);

  /**
   * 根据主键删除.
   *
   * @param elementType 持久化对象
   * @param id 主键
   * @param  主键类型
   * @param  持久化对象
   * @return 删除记录数
   */
  > int deleteById(Class elementType, ID id);

  /**
   * 根据条件删除.
   *
   * @param elementType 持久化对象
   * @param example 查询条件
   * @param  主键类型
   * @param  持久化对象
   * @return 删除记录数
   */
  > int deleteByExample(Class elementType, Example example);

  /**
   * 根据主键更新,忽略实体中的null
   *
   * @param persistent 持久化对象
   * @param addOrSub 需要做增加或者减去的字段,value为正数表示增加,负数表示减少
   * @param nullFields 需要设为null的字段
   * @param id 主键ID
   * @param  主键类型
   * @return 修改记录数
   */
   int updateById(Persistent persistent,
      Map addOrSub,
      List nullFields,
      ID id);

  /**
   * 根据条件更新,忽略实体中的null
   *
   * @param persistent 持久化对象
   * @param addOrSub 需要做增加或者减去的字段,value为正数表示增加,负数表示减少
   * @param nullFields 需要设为null的字段
   * @param example 查询条件
   * @param  主键类型
   * @return 修改记录数
   */
   int updateByExample(Persistent persistent,
      Map addOrSub,
      List nullFields, Example example);

  /**
   * 根据主键查找.
   *
   * @param elementType 持久化对象
   * @param id 主键
   * @param fields 返回的属性列表
   * @param  主键类型
   * @param  持久化对象
   * @return 持久化对象
   */
  > T findById(Class elementType, ID id, List fields);

  /**
   * 根据条件查找.
   *
   * @param elementType 持久化对象,
   * @param example 查询参数的定义,包括查询条件、排序规则等
   * @param  主键类型
   * @param  持久化对象
   * @return 持久化对象列表
   */
  > List findByExample(Class elementType, Example example);

  /**
   * 根据条件查找.
   *
   * @param elementType 持久化对象
   * @param example 查询参数的定义,包括查询条件、排序规则等
   * @param start 开始索引
   * @param limit 查询数量
   * @param  主键类型
   * @param  持久化对象
   * @return 持久化对象列表
   */
  > List findByExample(Class elementType, Example example,
      int start,
      int limit);

  /**
   * 根据条件查找.
   *
   * @param elementType 持久化对象
   * @param example 查询条件
   * @param  主键类型
   * @param  持久化对象
   * @return 记录数
   */
  > int countByExample(Class elementType, Example example);

  /**
   * 根据主键查找.
   *
   * @param elementType 持久化对象
   * @param id 主键
   * @param  主键类型
   * @param  持久化对象
   * @return 持久化对象,如果未找到任何数据,返回null
   */
  > T findById(Class elementType, ID id);

  /**
   * 根据主键更新,忽略实体中的null.
   *
   * @param persistent 持久化对象
   * @param id 主键
   * @param  主键类型
   * @return 修改记录数
   */
  default  int updateById(Persistent persistent, ID id) {
    return updateById(persistent, new HashMap<>(), new ArrayList<>(), id);
  }

  /**
   * 根据条件更新,忽略实体中的null.
   *
   * @param persistent 持久化对象
   * @param example 查询条件
   * @param  条件集合
   * @return 修改记录数
   */
  default  int updateByExample(Persistent persistent, Example example) {
    return updateByExample(persistent, new HashMap<>(), new ArrayList<>(), example);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy