[使用jdbctemplate在模型中保存查询的数据

问题描述 投票:0回答:1

下午好,我有一个查询,如何使用jdbctemplate将查询的值保存在模型或类中。

这是我的代码:

RequestMapping(value= "/formobligatorio/{pidm}" , method = RequestMethod.GET)
    public List<FormPerson> grupoCodf(@PathVariable Long pidm) {
      int codf = 0;
      LinkedList<Integer> codFormsOblig = new LinkedList<Integer>();
        List<Integer> codfoblig = new ArrayList<Integer>();
        String query=" select p.codigo_uzgtformularios from UTIC.UZGTFORMULARIO_PERSONA p,UTIC.UZGTFORMULARIOS f where p.spriden_pidm =" + pidm + "and p.codigo_uzgtformularios = f.codigo_uzgtformularios and (  p.uzgtformularios_estado_llenado ='N' or f.uzgtformularios_estado_llenado ='S' or f.uzgtformularios_estado_llenado ='M' )  ORDER BY codigo_UZGTFORMULARIOS ASC";
        codfoblig = jdbcTemplate.queryForList(query,Integer.class);
        for (int u = 0; u < codfoblig.size(); u++) {
            codf = codfoblig.get(u);
            codFormsOblig.add(codf);
        }
        System.out.println("codFormsOblig "+codFormsOblig);

//这是我的问题,codFormsOblig有2个值,因此下面的查询将执行2次,并得到不同的结果,这2个结果需要将其保存在模型或FormPerson类中。

    for (int u = 0; u <codFormsOblig.size (); u ++) {
    String query1 = "SELECT CODIGO_UZGTFORMULARIOS, UZGTFORMULARIOS_ESTADO FROM UTIC.UZGTFORMULARIOS where UZGTFORMULARIOS_ESTADO = 1 AND codigo_uzgtFormularios =" + codFormsOblig.get (u) + "AND UZGTFORMULARIOS_DE = CODE"
                     
         // I don't know what type of jdbctemplate to use , queryforobject, queryforlist or any other that will allow me to save the values of the fields in the model
;
           List <FormPerson> cod = jdbcTemplate.queryForObject (query1, FormPerson.class);

                         return code;


}
java spring spring-boot spring-data-jpa jdbctemplate
1个回答
0
投票

您需要使用BeanPropertyRowMapper

https://mkyong.com/spring/spring-jdbctemplate-querying-examples/

此外,此查询中需要空格

    String query1 = "SELECT CODIGO_UZGTFORMULARIOS, UZGTFORMULARIOS_ESTADO FROM
 UTIC.UZGTFORMULARIOS where UZGTFORMULARIOS_ESTADO = 1 AND codigo_uzgtFormularios =" +
 codFormsOblig.get (u) + "AND UZGTFORMULARIOS_DE = CODE"
© www.soinside.com 2019 - 2024. All rights reserved.