218 lines
4.7 KiB
Java
218 lines
4.7 KiB
Java
package com.imyeyu.spring.mapper;
|
|
|
|
import com.imyeyu.spring.bean.Logic;
|
|
import com.imyeyu.spring.bean.Page;
|
|
import com.imyeyu.spring.bean.PageResult;
|
|
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.Param;
|
|
import org.apache.ibatis.annotations.SelectProvider;
|
|
import org.apache.ibatis.annotations.UpdateProvider;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 基本 SQL 映射
|
|
*
|
|
* @param <T> 实体类型
|
|
* @param <P> 主键类型
|
|
* @author 夜雨
|
|
* @version 2021-07-16 09:40
|
|
*/
|
|
public interface BaseMapper<T, P> {
|
|
|
|
/**
|
|
* 排序方式
|
|
*
|
|
* @author 夜雨
|
|
* @version 2023-09-05 22:14
|
|
*/
|
|
enum OrderType {
|
|
|
|
/** 升序 */
|
|
ASC,
|
|
|
|
/** 降序 */
|
|
DESC
|
|
}
|
|
|
|
/** 当前时间戳毫秒 */
|
|
String UNIX_TIME = " FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000) ";
|
|
|
|
/** 未删除条件 */
|
|
String NOT_DELETE = " AND (`deleted_at` IS NULL OR " + UNIX_TIME + " < `deleted_at`) ";
|
|
|
|
/** 限制一条 */
|
|
String LIMIT_1 = " LIMIT 1";
|
|
|
|
/** 分页限制 */
|
|
String PAGE = NOT_DELETE + " LIMIT #{offset}, #{limit}";
|
|
|
|
/**
|
|
* 根据 Page 对象查询数据列表
|
|
*
|
|
* @param page 分页参数
|
|
* @return 数据列表
|
|
*/
|
|
@SelectProvider(type = SQLProvider.class, method = "selectByPage")
|
|
List<T> selectByPage(Page<T> page);
|
|
|
|
/**
|
|
* 根据 Page 对象统计数据量
|
|
*
|
|
* @param page 分页参数
|
|
* @return 数据量
|
|
*/
|
|
@SelectProvider(type = SQLProvider.class, method = "countByPage")
|
|
long countByPage(Page<T> page);
|
|
|
|
/**
|
|
* 分页查询
|
|
*
|
|
* @param page 分页参数
|
|
* @return 分页结果
|
|
*/
|
|
default PageResult<T> selectPageResult(Page<T> page) {
|
|
PageResult<T> result = new PageResult<>();
|
|
result.setTotal(countByPage(page));
|
|
result.setList(selectByPage(page));
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 查询全部数据
|
|
*
|
|
* @return 数据列表
|
|
*/
|
|
@SelectProvider(type = SQLProvider.class, method = "selectAll")
|
|
List<T> selectAll();
|
|
|
|
/**
|
|
* 创建数据
|
|
*
|
|
* @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);
|
|
|
|
/**
|
|
* 根据示例查询单条数据
|
|
*
|
|
* @param t 示例对象
|
|
* @return 数据对象
|
|
*/
|
|
default T selectByExample(T t) {
|
|
return selectByExample(t, Logic.AND);
|
|
}
|
|
|
|
/**
|
|
* 根据示例查询单条数据
|
|
*
|
|
* @param t 示例对象
|
|
* @param logic 条件连接逻辑
|
|
* @return 数据对象
|
|
*/
|
|
@SelectProvider(type = SQLProvider.class, method = "selectByExample")
|
|
T selectByExample(@Param("entity") T t, @Param("logic") Logic logic);
|
|
|
|
/**
|
|
* 根据示例查询全部数据
|
|
*
|
|
* @param t 示例对象
|
|
* @return 数据列表
|
|
*/
|
|
default List<T> selectAllByExample(T t) {
|
|
return selectAllByExample(t, Logic.AND);
|
|
}
|
|
|
|
/**
|
|
* 根据示例查询全部数据
|
|
*
|
|
* @param t 示例对象
|
|
* @param logic 条件连接逻辑
|
|
* @return 数据列表
|
|
*/
|
|
@SelectProvider(type = SQLProvider.class, method = "selectAllByExample")
|
|
List<T> selectAllByExample(@Param("entity") T t, @Param("logic") Logic logic);
|
|
|
|
/**
|
|
* 修改数据
|
|
*
|
|
* @param t 数据对象
|
|
*/
|
|
@UpdateProvider(type = SQLProvider.class, method = "update")
|
|
void update(T t);
|
|
|
|
/**
|
|
* 选择性更新
|
|
*
|
|
* @param t 数据对象
|
|
*/
|
|
@UpdateProvider(type = SQLProvider.class, method = "updateSelective")
|
|
void updateSelective(T t);
|
|
|
|
/**
|
|
* 软删除
|
|
*
|
|
* @param id 索引
|
|
*/
|
|
@UpdateProvider(type = SQLProvider.class, method = "delete")
|
|
void delete(P id);
|
|
|
|
/**
|
|
* 根据示例批量逻辑删除
|
|
*
|
|
* @param t 示例对象
|
|
*/
|
|
default void deleteAllByExample(T t) {
|
|
deleteAllByExample(t, Logic.AND);
|
|
}
|
|
|
|
/**
|
|
* 根据示例批量逻辑删除
|
|
*
|
|
* @param t 示例对象
|
|
* @param logic 条件连接逻辑
|
|
*/
|
|
@UpdateProvider(type = SQLProvider.class, method = "deleteAllByExample")
|
|
void deleteAllByExample(@Param("entity") T t, @Param("logic") Logic logic);
|
|
|
|
/**
|
|
* 销毁(物理删除)
|
|
*
|
|
* @param id 索引
|
|
*/
|
|
@DeleteProvider(type = SQLProvider.class, method = "destroy")
|
|
void destroy(P id);
|
|
|
|
/**
|
|
* 根据示例批量销毁(物理删除)
|
|
*
|
|
* @param t 示例对象
|
|
*/
|
|
default void destroyAllByExample(T t) {
|
|
destroyAllByExample(t, Logic.AND);
|
|
}
|
|
|
|
/**
|
|
* 根据示例批量销毁(物理删除)
|
|
*
|
|
* @param t 示例对象
|
|
* @param logic 条件连接逻辑
|
|
*/
|
|
@DeleteProvider(type = SQLProvider.class, method = "destroyAllByExample")
|
|
void destroyAllByExample(@Param("entity") T t, @Param("logic") Logic logic);
|
|
}
|