/* 纯为学习,无营养的评论,请绕道,谢谢 */
定义主类,BaseDao:
public interface BaseDao<T> { //采用泛型类
public ArrayList<T> query(String sql,Object[] obj);
public boolean update(String sql,Object[] obj);
}
定义实现类,DaoSupprot:
public class DaoSupport<T> implements BaseDao<T> {
private Connection con;
private ResultSet rs;
private PreparedStatement pstmt;
private Class clazz = GenericUtils.getSuperGenericType(this.getClass()); //获得超类,参数化类型。
public ArrayList<T> query(String sql, Object[] obj) {
ArrayList<T> list = new ArrayList<T>();
con = DBUtil.getConnection();
try {
pstmt = con.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
}
rs = pstmt.executeQuery();
list = mapping(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public ArrayList<T> mapping(ResultSet rs) {
ArrayList<T> list = new ArrayList<T>();
try {
while (rs.next()) {
Object obj = this.clazz.newInstance();
Field[] fields = this.clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
// setter
String setMethodName = "set"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
// Method
Method m = this.clazz.getDeclaredMethod(setMethodName,
fields[i].getType());
// 调用方法,防止类型不匹配,自写
if (fields[i].getType().equals(int.class)) {
m.invoke(obj, rs.getInt(fieldName));
} else if (fields[i].getType().equals(Date.class)) {
m.invoke(obj, rs.getDate(fieldName));
} else {
m.invoke(obj, rs.getObject(fieldName));
}
}
list.add((T) obj);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public boolean update(String sql, Object[] obj) {
con = DBUtil.getConnection();
try {
pstmt = con.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
}
if (pstmt.executeUpdate() > 0)
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.closeAll(con, pstmt, null);
}
return false;
}
}
GenericUtils类,获得参数化类型:
public class GenericUtils {
public static Class getSuperGenericType(Class clazz){
//超类是参数化类型
Type type=clazz.getGenericSuperclass();
//ParameterizedType 表示参数化类型,如 Collection<String>。
ParameterizedType paramType=(ParameterizedType)type;
//返回表示此类型实际类型参数的 Type 对象的数组。
Type[] types=paramType.getActualTypeArguments();
return (Class)types[0];
}
}
创建一个业务处理UserInfoDao:
public interface UserInfoDao extends BaseDao<UserInfo> {
public boolean insertUser(UserInfo userInfo);
public boolean updateUser(UserInfo userInfo);
public boolean deleteUser(String uname);
public ArrayList<UserInfo> getAllUsers();
public UserInfo getUserByName(String uname);
}
最后一操作类 UserInfoDaoImpl:
public class UserInfoDaoImpl extends DaoSupport<UserInfo> implements UserInfoDao{
@Override
public boolean deleteUser(String uname) {
// TODO Auto-generated method stub
String sql="delete from t_userinfo where uname=?";
Object[] obj={uname};
return this.update(sql, obj);
}
@Override
public ArrayList<UserInfo> getAllUsers() {
// TODO Auto-generated method stub
String sql="select * from t_userinfo ";
return this.query(sql, null);
}
@Override
public UserInfo getUserByName(String uname) {
// TODO Auto-generated method stub
String sql="select * from t_userinfo where uname=?";
Object[] obj=new Object[]{uname};
ArrayList<UserInfo> list=this.query(sql, obj);
UserInfo bean=null;
if (list.size()>0) bean=list.get(0);
return bean;
}
@Override
public boolean insertUser(UserInfo userInfo) {
// TODO Auto-generated method stub
String sql="insert into t_userinfo values(?,?)";
Object[] obj=new Object[]{userInfo.getUname(),userInfo.getUpwd()};
return this.update(sql, obj);
}
@Override
public boolean updateUser(UserInfo userInfo) {
// TODO Auto-generated method stub
String sql="update t_userInfo set upwd=? where uname=?";
Object[] obj=new Object[]{userInfo.getUpwd(),userInfo.getUname()};
return this.update(sql, obj);
}
}