简体   繁体   English

如何在 spring jdbc 中使用 IN 子句

[英]how to use IN Clause in spring jdbc

How to use IN clause in spring jdbc template or how to filter out all values which are not matching to string when I query db I can get all values using stream API is it possible to skip add only financialTx.getActivationCode() matches to VPMA and OLAM How to use IN clause in spring jdbc template or how to filter out all values which are not matching to string when I query db I can get all values using stream API is it possible to skip add only financialTx.getActivationCode() matches to VPMA and奥兰

Map<String, Object> params = new HashMap<String, Object>();
        List<String> activationCode = new ArrayList<String>();
        activationCode.add("VPMA");
        activationCode.add("OLAM");
        Map activationCodes = Collections.singletonMap(activationCode, "activationCode");
        params.put("userEmail", userEmail);
        params.put("activationCode", activationCodes);
        LocalDateTime time = asOf.minusDays(psValidationDays);
        params.put("time", Timestamp.valueOf(time));
        String sql =
                "select "
                        + "fx.date_created,"
                        + "fx.balance,"
                        + "au.email,"
                        + "l.code "
                        + "from financial_tx fx "
                        + "inner join altpay_user au on "
                        + "au.id = fx.altpay_user_id "
                        + "inner join lookup l on "
                        + "l.id = fx.tx_type_id "
                        + "where au.email in (:userEmail) and "
                        + "l.code in (:activationCode) and "
                        + "(fx.date_created  >= :time) order by date_created desc";
        List<FinancialTx> financialTxList =
                namedParameterJdbcTemplate.query(sql, params, financialTxrowMapper);
        /*  final Iterator<FinancialTx> iterator = financialTxList.iterator();
           while (iterator.hasNext()) {
                    final FinancialTx financialTx = iterator.next();
                    if (financialTx.getActivationCode().equals("VPMA")) {
                        iterator.remove();
                    }
        */
        return financialTxList;
    }

In your params map you can make the values of type Collection so they get searched using IN .在您的params map 中,您可以设置Collection类型的值,以便使用IN搜索它们。 Looking at your SQL you want to do this for userEmail and activationCode .查看您的 SQL 您想为userEmailactivationCode执行此操作。 So you would set them up in params like this:所以你可以像这样在参数中设置它们:

params.put("userEmail", Lists.newArrayList(userEmail));//create List using Google guava, feel free to use whatever
params.put("activationCode", activationCode);//i.e. no need to add to a Map

Bear in mind you haven't instantiated userEmail is not instantiated in your snippet of code so I assume it is accessed from somewhere else.请记住,您尚未实例化userEmail未在您的代码片段中实例化,因此我假设它是从其他地方访问的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM