本文主要是介绍JDBC将查询数据转换成javaBean,使用RowMapper实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
话不多说,上代码:
DBUtil:
1:查询单个Entity
/**
* @param sql sql语句* @param obj sql语句需要的参数数组
* @param rowMapper 具体对应的rowMapper
* @return
*/
public <T> T queryEntityBySql(String sql, Object[] obj,RowMapper<T> rowMapper) {
T entity = jdbcTemplate.queryForObject(sql, obj,rowMapper);
return entity;
}
2:查询EntityList
/**
* @param sql sql语句
* @param obj sql语句需要的参数数组
* @param rowMapper 具体对应的rowMapper
* @return
*/
public <T> List<T> queryEntityListBySql(String sql, Object[] obj,RowMapper<T> rowMapper) {
List<T> list = jdbcTemplate.query(sql, obj,rowMapper);
return list;
}
DAO:
1:
@Override
public ProviderInfoEntity findProviderInfoEntityByIndex(String providerCode, String serviceCode) {
ProviderInfoEntity providerInfoEntity = dbUtil.queryEntityBySql(
"select * from RM_ProviderInfo where ProviderCode = ? and ServiceCode = ?",
new Object[] {providerCode, serviceCode},
new ProviderInfoEntityRowMapper());
return providerInfoEntity;
}
2:
@Override
public List<ProviderInfoEntity> findProviderInfoEntityByProvider(String providerCode) {
List<ProviderInfoEntity> list = dbUtil.queryEntityListBySql(
"select * from RM_ProviderInfo where ProviderCode = ?",
new Object[] {providerCode},
new ProviderInfoEntityRowMapper());
return list;
}
OK,,,,结束
so easy
这篇关于JDBC将查询数据转换成javaBean,使用RowMapper实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!