论坛首页 Java企业应用论坛

帮看看这个dao

浏览 7246 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2011-05-28  
package com.test.dao;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.test.util.DBUtil;
import com.test.GenericClass;


@SuppressWarnings("unchecked")
public class BaseDAO<T> implements DAO<T> {

private Class classs = GenericClass.getClass(this.getClass(), 0);

public void addOrUpdate(String sql, Object[] objects) {
}

public void del(String sql, Object[] objects) {
}

public T find(String where, Object[] objects) throws Exception {
Connection con = DBUtil.getConnection();
PreparedStatement ps = con.prepareStatement("select * from "
+ classs.getSimpleName() + where);
setPrames(ps, objects);
ResultSet rs = ps.executeQuery();
Field[] fields = classs.getDeclaredFields();
Method[] methods = classs.getDeclaredMethods();
T o = (T) classs.newInstance();
if (rs.next()) {
ext(rs, fields, methods, o);
}
return o;
}

public QueryResult<T> findAll(int frist, int max, String where,
Object[] objects) throws Exception {
QueryResult queryResult = new QueryResult();
Connection con = DBUtil.getConnection();
StringBuilder sql = new StringBuilder();
String wheres = (where == null || "".equals(where)) ? "" : " where "
+ where;
//如果frist 和max都为-1 就不需要分页
if (frist == -1 && max == -1) {
sql.append("select * from " + classs.getSimpleName());
sql.append(wheres);
} else {
sql
.append("select * from(select *,row_number() over(order by id) as rownumber from "
+ classs.getSimpleName()
+ " ) as s where rownumber between "
+ frist
+ " and " + max + "");
sql.append(" and " + where);
}
PreparedStatement ps = con.prepareStatement(sql.toString());
setPrames(ps, objects);
ResultSet rs = ps.executeQuery();
Field[] fields = classs.getDeclaredFields();
Method[] methods = classs.getDeclaredMethods();
List list = new ArrayList();
while (rs.next()) {
T o = (T) classs.newInstance();
ext(rs, fields, methods, o);
list.add(o);
}
queryResult.setRows(list);
ps = con.prepareStatement("select count(*) from "
+ classs.getSimpleName() + wheres);
setPrames(ps, objects);
rs = ps.executeQuery();
if (rs.next()) {
queryResult.setTotal(rs.getLong(1));
}
return queryResult;
}

private void ext(ResultSet rs, Field[] fields, Method[] methods, T o)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, SQLException {
for (int i = 0; i < fields.length; i++) {
for (int j = 0; j < methods.length; j++) {
if (methods[j].getName().equalsIgnoreCase(
"set" + fields[i].getName())) {
if (fields[i].getType().getSimpleName().equals("int")) {
methods[j].invoke(o, rs.getInt(fields[i].getName()));
} else if (fields[i].getType().getSimpleName().equals(
"String")) {
methods[j].invoke(o, rs.getString(fields[i].getName()));
} else if (fields[i].getType().getSimpleName().equals(
"float")) {
methods[j].invoke(o, rs.getFloat(fields[i].getName()));
}
}
}
}
}

private void setPrames(PreparedStatement ps, Object[] objects)
throws SQLException {
if (objects != null && objects.length > 0) {
for (int i = 0; i < objects.length; i++) {
ps.setObject(i + 1, objects[i]);
}
}
}

}
package com.test.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

@SuppressWarnings("unchecked")
public class GenerieUtils {
public static Class getClass(Class class1, int index) {
Type type = class1.getGenericSuperclass();
if (!(type instanceof ParameterizedType)) {
return Object.class;
}
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
if (!(type instanceof Class)) {
return Object.class;
}
return (Class) types[index];
}
}
package com.test.dao;

import java.util.List;

public class QueryResult<T> {
private List<T> rows;
private long total;

public List<T> getRows() {
return rows;
}

public void setRows(List<T> rows) {
this.rows = rows;
}

public long getTotal() {
return total;
}

public void setTotal(long total) {
this.total = total;
}

}
增删改 不知道该怎么写好一点..........
本人学java还没一年 所以写的不好请见谅
   发表时间:2011-05-29  
这个代码格式。能把人累死。javaeye有代码排版这个功能的啊,怎么不用
0 请登录后投票
   发表时间:2011-05-29  
这个太乱了,看起来好费劲哦。
0 请登录后投票
   发表时间:2011-05-30  
1年的写个这个不错了
0 请登录后投票
   发表时间:2011-05-30  
建议不要用泛型
0 请登录后投票
   发表时间:2011-05-30  
steafler 写道
建议不要用泛型

Hi 
   steafler
   小弟不解 。求结果
0 请登录后投票
   发表时间:2011-05-30   最后修改:2011-05-30
泛型、反射、jdbc,感觉怪怪的。
lz的对象属性全是基本类型?
0 请登录后投票
   发表时间:2011-05-30  
