对于不同的结果集使用相同的RowMapper可以使用一些共同的列

时间:2014-09-25 17:43:44

标签: java database spring-jdbc

两位地图制作者如下:

           class Apple implements RowMapper<Apple> {
            public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
              Apple a = new Apple();
              a.setName(rs.getString("Name"));
              a.setName(rs.getString("Color")); 
              a.setDesc(rs.getString("Desc"));
            }
          }

          class Apple implements RowMapper<Apple> {
            public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
              Apple a = new Apple();   
              a.setName(rs.getString("Name"));
              a.setName(rs.getString("Color"));
              a.setCost(rs.getString("Cost"));
            } 
          }
        }

我有两个Rowmappers返回相同的bean。如果第一个成本不存在,则结果集将不同,在第二种情况下,Desc将不会出现在结果集中。在这种情况下,我可以使用单个映射器,如下所示:

class Apple implements RowMapper<Apple> {

Private Boolean determine;

                public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
                  Apple a = new Apple();   
                  a.setName(rs.getString("Name"));
                  a.setName(rs.getString("Color"));
                 if(determine==true){
                    a.setCost(rs.getString("Cost"));
                }else{
                a.setDesc(rs.getString("Desc"));
                  }
                } 
              }
            }

在调用mapper时在构造函数中传递Boolean。如果不是最好的方法,可以实施吗。

注意:请忽略语法错误。

0 个答案:

没有答案