118 lines
2.6 KiB
Java
118 lines
2.6 KiB
Java
package com.imyeyu.spring.mapper;
|
||
|
||
import com.imyeyu.spring.util.SQLProvider;
|
||
import org.apache.ibatis.annotations.DeleteProvider;
|
||
import org.apache.ibatis.annotations.InsertProvider;
|
||
import org.apache.ibatis.annotations.Options;
|
||
import org.apache.ibatis.annotations.SelectProvider;
|
||
import org.apache.ibatis.annotations.UpdateProvider;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 基本 SQL 映射,子接口可以不实现
|
||
*
|
||
* @author 夜雨
|
||
* @version 2021-07-16 09:40
|
||
*/
|
||
public interface BaseMapper<T, P> {
|
||
|
||
/**
|
||
* 排序方式
|
||
*
|
||
* @author 夜雨
|
||
* @version 2023-09-05 22:14
|
||
*/
|
||
enum OrderType {
|
||
|
||
ASC,
|
||
|
||
DESC
|
||
}
|
||
|
||
static final String NOT_DELETE = " AND `deleted_at` IS NULL ";
|
||
|
||
static final String LIMIT_1 = " LIMIT 1";
|
||
|
||
static final String UNIX_TIME = " FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000) ";
|
||
|
||
static final String PAGE = NOT_DELETE + " LIMIT #{offset}, #{limit}";
|
||
|
||
/**
|
||
* 统计数据量
|
||
*
|
||
* @return 数据量
|
||
*/
|
||
@SelectProvider(type = SQLProvider.class, method = "count")
|
||
long count();
|
||
|
||
default List<T> list(long offset, int limit) {
|
||
return listOrder(offset, limit, null);
|
||
}
|
||
|
||
/**
|
||
* 获取部分数据
|
||
*
|
||
* @param offset 偏移
|
||
* @param limit 数据量
|
||
* @return 数据列表
|
||
*/
|
||
@SelectProvider(type = SQLProvider.class, method = "listOrder")
|
||
List<T> listOrder(long offset, int limit, Map<String, OrderType> orderMap);
|
||
|
||
@SelectProvider(type = SQLProvider.class, method = "listAll")
|
||
List<T> listAll();
|
||
|
||
/**
|
||
* 创建数据。默认自增主键为 id,如需修改请重写此接口
|
||
*
|
||
* @param t 数据对象
|
||
*/
|
||
@InsertProvider(type = SQLProvider.class, method = "insert")
|
||
@Options(useGeneratedKeys = true, keyProperty = "id")
|
||
void insert(T t);
|
||
|
||
/**
|
||
* 根据 ID 获取对象
|
||
*
|
||
* @param id 索引
|
||
* @return 数据对象
|
||
*/
|
||
@SelectProvider(type = SQLProvider.class, method = "select")
|
||
T select(P id);
|
||
|
||
@SelectProvider(type = SQLProvider.class, method = "selectByExample")
|
||
T selectByExample(T t);
|
||
|
||
@SelectProvider(type = SQLProvider.class, method = "selectAllByExample")
|
||
List<T> selectAllByExample(T t);
|
||
|
||
/**
|
||
* 修改数据
|
||
*
|
||
* @param t 数据对象
|
||
*/
|
||
@UpdateProvider(type = SQLProvider.class, method = "update")
|
||
void update(T t);
|
||
|
||
@UpdateProvider(type = SQLProvider.class, method = "updateSelective")
|
||
void updateSelective(T t);
|
||
|
||
/**
|
||
* 软删除
|
||
*
|
||
* @param id 索引
|
||
*/
|
||
@UpdateProvider(type = SQLProvider.class, method = "delete")
|
||
void delete(P id);
|
||
|
||
/**
|
||
* 销毁(物理删除)
|
||
*
|
||
* @param id 索引
|
||
*/
|
||
@DeleteProvider(type = SQLProvider.class, method = "destroy")
|
||
void destroy(P id);
|
||
}
|