ResultHandler 返回对象属性都是null

2019-01-08  本文已影响0人  白驹过隙_忽然而已

Javabean,使用@Data注解

@Data@Accessors(chain=true)
@NoArgsConstructor
public class Admin implements Serializable{   
  private static final long serialVersionUID = 1L;  
  private Integer id;    
  private String name;    
  private String roles;    
  private String password;        
  private String token;    
  public Admin(Integer id, String name, String roles, String password, String token) {        
    this.id = id;       
    this.name = name;        
    this.roles = roles;        
    this.password = password;        
    this.token = token;    
  }    
  public Admin initToken() {        
    this.token = JWTUtil.createJWT(this.name, this.password);        return this;     
   }      
  public Admin encodePsd(){        
    this.password= PsdEncodes.entryptPassword(this.password);        
    return this;      
    }
}

ResultsetHandler测试:

/** 
  * BeanListHandler:将数据结果集的每一行数据,封装成JavaBean对象,多个JavaBean对象封装到List集合中 
  * @param conn 
  * @param sql */
public static void beanListhandler(Connection conn,String sql){    
  try {        
    Object o = qr.query(conn, sql, new BeanListHandler<Admin>(Admin.class));        
    ArrayList<Admin> admins = (ArrayList<Admin>) o;        
    admins.stream().map(x -> x.toString()).forEach(System.out::print);    
  } catch (SQLException e) {       
     e.printStackTrace();    
  }
}

返回的结果:

image

打断点发现是程序找不到Admin的setter方法,于是在实体类中补上setter:

//@Data
@Accessors(chain=true)
@NoArgsConstructor
public class Admin implements Serializable{   
  private static final long serialVersionUID = 1L;   
  private Integer id;    
  private String name;    
  private String roles;   
  private String password;        
  private String token;    
  public Admin(Integer id, String name, String roles, String password, String token) {        t
    his.id = id;        
    this.name = name;        
    this.roles = roles;        
    this.password = password;       
    this.token = token;    
  }    
  public Admin initToken() {       
   this.token = JWTUtil.createJWT(this.name, this.password);        
    return this;     
   }      
  public Admin encodePsd(){       
   this.password= PsdEncodes.entryptPassword(this.password);       
   return this;      
  }    
  public void setId(Integer id) {       
   this.id = id;   
 }    
  public void setName(String name) {       
   this.name = name;   
  }    
public void setRoles(String roles) {        
  this.roles = roles;    
  }    
  public void setPassword(String password) {       
     this.password = password;    
  }
}

结果:

image

Resulthander获取不到@data注解的setter,不知道为什么,记录下。

也可以手动实现handle方法,这个是各种resulthandler的核心:

image
上一篇下一篇

猜你喜欢

热点阅读