[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder

request

sample

Suppose you have the following table "shops"

shop_cd name
0001 Main store
0002 Dictionary store
0003 Bunko store
0004 Picture book store

I want a list of stores by specifying WHERE shop_cd IN (0001, 0002).

Class that holds data

@Getter
@Setter
public class Shop {
  private String shopCd;
  private String name;
}

Mapper

public interface ShopMapper {
  /**
   *Receives a list of store codes and returns a list of stores
   */
  @SelectProvider(type = SqlProvider.class, method = "selectByShopCodes")
  List<Shop> selectByShopCodes(List<String> shopCodes);

  class SqlProvider {
    public String selectByShopCodes(List<String> shopCodes) {
      final String inParams = getInPhraseParamString(shopCodes, "shopCodes");
      SQL sql = new SQL() {
        {
          SELECT("*");
          FROM("shops");
          WHERE(String.format("shops.shop_cd IN (%s)", inParams));
          ORDER_BY("shop_cd ASC");
        }
      };
      return sql.toString();
    }

    /**
     *Receive the list and IN()Returns the parameter string used in the clause
     *Example: #{shopCodes[0]}, #{shopCodes[1]}, #{shopCodes[2]}
     */
    private String getInPhraseParamString(List<String> values, String paramName) {
      String paramString = "";
      int i = 0;
      for (String value : values) {
        if (i != 0) {
          paramString += ",";
        }
        paramString += String.format("#{%s[%s]}", paramName ,i);
        i++;
      }
      return paramString;
    }
  }
}

Tips

Recommended Posts

[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
[JAVA] [Spring] [MyBatis] Use GROUP BY in SQL Builder
Use inequality comparison operators in MyBatis SQL
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Use Interceptor in Spring
Use OpenCV in Java
Use PreparedStatement in Java
Encode query parameters with Uri Components Builder in spring
Use Lambda Layers with Java
Java Config with Spring MVC
Using Mapper with Java (Spring)
Use Spring JDBC with Spring Boot
Use Redis Stream in Java
Use SpatiaLite with Java / JDBC
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
Use thymeleaf3 with parent without specifying spring-boot-starter-parent in Spring Boot
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
How to use Lombok in Spring
Use Basic Authentication with Spring Boot
Morphological analysis in Java with Kuromoji
Use constructor with arguments in cucumber-picocontainer
Use Microsoft Graph with standard Java
Let's use Twilio in Java! (Introduction)
Use Azure Bing SpellCheck with Java
Use JDBC with Java and Scala.
[Java] Do not use "+" in append!
Use composite keys in Java Map.
Use Java 11 with Google Cloud Functions
How to use classes in Java?
Beginning with Spring Boot 0. Use Spring CLI
Play with Markdown in Java flexmark-java
Do you use Stream in Java?
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Multilingual Locale in Java How to use Locale
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
Concurrency Method in Java with basic example
Create Java Spring Boot project in IntelliJ
Use "Rhino" which runs JavaScript in Java
Use DynamoDB query method in Spring Boot
[JaCoCo (Java Code Coverage)] Use with NetBeans
Read xlsx file in Java with Selenium
Spring Java
Split a string with ". (Dot)" in Java
Use Spring Security JSP tags with FreeMarker
Working with huge JSON in Java Lambda
[Java Spring MVC] I want to use DI in my own class
[* Java *] I participated in JJUG CCC 2019 Spring
Use cache with EhCashe 2.x with Spring Boot
Use JDBC Manager with the settings in jdbc.dicon.
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Read a string in a PDF file with Java
Create a CSR with extended information in Java
Refactored GUI tools made with Java8 + JavaFX in 2016
Use OpenCV_Contrib (ArUco) in Java! (Part 1-Build) (OpenCV-3.4.4)
How to use built-in h2db with spring boot
With Tomcat you can use placeholders ($ {...}) in web.xml
Tests with @Parameters in Spring 4.3 should have no parameters
How to use Java framework with AWS Lambda! ??