BeanPropertyRowMapper

BeanPropertyRowMapper在query过程中使用,可以按照属性名与字段名进行自动的数据类型转换。

@Data
class Student {
    private Integer id;
    private String name;
    private Integer age;
}
@Repository("studentDao")
class StudentDaoImpl implements StudentDao {
    
    @Autowired
    @Qualifier("namedParameterJdbcTemplate")
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static String SELECT_SQL = " select id,name,age from student ";
    
    @Override
    public List getListByCondition(List ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            Map params = new HashMap<>();
            params.put("ids", ids);
            StringBuilder sb = new StringBuilder(SELECT_SQL);
            sb.append(" where a.app_id in (:appIds) ");
            return namedParameterJdbcTemplate.query(sb.toString(), params,
                new BeanPropertyRowMapper<>(Student.class));
        }
        return Lists.newArrayList();
    }

}

注:

  1. 字段名对应可以有驼峰形式。例如:表中字段为stu_name,实体类中的属性名可以对应stuName。

你可能感兴趣的:(BeanPropertyRowMapper)