1、数据库当中的表设计
2、对应数据表的实体Bean (id为主键)
- public class EnginConfigVO {
- int id = 0;
- int THREADS_COUNT;
-
-
-
-
- public int primaryGetId() {
- return id;
- }
-
-
-
- public void primarySetId(int id) {
- this.id = id;
- }
-
-
-
- public int getTHREADS_COUNT() {
- return THREADS_COUNT;
- }
-
-
-
-
- public void setTHREADS_COUNT(int tHREADS_COUNT) {
- THREADS_COUNT = tHREADS_COUNT;
- }
- }
public class EnginConfigVO {
int id = 0;
int THREADS_COUNT;
/**
* @return the id
*/
public int primaryGetId() {
return id;
}
/**
* @param id the id to set
*/
public void primarySetId(int id) {
this.id = id;
}
/**
* @return the tHREADS_COUNT
*/
public int getTHREADS_COUNT() {
return THREADS_COUNT;
}
/**
* @param tHREADS_COUNT the tHREADS_COUNT to set
*/
public void setTHREADS_COUNT(int tHREADS_COUNT) {
THREADS_COUNT = tHREADS_COUNT;
}
}
由于没有像hibernate那样的注解机制,所以只能在主键的setter和getter方法上动动手脚primaryGetId() ,primarySetId(int id)
而实体bean的类名在与数据表的匹配上最后多了“vo” 两个字母,所以在tb下面方法中将这两个字母剪裁掉。
反射方法:
T o 对应的就是实体Bean,这样的方法当然是写在DAO层中,供上层的service调用,传入需要修改的实体Bean
- public <T> void updatePropertiesValues(T o) {
- StringBuilder sd = new StringBuilder("update ");
- sd.append(o.getClass().getSimpleName().toLowerCase().substring(0, o.getClass().getSimpleName().length()-2)).append(" ");
- sd.append("set ");
- StringBuilder id = new StringBuilder("where ");
- try {
- for(Method m : o.getClass().getDeclaredMethods()) {
- String name = m.getName();
- if (name.startsWith("get")) {
- sd.append(name.substring(3).toLowerCase()).append("=");
- if(m.invoke(o) instanceof String) {
- sd.append("'").append(m.invoke(o)).append("', ");
- }else {
- sd.append(m.invoke(o)).append(", ");
- }
- }
- if(name.startsWith("primaryGet")) {
- id.append(name.substring(10).toLowerCase()).append("=");
- if(m.invoke(o) instanceof String) {
- id.append("'").append(m.invoke(o)).append("';");
- }else {
- id.append(m.invoke(o)).append(";");
- }
- }
- }
- sd.delete(sd.length()-2, sd.length());
- sd.append(" ");
- sd.append(id);
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
-
- executeTrans(sd.toString());
-
- }
public <T> void updatePropertiesValues(T o) {
StringBuilder sd = new StringBuilder("update ");
sd.append(o.getClass().getSimpleName().toLowerCase().substring(0, o.getClass().getSimpleName().length()-2)).append(" ");
sd.append("set ");
StringBuilder id = new StringBuilder("where ");
try {
for(Method m : o.getClass().getDeclaredMethods()) {
String name = m.getName();
if (name.startsWith("get")) {
sd.append(name.substring(3).toLowerCase()).append("=");
if(m.invoke(o) instanceof String) {
sd.append("'").append(m.invoke(o)).append("', ");
}else {
sd.append(m.invoke(o)).append(", ");
}
}
if(name.startsWith("primaryGet")) {
id.append(name.substring(10).toLowerCase()).append("=");
if(m.invoke(o) instanceof String) {
id.append("'").append(m.invoke(o)).append("';");
}else {
id.append(m.invoke(o)).append(";");
}
}
}
sd.delete(sd.length()-2, sd.length());
sd.append(" ");
sd.append(id);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
executeTrans(sd.toString());
}
这样以后便可以拼凑出完整的sql语句,为我们解决了功能相似代码的冗余。。
另外在查找时,我们还可以利用发射机制,将数据库返回的resultset 对象包装成List<T>
- public static <T> List<T> getObjectsList(ResultSet rs, Class<T> k)
- throws SQLException {
- List<T> bl = new ArrayList<T>();
- if (rs != null) {
- while (rs.next()) {
-
- T o = null;
- try {
- o = k.newInstance();
- for (Method m : k.getDeclaredMethods()) {
- String name = m.getName();
- if (name.startsWith("set")) {
-
- m.invoke(o, rs.getObject(name.substring(3)));
- }
- }
- bl.add(o);
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- return bl;
- }
- return null;
- }
public static <T> List<T> getObjectsList(ResultSet rs, Class<T> k)
throws SQLException {
List<T> bl = new ArrayList<T>();
if (rs != null) {
while (rs.next()) {
// System.out.println("result is not null");
T o = null;
try {
o = k.newInstance();
for (Method m : k.getDeclaredMethods()) {
String name = m.getName();
if (name.startsWith("set")) {
// System.out.println(rs.getObject(name.substring(3)).getClass().getName());
m.invoke(o, rs.getObject(name.substring(3)));
}
}
bl.add(o);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return bl;
}
return null;
}
这样,我们就可以从底层直接获得包装好的List<T>集合。。不足之处,欢迎大家讨论。。