首页 > 其他分享 >test

test

时间:2024-11-05 21:34:55浏览次数:2  
标签:queryWrapper LambdaEsQueryWrapper String field test import public

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@RestController
@RequestMapping("/api/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @GetMapping("/search")
    public List<YourEntityClass> search(
            @RequestParam String keyword,
            @RequestParam String value) {

        // 调用 Service 查询方法
        return personService.searchByKeyword(keyword, value);
    }
}

import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class PersonService {

    @Autowired
    private YourEntityClassMapper yourEsMapper;

    private final QueryStrategyFactory<YourEntityClass> strategyFactory = new QueryStrategyFactory<>();

    public List<YourEntityClass> searchByKeyword(String keyword, String value) {
        EsQueryBuilder<YourEntityClass> builder = new EsQueryBuilder<>(YourEntityClass.class);

        // 获取通用查询策略
        QueryStrategy<YourEntityClass> strategy = strategyFactory.getStrategy(keyword);

        // 应用策略
        strategy.apply(builder.getQueryWrapper(), value);

        // 执行查询
        return yourEsMapper.selectList(builder.build());
    }
}


import com.xpc.easyes.core.conditions.LambdaEsQueryWrapper;

public class FieldQueryStrategy<T> implements QueryStrategy<T> {
    private final String field;

    public FieldQueryStrategy(String field) {
        this.field = field;
    }

    @Override
    public void apply(LambdaEsQueryWrapper<T> queryWrapper, String value) {
        if (field.contains(".")) {
            // 如果字段是嵌套字段,例如 "list.name"
            String[] parts = field.split("\\.");
            String nestedPath = parts[0];
            String nestedField = parts[1];
            queryWrapper.nested(nestedPath, w -> w.eq(nestedPath + "." + nestedField, value));
        } else {
            // 普通字段查询
            queryWrapper.eq(field, value);
        }
    }
}

import java.util.HashMap;
import java.util.Map;

public class QueryStrategyFactory<T> {

    private final Map<String, QueryStrategy<T>> strategies = new HashMap<>();

    public QueryStrategyFactory() {
        // 注册字段对应的通用策略
        registerField("name");
        registerField("list.name");
        // 可以在此添加其他字段
    }

    private void registerField(String field) {
        strategies.put(field, new FieldQueryStrategy<>(field));
    }

    public QueryStrategy<T> getStrategy(String keyword) {
        QueryStrategy<T> strategy = strategies.get(keyword);
        if (strategy == null) {
            throw new IllegalArgumentException("Invalid keyword: " + keyword);
        }
        return strategy;
    }
}

import com.xpc.easyes.core.conditions.LambdaEsQueryWrapper;
import com.xpc.easyes.core.core.EsWrappers;

public class EsQueryBuilder<T> {

    private final LambdaEsQueryWrapper<T> queryWrapper;

    public EsQueryBuilder(Class<T> clazz) {
        this.queryWrapper = EsWrappers.lambdaQuery(clazz);
    }

    public LambdaEsQueryWrapper<T> getQueryWrapper() {
        return queryWrapper;
    }

    public LambdaEsQueryWrapper<T> build() {
        return queryWrapper;
    }
}


import com.xpc.easyes.core.conditions.LambdaEsQueryWrapper;

public interface QueryStrategy<T> {
    void apply(LambdaEsQueryWrapper<T> queryWrapper, String value);
}

  

标签:queryWrapper,LambdaEsQueryWrapper,String,field,test,import,public
From: https://www.cnblogs.com/findlisa/p/18528899

相关文章

  • AtCoder Beginner Contest 378 E
    https://atcoder.jp/contests/abc378/tasks/abc378_ehttps://atcoder.jp/contests/abc378/editorial/11300#include<bits/stdc++.h>#definexfirst#defineysecond#defineall(x)(x).begin(),(x).end()#definelowbit(x)(x)&-(x)usingnamespacestd;ty......
  • Docker安装MongoDB详解(mongo.latest)
    一、MongoDB介绍MongoDB是一种基于分布式文件存储的数据库,使用C++语言开发,旨在为Web应用提供可扩展且高性能的数据存储解决方案。作为一种介于关系数据库和非关系数据库之间的技术,MongoDB具有强大的功能和高效的性能,特别适用于处理海量的非结构化数据。MongoDB的核心概念与特......
  • AtCoder Beginner Contest 378
    AtCoderBeginnerContest378总结A直接模拟,存\(1\)到\(4\)出现个数。#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map&g......
  • 2024.11.4 test
    B你可以进行以下的操作:选择一个点染白色;此后每次染有白色点相邻的,且\(a_i\)最小的点。\(q\)次询问每次给出\(p,k\),问有多少种选择点的方案,使得\(p\)是第\(k\)个选到的。\(a_i\)是排列。\(n,q\le1e5\)。设\(l=p-k+1,r=p+k-1\),若\([l,p-1]\)能取到且\(a_p<a_{l-1}......
  • FontDialogTest自定义字体对话框的使用
    packagecom.shrimpking.t1;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;/***CreatedbyIntelliJIDEA.**@Author:Shrimpking*@create2024/11/310:44*/publicclassFontDial......
  • 2024.11.3 test
    BP6563[SBCOI2020]一直在你身旁,\(n\le10^5\),\(c_i\le9\)。考虑利用\(c_i\le9\)的性质,那么最后答案很小。我们原本是计算每个区间的答案,同时区间答案具有单调性,那么考虑把答案放进状态里即可。即维护\(f_{l,ans}\)表示花费\(ans\)的代价能确定的最远的\(r\)。C请......