以下查询工作正常,没有任何问题,因为它只有一列,我可以轻松地检查它是否不为null并继续。
String query = "SELECT * FROM test WHERE ID=?";
Test test = template.queryForObject(query,new Object[]{id},
new BeanPropertyRowMapper<>(Test.class));
我正在构建一个网页,并且有一个要求,我需要根据用户输入生成不具有空列的动态queryForObject,我该怎么做?
例如在我的网页中,如果我有输入字段,如id,name和薪金,并且如果用户仅输入name和ID而未输入薪金数据,则它应在执行语句下方动态生成
String query = "SELECT * FROM test WHERE ID=? and name=?" ;
Test test = template.queryForObject(query,new Object[]{id, name},
new BeanPropertyRowMapper<>(Test.class));
答案 0 :(得分:0)
这是您要找的东西吗?快速而肮脏的方式。
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class TestMe {
private static DataSource dataSource = null;//@TODO please create datasource
private static NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
public static void main(String[] args) {
String criterias = "id=123";
//criterias = "id=123,name=test;
//criterias = "id=123,name=test, salary=100.00";
final List<String> criteriaList = createQeuryCriteriaList(criterias);
Test test = getTest(criteriaList);
System.out.println(test);
}
public static List<String> createQeuryCriteriaList(final String criterias) {
final List<String> queryCriteriaList = new ArrayList<>();
if (StringUtils.isNotEmpty(criterias)) {
final String qryCriterias = criterias.replaceAll("[{}]", "");
for (String criteria : qryCriterias.split(",")) {
queryCriteriaList.add(criteria);
}
}
return queryCriteriaList;
}
public static Test getTest(final List<String> criteriaList) {
try {
final MapSqlParameterSource namedParameters = new MapSqlParameterSource();
final String qry = addCriteriasToQuery("SELECT * FROM test WHERE 1=1 ", criteriaList, namedParameters);
return jdbcTemplate.queryForObject(qry, namedParameters, (resultSet, i) ->
new Test(resultSet.getLong("id"), resultSet.getString("name"),
resultSet.getBigDecimal("salary"))
);
} catch (EmptyResultDataAccessException emptyExp) {
return null;
}
}
private static String addCriteriasToQuery(final String query, final List<String> criteriaList,
final MapSqlParameterSource namedParameters) {
if (CollectionUtils.isEmpty(criteriaList)) {
return query;
}
StringBuilder qryCriteria = new StringBuilder(query);
for (String criteria : criteriaList) {
final String[] keyValue = criteria.split("=");
if (keyValue.length < 2) {
continue;
}
final String key = keyValue[0].trim();
final String value = keyValue[1];
switch (key) {
case "id":
qryCriteria.append(" AND id=:id");
namedParameters.addValue("id", value);
break;
case "name":
qryCriteria.append(" AND name=:name");
namedParameters.addValue("name", value);
break;
case "salary":
qryCriteria.append(" AND salary=:salary");
namedParameters.addValue("salary", value);
break;
default:
System.out.println(String.format("Unknown criteria, Key:%s, Value:%s", key, value));
}
}
return qryCriteria.toString();
}
static class Test {
Long id;
String name;
BigDecimal salary;
public Test(Long id, String name, BigDecimal salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
}
}
注意:这只是示例代码,可能有编译或其他问题。