我试图插入一个简单的基础,其中Spring JdbcTemplate
使用MapSqlParamaterSource
映射参数查询,并将错误视为以下数据:
public void adiciona(Conta conta) {
String sql = "insert into contas (descricao, paga, valor, tipo) values (:descricao,:paga,:valor,:tipo)";
MapSqlParameterSource pss = new MapSqlParameterSource();
pss.addValue("descricao", conta.getDescricao());
pss.addValue("paga", "true");
pss.addValue("valor", conta.getValor());
pss.addValue("tipo", conta.getTipo());
getJdbcTemplate().update(sql, pss);
}
错误日志:
mar 22, 2015 12:16:00 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [spring mvc] in context with path [/contas] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [insert into contas (descricao, paga, valor, tipo) values (:descricao,:paga,:valor,:tipo)]; Invalid argument value: java.io.NotSerializableException; nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException] with root cause
java.io.NotSerializableException: org.springframework.jdbc.core.namedparam.MapSqlParameterSource
Java Bean:
public class Conta implements Serializable {
private static final long serialVersionUID = 4678852901357132238L;
private Long id;
private String descricao;
private boolean paga;
private double valor;
private Calendar dataPagamento;
private TipoDaConta tipo;
// getters and settes
有人可以告诉我如何解决这个问题吗?
答案 0 :(得分:1)
我也面临同样的问题,我通过改变它来解决它
this.getJdbcTemplate().update(query,mapSqlParameterSource);
到
this.getNamedParameterJdbcTemplate().update(query,mapSqlParameterSource);
答案 1 :(得分:0)
尝试类似:
this.getJdbcTemplate().update(updateStatement, new Object[] {conta.getDescricao(), "true", conta.getValor(), conta.getTipo()});