gpt4 book ai didi

java - 有 2 个对象的PreparedStatement 中没有结果 "In"子句

转载 作者:行者123 更新时间:2023-12-02 03:30:39 24 4
gpt4 key购买 nike

我花了很多时间来寻找解决方案,但还是找不到。

我找到了一些关于如何将“IN”元素传递到PreparedStatement中的解决方案,但如果有任何其他解决方案,我很高兴看到,但是,无论如何我有

RowMapper<Map<BigInteger, Status>> mapper = new QueryDescriptionById.RowMapperDescription();

@Override
public Multimap<BigInteger, Status> findObject(BigInteger[] ids) {
StringBuilder builder = new StringBuilder();
for (Object id : ids) {
builder.append("?,");
}
String statement = SQL + builder.deleteCharAt(builder.length() - 1).toString() + ")";

Multimap<BigInteger, Status> multimap = ArrayListMultimap.create();
intoMap(multimap, getJdbcTemplate().query(statement, ps -> {
int index = 1;
for (BigInteger id : ids) {
ps.setInt(index++, id.intValue());
}
}, mapper));
return multimap;
}

private void intoMap(Multimap<BigInteger, Status> multimap, List<Map<BigInteger, Status>> list) {
list.forEach(map -> map.forEach((multimap::put)));
}

class RowMapperDescription implements RowMapper {
@Override
public Map<BigInteger, Status> mapRow(ResultSet rs, int rowNum) throws SQLException {
Status status = new Status();
Map<BigInteger, Status> map = new HashMap<>();
BigInteger id = rs.getBigDecimal("ID").toBigInteger();
BigInteger parentId = rs.getBigDecimal("PARENT_ID").toBigInteger();
status.setId(id);
status.setDescription(rs.getString("attempt_status_text"));
map.put(parentId, status);
return map;
}
}

但是当我只传入 BigInteger[] 1 个元素时它效果很好,但是当有 2 个或更多元素时它不会进入我的映射器。

为什么?

附注

我尝试使用 ResultSet 来代替 RowMapper

return getJdbcTemplate().query(SQL, ids, resultSet -> {
Multimap<BigInteger, Status> multimap = ArrayListMultimap.create();
int rowNum = 0;
while (resultSet.next()) {
System.out.println("rownum = " + rowNum);
mapper.mapRow(resultSet, rowNum++).forEach(multimap::put);
}
return multimap;
})

但是控制台中没有任何行,这意味着 resultSet 没有 next()

附注

我的SQL 很难,所以我只放了一个简短的版本。看起来像

private String SQL = "select el1,el2 from mytable where el3 in(";

我尝试过使用 in(";in :ID 以及 in = :IDin (:ID )in (?) 以及其他一些我不记得的变体 (-:

已编辑

其实我不明白,为什么会这样

public QueryDescriptionBySmsId(DataSource dataSource) {
super(dataSource, SQL);
logger.debug("Created QueryStatus");
declareParameter(new SqlParameter("ID", Types.NUMERIC));
compile();
logger.debug("Created QueryStatus");
}

@Override
public Multimap<BigInteger, Status> query(Set<BigInteger> ids) {
Map<String, Set> paramMap = Collections.singletonMap("ID", smsIds);
List l = executeByNamedParam(paramMap); // it returns a lot of elements which I am looking for
}

但这行不通

 @Override
public Multimap<BigInteger, SmsStatus> query(Set<BigInteger> sids) {

Map<String, Set> paramMap = Collections.singletonMap("ID", ids);
return getJdbcTemplate().query(SQL, resultSet -> {
int rowNum = 0;
Multimap<BigInteger, SmsStatus> multimap =
ArrayListMultimap.create();
while (resultSet.next()) {
mapper.mapRow(resultSet, rowNum++).forEach(multimap::put);
}
return multimap;
},paramMap);
}

显示错误

SQL state [99999]; error code [17004]; Invalid column type

最佳答案

我已经像这样更改了我的代码,现在它可以工作了,但我不喜欢这个解决方案

public class QueryDescriptionById extends SqlQuery implements IQueryDescriptionById {

RowMapper<Map<BigInteger, Status>> mapper = new RowMapperDescription();

public QueryDescriptionById(DataSource dataSource) {
super(dataSource, SQL);
}

public Multimap<BigInteger, SmsStatus> query(Set<BigInteger> ids) {

Map<String, Set> paramMap = Collections.singletonMap("ID", ids);
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(getJdbcTemplate().getDataSource());

return template.query(SQL, paramMap, resultSet -> {
int rowNum = 0;
Multimap<BigInteger, Status> multimap = ArrayListMultimap.create();
while (resultSet.next()) {
mapper.mapRow(resultSet, rowNum++).forEach(multimap::put);
}
return multimap;
});
}
.........
.........
.........
}

关于java - 有 2 个对象的PreparedStatement 中没有结果 "In"子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56887772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com