获取单个对象
- 直接使用queryForObject即可,示例如下:
StringBuffer sb = new StringBuffer("select a.user_id userId,a.login_name,a.name name from cs_user a where a.login_name='").append(loginName).append("'");
CsUser csUser = jdbcTemplate.queryForObject(sb.toString(), new BeanPropertyRowMapper<CsUser>(CsUser.class));
return csUser;
获取list
实体类实现RowMapper接口(也可不实现)
@Override
public CsOrgan mapRow(ResultSet resultSet, int i) throws SQLException {
CsOrgan csOrgan = new CsOrgan();
csOrgan.setId(resultSet.getString("id"));
csOrgan.setOrgId(resultSet.getString("orgId"));
csOrgan.setOrgCode(resultSet.getString("orgCode"));
csOrgan.setOrgType(resultSet.getString("orgType"));
csOrgan.setOrgLevel(resultSet.getInt("orgLevel"));
csOrgan.setOrgName(resultSet.getString("orgName"));
csOrgan.setProvince(resultSet.getString("province"));
csOrgan.setCity(resultSet.getString("city"));
csOrgan.setArea(resultSet.getString("area"));
return csOrgan;
}
使用query方法
- 如果实体类实现了RowMapper接口,可用以下方法:
List<CsOrgan> list = null;
StringBuffer sb = new StringBuffer("select w.*")
.append(" from cs_user_organnode a left join wdim_info w on a.ORGAN_NODE_ID = w.id ")
.append("where a.SECURITY_USER_ID='").append(userId).append("'");
list = jdbcTemplate.query(sb.toString(),new CsOrgan());
- 如果实体类没有实现RowMapper接口,可用以下方法:
List<CsOrgan> list = null;
StringBuffer sb = new StringBuffer("select w.*")
.append(" from cs_user_organnode a left join wdim_info w on a.ORGAN_NODE_ID = w.id ")
.append("where a.SECURITY_USER_ID='").append(userId).append("'");
list = jdbcTemplate.query(sb.toString(),new BeanPropertyRowMapper<CsOrgan>(CsOrgan.class));
网友评论