select * from table where (cloum1,cloum2) in (( 'A017', '400000001644'), ('A017', '400000001641')) or cloum1 in ('000000009247','000000009247'); 使用jdbctemplate如何查询
时间: 2023-08-20 18:32:34 浏览: 46
你可以使用JdbcTemplate的query方法来执行这个SQL查询。下面是一个简单的示例代码:
```
String sql = "select * from table where (cloum1,cloum2) in ((?, ?), (?, ?)) or cloum1 in (?, ?)";
List<Object> params = Arrays.asList("A017", "400000001644", "A017", "400000001641", "000000009247", "000000009247");
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, params.toArray());
```
这里使用了一个带有占位符的SQL语句来执行查询,并将查询参数传递给query方法。最终,该方法将返回一个包含查询结果的列表,每个结果都是一个Map对象,其中包含列名和对应的值。
相关问题
select * from table where (cloum1,cloum2) in (( 'A017', '400000001644'), ('A017', '400000001641')) or cloum1 in ('000000009247','000000009247'); in的参数个数不固定 如何使用jdbctemplate如何查询
如果`in`语句的参数个数不固定,你可以使用JdbcTemplate的`query`方法和`PreparedStatementSetter`接口来动态设置查询参数。下面是一个示例代码:
```
String sql = "select * from table where (cloum1,cloum2) in " +
"(?, ?)" + ", ".repeat(cloumPairs.length - 1) + "or cloum1 in " +
"(?" + ", ?".repeat(cloum1s.length - 1) + ")";
List<Object> params = new ArrayList<>();
for (String[] cloumPair : cloumPairs) {
params.addAll(Arrays.asList(cloumPair));
}
params.addAll(Arrays.asList(cloum1s));
jdbcTemplate.query(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
int i = 1;
for (Object param : params) {
ps.setObject(i++, param);
}
}
}, new RowMapper<Map<String, Object>>() {
@Override
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
ResultSetMetaData meta = rs.getMetaData();
Map<String, Object> row = new HashMap<>();
for (int i = 1; i <= meta.getColumnCount(); i++) {
row.put(meta.getColumnName(i), rs.getObject(i));
}
return row;
}
});
```
这里使用了一个动态生成的SQL语句来执行查询,并使用`PreparedStatementSetter`接口设置查询参数。最终,该方法将返回一个包含查询结果的列表,每个结果都是一个Map对象,其中包含列名和对应的值。
select * from A where (cloum<>'1' or cloum is null)使用tp5如何书写
在 ThinkPHP 5 中,可以通过以下方式书写该 SQL 语句:
```
Db::table('A')
->where('cloum', '<>', '1')
->whereOr('cloum', null)
->select();
```
其中,`table` 方法用于指定要操作的数据表,`where` 方法用于添加 WHERE 条件,`whereOr` 方法用于添加 OR 条件。在 `where` 方法中,第一个参数是要查询的字段名,第二个参数是操作符,第三个参数是要查询的值。当操作符为 `<>` 时,可以使用 `null` 来查询空值。最后,使用 `select` 方法执行查询并返回结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rp](https://img-home.csdnimg.cn/images/20210720083646.png)