spring jdbctemplate 插入数据

手动拼装的sql : insert into tableA (id,name) values(:id,:name)

使用BeanPropertySqlParameterSource

执行:jdbcTemplate.update(sql, new BeanPropertySqlParameterSource(vo));
出问题了,
我跟代码进去 发现new BeanPropertySqlParameterSource(vo) 所有集合是空的,但是我的vo对象是有值的

-------------源码-------------
public int insertVo(SuperVo vo) throws Exception {
StringBuffer insert = new StringBuffer(" insert into ");
StringBuffer insert_value = new StringBuffer(" values ( ");
insert.append(vo.getTableName()).append(" ( ");

    // 主键/序列
    String pkFieldName = vo.getPKFieldName();
    String sequence = vo.getSequence();
    Integer seq = null;
    if (pkFieldName != null) {
        if (sequence != null && null == vo.getAttributeValue(vo.getPKFieldName())) {
            seq = this.queryNextSeq(sequence);
            vo.setAttributeValue(pkFieldName, seq);
        } else {
            seq = Util.getIntegerValue(vo.getAttributeValue(vo.getPKFieldName()));
        }
    }

    List<String> list = vo.getAttributeNames();
    int[] types = new int[list.size()];
    Object[] params = new Object[list.size()];
    String split = "";
    for (int i = 0; i < list.size(); i++) {
        String fieldName = list.get(i);
        insert.append(split).append(fieldName);
        insert_value.append(split).append(":").append(fieldName);
        split = ",";
    }
    insert.append(")");
    insert_value.append(")");

    SqlParameterSource paramSource = new BeanPropertySqlParameterSource(vo);
    String sql = insert.append(insert_value).toString();
    jdbcTemplate.update(sql, new BeanPropertySqlParameterSource(vo));


    }
 ![图片说明](https://img-ask.csdn.net/upload/201505/26/1432611494_91042.jpg)

![图片说明](https://img-ask.csdn.net/upload/201505/26/1432611544_196163.jpg)

查看全部
u013890437
茂盛的小绿芽
2015/05/26 03:39
  • spring
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复