How to use a list of string in NamedParameterJDBCTemplate to get results

前端 未结 2 559
南方客
南方客 2020-12-31 00:00

Experimenting with Spring-JDBC. I am using this as reference. I am trying to get a list of actors who have the same last name. Running this code gave me the desired results:

相关标签:
2条回答
  • 2020-12-31 00:31

    Use IN Clause..

    How to use SELECT IN clause in JDBCTemplates?

    List<String> lastnames= new ArrayList<>();
    
    Map namedParameters = Collections.singletonMap("lastnamevalues", lastnames);
    
    StringBuffer recordQueryString = new StringBuffer();
    
    recordQueryString.append("select FIRSTNAME, LASTNAME from ACTORS where lastname in (:lastnamevalues)");
    
    List nameInvolvements = this.namedparameterJdbcTemplate.query(recordQueryString.toString(), namedParameters, new MyMapper());
    
    0 讨论(0)
  • 2020-12-31 00:33

    You can also use MapSqlParameterSource

    String query = "SELECT FIRSTNAME FROM ACTORS WHERE LASTNAME in (:LASTNAME)";
    Set<String> ids = ....;
    
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("LASTNAME", ids);
    
    this.namedparameterJdbcTemplate.query(query, parameters);
    
    0 讨论(0)
提交回复
热议问题