How to generate a dynamic “in (…)” sql list through Spring JdbcTemplate?

前端 未结 3 879
自闭症患者
自闭症患者 2020-12-01 06:23

Is it possible to generate arbitrary \"in ()\" lists in a SQL query through Jdbc template:

example:

\"select * from t where c in (#)\" , However \'#\' could

3条回答
  •  有刺的猬
    2020-12-01 06:47

    SimpleJDBCTemplate is depricated now. You can use NamedParameterJdbcTemplate instead. Sample code is below. If you have multiple parameters of different kind you can use Object as key, otherwise use your List

    String sqlAllEmpl = queryLoader.getProperty("allEmployeesByLevelAndPeriod");
            Map paramMap = new HashMap();
            paramMap.put("level", levelStr);
            paramMap.put("periodList", periodList);
    
            gridList = namedParameterJdbcTemplate.query(sqlAllEmpl, paramMap, new YourRowMapper());
    

    your sqlAllEmpl will have two place holders, level - string and periodList - which is a list used in the IN statement of sql.

提交回复
热议问题