JDBC将查询数据转换成javaBean,使用RowMapper实现

话不多说,上代码:

DBUtil:

1:查询单个Entity

/**

* @param sql sql语句
* @param obj sql语句需要的参数数组
* @param rowMapper 具体对应的rowMapper
* @return
*/
public T queryEntityBySql(String sql, Object[] obj,RowMapper rowMapper) {
T entity = jdbcTemplate.queryForObject(sql, obj,rowMapper);
return entity;
}
2:查询EntityList
/**
* @param sql sql语句
* @param obj sql语句需要的参数数组
* @param rowMapper 具体对应的rowMapper
* @return
*/
public List queryEntityListBySql(String sql, Object[] obj,RowMapper rowMapper) {
List 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 findProviderInfoEntityByProvider(String providerCode) {
List list = dbUtil.queryEntityListBySql(
"select * from RM_ProviderInfo where ProviderCode = ?",
new Object[] {providerCode},
new ProviderInfoEntityRowMapper());
return list;
}


OK,,,,结束

so easy

你可能感兴趣的:(Java)