ext(ResultSet rs, Field[] fields, Method[] methods, T o)方法中,你是通过fields判断属性的,其实只要找出所有已“set”开头的方法,调用就行了。
你那个双重循环加上反射,太费劲了。
还有,整个类,一个close方法都没有,Connection / PreparedStatment/ ResultSet用完后都要close的,要放在finally块中。
这个类不是很通用,限制比较多,建议你看看spring的JdbcTemplate。
0 请登录后投票
   发表时间:2011-05-30  
已阅。。。
0 请登录后投票
   发表时间:2011-05-30   最后修改:2011-05-30
package com.test.dao; 

import java.lang.reflect.Field; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 
import com.test.util.DBUtil; 
import com.test.GenericClass; 


@SuppressWarnings("unchecked") 
public class BaseDAO<T> implements DAO<T> { 

private Class classs = GenericClass.getClass(this.getClass(), 0); 

public void addOrUpdate(String sql, Object[] objects) { 
} 

public void del(String sql, Object[] objects) { 
} 

public T find(String where, Object[] objects) throws Exception { 
Connection con = DBUtil.getConnection(); 
PreparedStatement ps = con.prepareStatement("select * from " 
+ classs.getSimpleName() + where); 
setPrames(ps, objects); 
ResultSet rs = ps.executeQuery(); 
Field[] fields = classs.getDeclaredFields(); 
Method[] methods = classs.getDeclaredMethods(); 
T o = (T) classs.newInstance(); 
if (rs.next()) { 
ext(rs, fields, methods, o); 
} 
return o; 
} 

public QueryResult<T> findAll(int frist, int max, String where, 
Object[] objects) throws Exception { 
QueryResult queryResult = new QueryResult(); 
Connection con = DBUtil.getConnection(); 
StringBuilder sql = new StringBuilder(); 
String wheres = (where == null || "".equals(where)) ? "" : " where " 
+ where; 
//如果frist 和max都为-1 就不需要分页 
if (frist == -1 && max == -1) { 
sql.append("select * from " + classs.getSimpleName()); 
sql.append(wheres); 
} else { 
sql 
.append("select * from(select *,row_number() over(order by id) as rownumber from " 
+ classs.getSimpleName() 
+ " ) as s where rownumber between " 
+ frist 
+ " and " + max + ""); 
sql.append(" and " + where); 
} 
PreparedStatement ps = con.prepareStatement(sql.toString()); 
setPrames(ps, objects); 
ResultSet rs = ps.executeQuery(); 
Field[] fields = classs.getDeclaredFields(); 
Method[] methods = classs.getDeclaredMethods(); 
List list = new ArrayList(); 
while (rs.next()) { 
T o = (T) classs.newInstance(); 
ext(rs, fields, methods, o); 
list.add(o); 
} 
queryResult.setRows(list); 
ps = con.prepareStatement("select count(*) from " 
+ classs.getSimpleName() + wheres); 
setPrames(ps, objects); 
rs = ps.executeQuery(); 
if (rs.next()) { 
queryResult.setTotal(rs.getLong(1)); 
} 
return queryResult; 
} 

private void ext(ResultSet rs, Field[] fields, Method[] methods, T o) 
throws IllegalArgumentException, IllegalAccessException, 
InvocationTargetException, SQLException { 
for (int i = 0; i < fields.length; i++) { 
for (int j = 0; j < methods.length; j++) { 
if (methods[j].getName().equalsIgnoreCase( 
"set" + fields[i].getName())) { 
if (fields[i].getType().getSimpleName().equals("int")) { 
methods[j].invoke(o, rs.getInt(fields[i].getName())); 
} else if (fields[i].getType().getSimpleName().equals( 
"String")) { 
methods[j].invoke(o, rs.getString(fields[i].getName())); 
} else if (fields[i].getType().getSimpleName().equals( 
"float")) { 
methods[j].invoke(o, rs.getFloat(fields[i].getName())); 
} 
} 
} 
} 
} 

private void setPrames(PreparedStatement ps, Object[] objects) 
throws SQLException { 
if (objects != null && objects.length > 0) { 
for (int i = 0; i < objects.length; i++) { 
ps.setObject(i + 1, objects[i]); 
} 
} 
} 

} 
package com.test.util; 

import java.lang.reflect.ParameterizedType; 
import java.lang.reflect.Type; 

@SuppressWarnings("unchecked") 
public class GenerieUtils { 
public static Class getClass(Class class1, int index) { 
Type type = class1.getGenericSuperclass(); 
if (!(type instanceof ParameterizedType)) { 
return Object.class; 
} 
Type[] types = ((ParameterizedType) type).getActualTypeArguments(); 
if (!(type instanceof Class)) { 
return Object.class; 
} 
return (Class) types[index]; 
} 
} 
package com.test.dao; 

import java.util.List; 

public class QueryResult<T> { 
private List<T> rows; 
private long total; 

public List<T> getRows() { 
return rows; 
} 

public void setRows(List<T> rows) { 
this.rows = rows; 
} 

public long getTotal() { 
return total; 
} 

public void setTotal(long total) { 
this.total = total; 
} 

} 


没有缩进。。。汗。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics