建议在Spring中调用存储过程的泛化代码

时间:2010-10-28 12:27:48

标签: spring stored-procedures

public class TestProcedure extends StoredProcedure {

TestProcedure(BasicDataSource dataSource, String procName) {
    this.setDataSource(dataSource);
    this.setSql(procName);
    this.declareParameter(new SqlParameter("@test", Types.INTEGER));
}

public List<Detail> executeProcedure(Integer studentId) {
    Map inParams = new HashMap();
    inParams.put("@test", studentId);
    this.compile();
    List<Detail> details = new ArrayList<Detail>();
    try {
        ArrayList<LinkedHashMap<String, Object>> list = (ArrayList<LinkedHashMap<String, Object>>) execute(inParams).get("#result-set-1");
        for (LinkedHashMap<String, Object> linkedHashMap : list) {
            Detail detail = new Detail();
            detail.setStudent_id((Integer) linkedHashMap.get("student_id"));
            detail.setStudent_name((String) linkedHashMap.get("student_name"));
            detail.setStudent_marks((Double) linkedHashMap.get("student_marks"));
            details.add(detail);
        }
    } catch (Exception e) {
        System.out.println("Error Man : " + e);
    }
    return details;
}

}

上面是通过Spring调用SP的代码任何人都可以建议它的一般结构。一些抽象类或接口....

1 个答案:

答案 0 :(得分:0)

查看JPA,iBatis或Hibernate。所有这些都是包含在Spring中的实现的ORM数据访问包。我使用iBatis,但我确定3个中的任何一个都很好