自定义joinQueryWrapper实现
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
package com.czg.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
|
import com.mybatisflex.core.query.*;
|
||||||
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
|
import com.mybatisflex.core.util.LambdaGetter;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.lang.invoke.SerializedLambda;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.util.LambdaUtil.getFieldName;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联查询的queryWrapper
|
||||||
|
* @author Administrator
|
||||||
|
*/
|
||||||
|
public class JoinQueryWrapper extends QueryWrapper {
|
||||||
|
public JoinQueryWrapper() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SerializedLambda getSerializedLambda(Serializable getter) {
|
||||||
|
try {
|
||||||
|
Method method = getter.getClass().getDeclaredMethod("writeReplace");
|
||||||
|
method.setAccessible(Boolean.TRUE);
|
||||||
|
return (SerializedLambda) method.invoke(getter);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getImplClassName(SerializedLambda lambda) {
|
||||||
|
String type = lambda.getInstantiatedMethodType();
|
||||||
|
return type.substring(2, type.indexOf(";"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class<?> getImplClass0(SerializedLambda lambda) {
|
||||||
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
String implClass = getImplClassName(lambda);
|
||||||
|
try {
|
||||||
|
return Class.forName(implClass.replace("/", "."), true, classLoader);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw FlexExceptions.wrap(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> String getColum(LambdaGetter<T> column) {
|
||||||
|
SerializedLambda lambda = getSerializedLambda(column);
|
||||||
|
Class<?> entityClass = getImplClass0(lambda);
|
||||||
|
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
|
||||||
|
return tableInfo.getTableName() + "." + StrUtil.toUnderlineCase(getFieldName(column));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> JoinQueryWrapper eq(LambdaGetter<T> column, Object value) {
|
||||||
|
eq(getColum(column), value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> JoinQueryWrapper orderBy(LambdaGetter<T> column, Boolean asc) {
|
||||||
|
orderBy(getColum(column), asc);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,5 +13,5 @@ import com.mybatisflex.core.paginate.Page;
|
|||||||
*/
|
*/
|
||||||
public interface PadProductCategoryDetailMapper extends BaseMapper<PadProductCategoryDetail> {
|
public interface PadProductCategoryDetailMapper extends BaseMapper<PadProductCategoryDetail> {
|
||||||
Page<PadProductCategoryDTO> selectPageByKeyAndShopId();
|
Page<PadProductCategoryDTO> selectPageByKeyAndShopId();
|
||||||
Page<PadProductCategoryDTO> selectPageByKeyAndShopId_COUNT();
|
long selectPageByKeyAndShopId_COUNT();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ import com.czg.account.service.*;
|
|||||||
import com.czg.enums.StatusEnum;
|
import com.czg.enums.StatusEnum;
|
||||||
import com.czg.exception.ApiNotPrintException;
|
import com.czg.exception.ApiNotPrintException;
|
||||||
import com.czg.service.account.mapper.PadProductCategoryDetailMapper;
|
import com.czg.service.account.mapper.PadProductCategoryDetailMapper;
|
||||||
|
import com.czg.utils.JoinQueryWrapper;
|
||||||
import com.mybatisflex.core.paginate.Page;
|
import com.mybatisflex.core.paginate.Page;
|
||||||
|
import com.mybatisflex.core.query.QueryColumn;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import com.mybatisflex.core.util.LambdaUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -32,12 +35,17 @@ public class PadProdServiceImpl implements PadProdService {
|
|||||||
@Resource
|
@Resource
|
||||||
private PadProductCategoryDetailService padProductCategoryDetailService;
|
private PadProductCategoryDetailService padProductCategoryDetailService;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
LambdaUtil.getQueryColumn(SysUser::getId).getName();
|
||||||
|
// new QueryWrapper().eq()
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public Page<PadProductCategoryDTO> pageInfo(Page<PadProductCategoryDTO> objectPage, Long productCategoryId, Long shopId) {
|
public Page<PadProductCategoryDTO> pageInfo(Page<PadProductCategoryDTO> objectPage, Long productCategoryId, Long shopId) {
|
||||||
QueryWrapper queryWrapper = new QueryWrapper().eq(PadProductCategoryDetail::getPadProductCategoryId, shopId);
|
JoinQueryWrapper queryWrapper = new JoinQueryWrapper().eq(PadProductCategory::getShopId, shopId);
|
||||||
if (productCategoryId != null) {
|
if (productCategoryId != null) {
|
||||||
queryWrapper.eq(PadProductCategoryDetail::getPadProductCategoryId, productCategoryId);
|
queryWrapper.eq(PadProductCategory::getId, productCategoryId);
|
||||||
}
|
}
|
||||||
|
queryWrapper.orderBy(PadProductCategory::getSort, true).orderBy(PadProductCategory::getId, false);
|
||||||
return padProductCategoryDetailMapper.xmlPaginate("selectPageByKeyAndShopId", objectPage, queryWrapper);
|
return padProductCategoryDetailMapper.xmlPaginate("selectPageByKeyAndShopId", objectPage, queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,6 +80,7 @@ public class PadProdServiceImpl implements PadProdService {
|
|||||||
|
|
||||||
PadProductCategory padProductCategory = new PadProductCategory().setPadLayoutId(padDetailAddDTO.getPadLayoutId()).setProductCategoryId(padDetailAddDTO.getProductCategoryId())
|
PadProductCategory padProductCategory = new PadProductCategory().setPadLayoutId(padDetailAddDTO.getPadLayoutId()).setProductCategoryId(padDetailAddDTO.getProductCategoryId())
|
||||||
.setShopId(shopId);
|
.setShopId(shopId);
|
||||||
|
padProductCategory.setSort(0);
|
||||||
padProductCategoryService.save(padProductCategory);
|
padProductCategoryService.save(padProductCategory);
|
||||||
|
|
||||||
ArrayList<PadProductCategoryDetail> details = new ArrayList<>();
|
ArrayList<PadProductCategoryDetail> details = new ArrayList<>();
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
${qwSql}
|
${qwSql}
|
||||||
limit ${pageOffset}, ${pageSize}
|
limit ${pageOffset}, ${pageSize}
|
||||||
</select>
|
</select>
|
||||||
<select id="selectPageByKeyAndShopId_COUNT" resultType="com.czg.account.dto.pad.PadProductCategoryDTO">
|
<select id="selectPageByKeyAndShopId_COUNT" resultType="java.lang.Long">
|
||||||
SELECT count(1)
|
SELECT count(1)
|
||||||
FROM tb_pad_product_category
|
FROM tb_pad_product_category
|
||||||
LEFT JOIN tb_pad_product_category_detail
|
LEFT JOIN tb_pad_product_category_detail
|
||||||
|
|||||||
Reference in New Issue
Block a user