|
|
@@ -1,382 +0,0 @@
|
|
|
-package cn.iocoder.yudao.framework.mybatis.core.mapper;
|
|
|
-
|
|
|
-import java.beans.PropertyDescriptor;
|
|
|
-import java.lang.reflect.Method;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.function.Function;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-import org.apache.ibatis.logging.Log;
|
|
|
-import org.apache.ibatis.logging.LogFactory;
|
|
|
-import org.apache.ibatis.reflection.property.PropertyNamer;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Assert;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
-import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
|
|
|
-import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
|
|
-
|
|
|
-
|
|
|
-public interface PlusBaseMapperX<T> extends BaseMapperX<T> {
|
|
|
- static final Log logger = LogFactory.getLog(PlusBaseMapperX.class);
|
|
|
- /**
|
|
|
- * 博客地址:https://blog.csdn.net/qq_19266669/article/details/114369195
|
|
|
- * 查看AbstractLambdaWrapper通过方法得到字段名称
|
|
|
- *
|
|
|
- * @param column
|
|
|
- * @return
|
|
|
- */
|
|
|
- default String columnToString(SFunction<T, ?> column) {
|
|
|
- return columnToString(column, true);
|
|
|
- }
|
|
|
-
|
|
|
- default String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
|
|
|
- ColumnCache cache = getColumnCache(column);
|
|
|
- return onlyColumn ? cache.getColumn() : cache.getColumnSelect();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取 SerializedLambda 对应的列信息,从 lambda 表达式中推测实体类
|
|
|
- * <p>
|
|
|
- * 如果获取不到列信息,那么本次条件组装将会失败
|
|
|
- *
|
|
|
- * @return 列
|
|
|
- * @throws com.baomidou.mybatisplus.core.exceptions.MybatisPlusException 获取不到列信息时抛出异常
|
|
|
- */
|
|
|
- default ColumnCache getColumnCache(SFunction<T, ?> column) {
|
|
|
- LambdaMeta meta = LambdaUtils.extract(column);
|
|
|
- String fieldName = PropertyNamer.methodToProperty(meta.getImplMethodName());
|
|
|
- Class<?> instantiatedClass = meta.getInstantiatedClass();
|
|
|
- Map<String, ColumnCache> columnMap = tryInitCache(instantiatedClass);
|
|
|
- return getColumnCache(fieldName, instantiatedClass, columnMap);
|
|
|
- }
|
|
|
-
|
|
|
- default ColumnCache getColumnCache(String fieldName, Class<?> lambdaClass, Map<String, ColumnCache> columnMap) {
|
|
|
- ColumnCache columnCache = columnMap.get(LambdaUtils.formatKey(fieldName));
|
|
|
- Assert.notNull(columnCache, "can not find lambda cache for this property [%s] of entity [%s]", fieldName,
|
|
|
- lambdaClass.getName());
|
|
|
- return columnCache;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- default Class<T> getEntityClass() {
|
|
|
- return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), PlusBaseMapperX.class, 0);
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- default Class<T> currentMapperClass() {
|
|
|
- return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), PlusBaseMapperX.class, 0);
|
|
|
- }
|
|
|
-
|
|
|
- default Map<String, ColumnCache> tryInitCache(Class<?> lambdaClass) {
|
|
|
-
|
|
|
- final Class<T> entityClass = getEntityClass();
|
|
|
- if (entityClass != null) {
|
|
|
- lambdaClass = entityClass;
|
|
|
- }
|
|
|
- Map<String, ColumnCache> columnMap = LambdaUtils.getColumnMap(lambdaClass);
|
|
|
- return columnMap;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 entity 条件,删除记录
|
|
|
- *
|
|
|
- * @param queryWrapper 实体包装类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default boolean delete(T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- return SqlHelper.retBool(delete(queryWrapper));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 whereEntity 条件,更新记录
|
|
|
- *
|
|
|
- * @param entity 实体对象
|
|
|
- * @param updateWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
|
|
|
- * @throws SecurityException
|
|
|
- * @throws NoSuchFieldException
|
|
|
- */
|
|
|
- default boolean update(T entity, SFunction<T, ?> column, Object value) {
|
|
|
-
|
|
|
- UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
|
|
|
- // 这里使用反射得到字段,
|
|
|
- String conditions = columnToString(column);
|
|
|
- updateWrapper.eq(conditions, value);
|
|
|
- return SqlHelper.retBool(update(entity, updateWrapper));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 whereEntity 条件,更新记录
|
|
|
- *
|
|
|
- * @param entity 实体对象
|
|
|
- * @param updateWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
|
|
|
- * @throws SecurityException
|
|
|
- * @throws NoSuchFieldException
|
|
|
- */
|
|
|
- default boolean update(T entity, Map<String, Object> columns) {
|
|
|
-
|
|
|
- UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
|
|
|
- for (String key : columns.keySet()) {
|
|
|
- updateWrapper.eq(key, columns.get(key));
|
|
|
- }
|
|
|
- return SqlHelper.retBool(update(entity, updateWrapper));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将
|
|
|
- *
|
|
|
- * @param entity
|
|
|
- * @param columns
|
|
|
- * @return
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- default boolean update(T entity, SFunction<T, ?>... columns) {
|
|
|
- // 这里是反射,通过反射来得到
|
|
|
- try {
|
|
|
- Map<String, Object> columnVal = new HashMap<>();
|
|
|
- for (SFunction<T, ?> column : columns) {
|
|
|
- String columnName = columnToString(column);
|
|
|
- String fieldName = getFieldName(column);
|
|
|
- Class<?> clazz = entity.getClass();
|
|
|
- PropertyDescriptor p = new PropertyDescriptor(fieldName, clazz);
|
|
|
- Method readMethod = p.getReadMethod();
|
|
|
- // 得到跟新的值
|
|
|
- Object value = readMethod.invoke(entity);
|
|
|
- // 把查询条件的值设置为null
|
|
|
- Method writeMethod = p.getWriteMethod();
|
|
|
- // 将相等的值设置为控制
|
|
|
- Object obj = null;
|
|
|
- writeMethod.invoke(entity, obj);
|
|
|
- columnVal.put(columnName, value);
|
|
|
- }
|
|
|
- return update(entity, columnVal);
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("update class" + entity.getClass() + " exception :" + e.getMessage());
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- default String getFieldName(SFunction<T, ?> column) {
|
|
|
- LambdaMeta meta = LambdaUtils.extract(column);
|
|
|
- String fieldName = PropertyNamer.methodToProperty(meta.getImplMethodName());
|
|
|
- return fieldName;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- default boolean update(T entity, SFunction<T, ?> column, Collection<?> coll) {
|
|
|
- UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
|
|
|
- // 这里使用反射得到字段,
|
|
|
- String conditions = columnToString(column);
|
|
|
- updateWrapper.in(conditions, coll);
|
|
|
- return SqlHelper.retBool(update(entity, updateWrapper));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- default boolean update(T entity, String conditions, Object... values) {
|
|
|
- UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
|
|
|
- updateWrapper.in(conditions, values);
|
|
|
- return SqlHelper.retBool(update(entity, updateWrapper));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 Wrapper,查询一条记录 <br/>
|
|
|
- * <p>
|
|
|
- * 结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default T selectOne(T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- return selectOne(queryWrapper, true);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 Wrapper 条件,查询总记录数
|
|
|
- *
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default long selectCount(T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- return SqlHelper.retCount(selectCount(queryWrapper));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询列表
|
|
|
- *
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default List<T> selectList(T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- return selectList(queryWrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询列表
|
|
|
- *
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default List<Map<String, Object>> selectMaps(T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- return selectMaps(queryWrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 Wrapper 条件,查询全部记录
|
|
|
- *
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default List<Object> listObjs(T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- return selectObjs(queryWrapper, Function.identity());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 Wrapper 条件,查询全部记录
|
|
|
- *
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- * @param mapper 转换函数
|
|
|
- */
|
|
|
- default <V> List<V> selectObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
|
|
|
- return selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 翻页查询
|
|
|
- *
|
|
|
- * @param page 翻页对象
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default <E extends IPage<T>> E selectPage(E page, T t, QueryMap... map) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t, map);
|
|
|
- try {
|
|
|
- return selectPage(page, queryWrapper);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 翻页查询
|
|
|
- *
|
|
|
- * @param page 翻页对象
|
|
|
- * @param queryWrapper 实体对象封装操作类
|
|
|
- * {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
|
|
- */
|
|
|
- default <E extends IPage<T>> E selectPage(E page, T t) {
|
|
|
- Wrapper<T> queryWrapper = QueryWrapperUtil.convertQuery(t);
|
|
|
- try {
|
|
|
- return selectPage(page, queryWrapper);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
- //select join
|
|
|
- /**
|
|
|
- * 查询对象 连表查询返回记录集合
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- * @param clazz resultType
|
|
|
- * @param objs 查询条件
|
|
|
- */
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据 Wrapper 条件,查询总记录数
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- */
|
|
|
- default Long selectJoinCount(MPJLambdaWrapper<T> wrapper, Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinCount(wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 连表查询返回一条记录
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- * @param clazz resultType
|
|
|
- */
|
|
|
- default <DTO> DTO selectJoinOne(Class<DTO> clazz, MPJLambdaWrapper<T> wrapper, Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinOne(clazz, wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 连表查询返回Map
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- */
|
|
|
- default Map<String, Object> selectJoinMap(MPJLambdaWrapper<T> wrapper, Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinMap(wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 连表查询返回记录集合
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- * @param clazz resultType
|
|
|
- */
|
|
|
- default <DTO> List<DTO> selectJoinList(Class<DTO> clazz, MPJLambdaWrapper<T> wrapper, Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinList(clazz, wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 连表查询返回Map集合
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- */
|
|
|
- default List<Map<String, Object>> selectJoinMaps(MPJLambdaWrapper<T> wrapper, Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinMaps(wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 连表查询返回记录集合并分页
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- * @param clazz resultType
|
|
|
- * @param <DTO> 分页返回对象
|
|
|
- */
|
|
|
- default <DTO, P extends IPage<DTO>> P selectJoinPage(P page, Class<DTO> clazz, MPJLambdaWrapper<T> wrapper,
|
|
|
- Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinPage(page, clazz, wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 连表查询返回Map集合并分页
|
|
|
- *
|
|
|
- * @param wrapper joinWrapper
|
|
|
- */
|
|
|
- default <P extends IPage<Map<String, Object>>> P selectJoinMapsPage(P page, MPJLambdaWrapper<T> wrapper,
|
|
|
- Object... param) {
|
|
|
- wrapper = QueryLambdaUtil.convertQuery(wrapper, param);
|
|
|
- return this.selectJoinMapsPage(page, wrapper);
|
|
|
- }
|
|
|
-}
|