本文介绍了如何使用NamedParameterJDBCTemplate中的字符串列表来获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用Spring-JDBC。我使用作为参考。我正在尝试获得具有相同姓氏的演员列表。运行此代码可以获得所需的结果:
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);
}
我有一个 List< String>
姓氏。如何获得具有列表的演员列表?我是否遍历姓氏列表并每次调用 getActorsWithSameLastName()
或者spring是否提供了迭代方式并为我提取结果的方法?请指教。
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.
推荐答案
使用IN子句..
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());
这篇关于如何使用NamedParameterJDBCTemplate中的字符串列表来获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!