JDBC--DAO设计模式
时间:2021-07-01 10:21:17
帮助过:2人阅读
class DAO {
public int update(String sql, Object ... args){
Connection conn =
null;
PreparedStatement ps =
null;
int rowNum = 0
;
try{
conn =
JDBCUtils.getConnection();
ps =
conn.prepareStatement(sql);
for(
int i = 0; i < args.length; i++
){
ps.setObject(i + 1
, args[i]);
}
rowNum =
ps.executeUpdate();
return rowNum;
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(conn, ps, null);
}
return rowNum;
}
public <T> T get(Class<T>
clazz, String sql, Object ...args){
T entity =
null;
Connection conn =
null;
PreparedStatement ps =
null;
ResultSet rs =
null;
try{
conn =
JDBCUtils.getConnection();
ps =
conn.prepareStatement(sql);
for(
int i = 0; i < args.length; i++
){
ps.setObject(i + 1
, args[i]);
}
rs =
ps.executeQuery();
Map<String, Object> map =
new HashMap<String, Object>
();
List<String> columnLabels =
getColumnLabels(rs);
if(rs.next()){
for(String columnLabel : columnLabels){
Object columnValue =
rs.getObject(columnLabel);
map.put(columnLabel, columnValue);
}
}
if(map.size() > 0
){
entity =
clazz.newInstance();
for(Map.Entry<String, Object>
entry : map.entrySet()){
String fieldName =
entry.getKey();
Object value =
entry.getValue();
//使用BeanUtils工具类来为属性赋值
BeanUtils.setProperty(entity, fieldName, value);
/**使用反射的方式为属性赋值
Field field = clazz.getDeclaredField(key);
field.setAccessible(true);
field.set(entity, value);*/
}
return entity;
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(conn, ps, rs);
}
return entity;
}
public <T> List<T> getForList(Class<T>
clazz, String sql, Object ... args){
List<T> list =
new ArrayList<>
();
Connection conn =
null;
PreparedStatement ps =
null;
ResultSet rs =
null;
try{
conn =
JDBCUtils.getConnection();
ps =
conn.prepareStatement(sql);
for(
int i = 0; i < args.length; i++
){
ps.setObject(i + 1
, args[i]);
}
rs =
ps.executeQuery();
/**
* 将得到的ResultSet结果集转换为Map<String, Object>列表,
* 其中key值为rs表的别名,value值为对应别名的值
*/
List<Map<String, Object>> data =
handleResultSetToMapList(rs);
/**
* 将MapList转换为对应的BeanList
*/
list =
transferMapListToBeanList(clazz, data);
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(conn, ps, rs);
}
return list;
}
private <T> List<T> transferMapListToBeanList(Class<T> clazz,List<Map<String, Object>> data)
throws Exception {
List<T> list =
new ArrayList<>
();
if(data.size() > 0
){
for(Map<String, Object>
map : data){
T entity =
clazz.newInstance();
for(Map.Entry<String, Object>
entry : map.entrySet()){
String fieldName =
entry.getKey();
Object value =
entry.getValue();
BeanUtils.setProperty(entity, fieldName, value);;
}
list.add(entity);
}
}
return list;
}
public static <E>
E getForValue(String sql, Object ... args){
E entity =
null;
Connection conn =
null;
PreparedStatement ps =
null;
ResultSet rs =
null;
try{
conn =
JDBCUtils.getConnection();
ps =
conn.prepareStatement(sql);
for(
int i = 0; i < args.length; i++
){
ps.setObject(i + 1
, args[i]);
}
rs =
ps.executeQuery();
if(rs.next()){
return (E)rs.getObject(1
);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(conn, ps, rs);
}
return entity;
}
/**
* 将获取到的结果集处理成MapList
* @param rs
* @return
* @throws Exception
*/
private List<Map<String, Object>>
handleResultSetToMapList(ResultSet rs)
throws Exception {
List<String> columnLabels =
getColumnLabels(rs);
List<Map<String, Object>> data =
new ArrayList<Map<String, Object>>
();
while(rs.next()){
Map<String, Object> map =
new HashMap<String, Object>
();
for(String columnLabel : columnLabels){
Object columnValue =
rs.getObject(columnLabel);
map.put(columnLabel, columnValue);
}
data.add(map);
}
return data;
}
/**
* 根据结果集获取所有列的别名
* @param rs
* @return
* @throws Exception
*/
private List<String> getColumnLabels(ResultSet rs)
throws Exception{
List<String> list =
new ArrayList<>
();
ResultSetMetaData rsmd =
rs.getMetaData();
for(
int i = 0; i < rsmd.getColumnCount(); i++
){
list.add(rsmd.getColumnLabel(i + 1
));
}
return list;
}
}
JDBC--DAO设计模式
标签: