精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-12-09
public class PrePareSql { public static String selectSql(Class<?> obj) { String sql = "select "; Field[] fields = obj.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (i < fields.length - 1) { sql = sql + fields[i].getName() + " ,"; } else { sql = sql + fields[i].getName() + " from "; } } String tableName = getTableName(obj); sql = sql + tableName; System.out.println(sql); return sql; } public static String selectSql(Class<?> obj,String id) { String sql = "select "; Field[] fields = obj.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (i < fields.length - 1) { sql = sql + fields[i].getName() + " ,"; } else { sql = sql + fields[i].getName() + " from "; } } String tableName = getTableName(obj) + " where " + fields[0].getName() +" = ?"; sql = sql + tableName; System.out.println(sql); return sql; } public static String insertSql(Class<?> obj) { String sql = "insert into " + getTableName(obj) +" ( "; Field[] fields = obj.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (i < fields.length - 1) { sql = sql + fields[i].getName() + " ,"; } else { sql = sql + fields[i].getName() + " ) "+"values( "; } } for (int i = 0; i < fields.length; i++) { if (i < fields.length - 1) { sql=sql+"?,"; }else{ sql=sql+"? )"; } } System.out.println(sql); return sql; } public static String deleteSql(Class<?> obj){ String sql = "delete from " + getTableName(obj)+" where "; Field[] fields = obj.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (i < fields.length - 1) { sql = sql + fields[i].getName() + " = ? and "; } else { sql = sql + fields[i].getName()+" = ? "; } } System.out.println(sql); return sql; } public static String updateSql(Class<?> obj){ //UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson' //UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson' String sql = "update " +getTableName(obj) +" set "; Field[] fields = obj.getDeclaredFields(); for (int i = 1; i < fields.length; i++) { if (i < fields.length - 1) { sql = sql + fields[i].getName() + " = ? , "; } else { sql = sql + fields[i].getName()+" = ? where "; } } sql=sql+ fields[0].getName() +" = ?"; System.out.println(sql); return sql; } public static String getTableName(Class<?> obj) { String className = obj.getName(); int index = className.lastIndexOf("."); String realClassName = className.substring(index + 1); return realClassName; } public static void main(String[] args) { PrePareSql.selectSql(UserInfo.class,"liu"); PrePareSql.insertSql(UserInfo.class); PrePareSql.deleteSql(UserInfo.class); PrePareSql.updateSql(UserInfo.class); } } //这个是工具类 import java.lang.reflect.Field; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class ConnectionUtil { private Connection conn; private String sql; private List params; public void setConn(Connection conn) { this.conn = conn; } public void setSql(String sql) { this.sql = sql; } public void setParams(List params) { this.params = params; } public Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root"); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } public List executeQuery(Class<?> entityClass) { ResultSet rs = null; List list = new ArrayList(); try { setSql(PrePareSql.selectSql(entityClass)); PreparedStatement pstmt = getConnection().prepareStatement(sql); // setParams(pstmt); rs = pstmt.executeQuery(); while (rs.next()) { // System.out.println(rs.getString(1) + "\t" + rs.getString(2)); Object newInstance = wrap(entityClass, rs); list.add(newInstance); } } catch (SQLException e) { e.printStackTrace(); } return list; } // getOne public Object executeQuery(Class<?> entityClass, String id) { // Class<?> entityClass = entity.getClass(); ResultSet rs = null; Object newInstance = null; setSql(PrePareSql.selectSql(entityClass, id)); try { PreparedStatement pstmt = getConnection().prepareStatement(sql); pstmt.setString(1, id); rs = pstmt.executeQuery(); while (rs.next()) { // System.out.println(rs.getString(1) + "\t" + rs.getString(2)); newInstance = wrap(entityClass, rs); } } catch (SQLException e) { e.printStackTrace(); } return newInstance; } public void save(Object entity) { Class<?> entityClass = entity.getClass(); Field[] fileds = entityClass.getDeclaredFields(); ArrayList arrayList = new ArrayList(); try { for (Field f : fileds) { String getMethodName = "get" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1); Method getMethod = entityClass.getMethod(getMethodName, new Class[] {}); // System.out.println(setMethod); Object obj = getMethod.invoke(entity, new Object[] {}); arrayList.add(obj); } } catch (Exception e) { e.printStackTrace(); } try { setSql(PrePareSql.insertSql(entityClass)); PreparedStatement pstmt = getConnection().prepareStatement(sql); setParams(arrayList); setParams(pstmt); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void update(Object entity) { Class<?> entityClass = entity.getClass(); Field[] fileds = entityClass.getDeclaredFields(); ArrayList arrayList = new ArrayList(); try { for (int i = 1; i < fileds.length; i++) { String getMethodName = "get" + fileds[i].getName().substring(0, 1).toUpperCase() + fileds[i].getName().substring(1); Method getMethod = entityClass.getMethod(getMethodName, new Class[] {}); System.out.println(getMethod); Object obj = getMethod.invoke(entity, new Object[] {}); arrayList.add(obj); } String getMethodName = "get" + fileds[0].getName().substring(0, 1).toUpperCase() + fileds[0].getName().substring(1); Method getMethod = entityClass.getMethod(getMethodName, new Class[] {}); System.out.println(getMethod); Object obj = getMethod.invoke(entity, new Object[] {}); arrayList.add(obj); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < arrayList.size(); i++) { System.out.println("array: " + arrayList.get(i)); } try { setSql(PrePareSql.updateSql(entityClass)); PreparedStatement pstmt = getConnection().prepareStatement(sql); setParams(arrayList); setParams(pstmt); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public Object wrap(Class<?> entityClass, ResultSet rs) { Object obj2 = null; try { obj2 = entityClass.newInstance(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Field[] fileds = entityClass.getDeclaredFields(); try { for (Field f : fileds) { String setMethodName = "set" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1); Method setMethod = entityClass.getMethod(setMethodName, new Class[] { f.getType() }); Object obj = rs.getObject(f.getName()); setMethod.invoke(obj2, new Object[] { obj }); } } catch (Exception e) { e.printStackTrace(); } return obj2; } private void setParams(PreparedStatement pstmt) { if (this.params == null || this.params.size() == 0) { return; } try { for (int i = 0; i < this.params.size(); i++) { pstmt.setObject(i + 1, this.params.get(i)); } } catch (SQLException e) { e.printStackTrace(); } } public void delete(Object entity) { ArrayList arrayList = new ArrayList(); Class<?> entityClass = entity.getClass(); Field[] fileds = entityClass.getDeclaredFields(); try { for (Field f : fileds) { String getMethodName = "get" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1); Method getMethod = entityClass.getMethod(getMethodName, new Class[] {}); // System.out.println(setMethod); Object obj = getMethod.invoke(entity, new Object[] {}); arrayList.add(obj); } } catch (Exception e) { e.printStackTrace(); } try { setSql(PrePareSql.deleteSql((entityClass))); PreparedStatement pstmt = getConnection().prepareStatement(sql); setParams(arrayList); setParams(pstmt); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-12-09
第一次发帖,不知道怎么搞。代码也写了一段时间了。一直没改。各位勿怪
|
|
返回顶楼 | |
发表时间:2011-12-09
http://freyja.iteye.com/blog/1250247
|
|
返回顶楼 | |
发表时间:2011-12-09
很不错的代码 绝对需要支持
|
|
返回顶楼 | |
发表时间:2011-12-10
一些意见:
1,不够完整 2,反射没做缓存,肯定速度不行 |
|
返回顶楼 | |
发表时间:2011-12-10
字符串拼接的时候可以用StringBuilder
|
|
返回顶楼 | |
发表时间:2011-12-12
最后修改:2011-12-12
obj.getDeclaredFields();
如果存在继承关系的话,貌似不能得到超类的属性 |
|
返回顶楼 | |
发表时间:2011-12-12
neaudiy 写道 一些意见:
1,不够完整 2,反射没做缓存,肯定速度不行 有时间了会改 |
|
返回顶楼 | |
发表时间:2011-12-12
jwrg 写道 obj.getDeclaredFields();
如果存在继承关系的话,貌似不能得到超类的属性 这个还有很多bug。以后有时间会改的 |
|
返回顶楼 | |
发表时间:2011-12-12
hbhrobin 写道 字符串拼接的时候可以用StringBuilder
用字符串效率确实比用string高。 |
|
返回顶楼 | |