Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

public List<String> getActorsWithSameLastName(String lastName,             NamedParameterJdbcTemplate template) {         String query = "SELECT FIRSTNAME FROM ACTORS WHERE LASTNAME=:LASTNAME";         Map<String, String> paramMap = new HashMap<String, String>();         paramMap.put("LASTNAME", lastName);         return template.queryForList(query, paramMap, String.class);     } 

I have a List<String> of last names. How can I get a List of actors with the list that I have? Do I iterate over the list of last names and call the getActorsWithSameLastName() everytime or does spring provide a way where it does the iteration and fetches the result for me? Please advice.

like image 715
Mono Jamoon Avatar asked Jan 21 '13 06:01

Mono Jamoon


People also ask

How do you use a results set extractor?

ResultSetExtractor interface is a callback interface used by JdbcTemplate's query methods. Implementations of this interface perform the actual work of extracting results from a ResultSet, but don't need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate.

What is the difference between JdbcTemplate and NamedParameterJdbcTemplate?

Spring - NamedParameterJdbcTemplate Example Functionally, there's no difference between Spring's JdbcTemplate and it's variant, NamedParameterJdbcTemplate except for : NamedParameterJdbcTemplate provides a better approach for assigning sql dynamic parameters instead of using multiple '?' in the statement.

How do you use RowMapper?

Usage. Step 1 − Create a JdbcTemplate object using a configured datasource. Step 2 − Create a StudentMapper object implementing RowMapper interface. Step 3 − Use JdbcTemplate object methods to make database operations while using StudentMapper object.


2 Answers

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()); 
like image 154
Shashi Avatar answered Oct 27 '22 00:10

Shashi


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); 
like image 31
Jayamohan Avatar answered Oct 27 '22 00:10

Jayamohan