bean 反射生成插入的sql
2015-09-15 21:11
344 查看
import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class BeanUtil { static Map<String,Method> getTypeMethod(Class clazz,String type){ Method[] methods = clazz.getMethods(); Map<String,Method> map = new HashMap<String, Method>(); for(Method method:methods){ String sb = method.getName().toLowerCase(); int pos = sb.toLowerCase().indexOf(type); if(pos > -1){ map.put(sb.substring(type.length()), method); } } return map; } /*获取get方法名称 * */ static Map<String,Method> getGetMethod(Class clazz){ return getTypeMethod(clazz,"get"); } /* * 获取set方法名称 */ static Map<String,Method> getSetMethod(Class clazz){ return getTypeMethod(clazz,"set"); } public static StringBuilder genreateSql(List<?> beanList,String tablename){ if(null != beanList && beanList.size() > 0){ Class clazz = beanList.get(0).getClass(); StringBuilder sql = new StringBuilder(""); Map<String,Method> getmethods = getGetMethod(clazz); Map<String,Method> setmethods = getSetMethod(clazz); sql.append("insert into ").append(tablename).append(" ("); List<Property> props = new ArrayList<Property>(); for(Map.Entry<String, Method> entry:getmethods.entrySet()){ String key = entry.getKey(); Method getmethod = entry.getValue(); if(null != setmethods.get(key)){ Class rtncls= getmethod.getReturnType(); Property prop = new Property(); if(rtncls == int.class || rtncls == Integer.class ||rtncls == float.class || rtncls == Float.class || rtncls == double.class || rtncls == Double.class){ prop.setType(1); }else if(rtncls == String.class || rtncls == Date.class){ prop.setType(0); } else{ continue; } prop.setName(key); props.add(prop); sql.append(key).append(","); } } sql.deleteCharAt(sql.length()-1).append(") values "); for(Object bean : beanList){ sql.append("("); for(Property prop : props){ Method getMehod = getmethods.get(prop.getName()); try { prop.append(sql, getMehod.invoke(bean)); sql.append(","); } catch (Exception e) { e.printStackTrace(); } } sql.deleteCharAt(sql.length()-1); sql.append(") ,"); } sql.deleteCharAt(sql.length()-1); //System.out.println(sql); return sql; } return null; } } class Property{ private String name; private int type;/*判断类型,时候添加*/ public String getName() { return name; } public void setName(String name) { this.name = name; } public int getType() { return type; } public void setType(int type) { this.type = type; } public StringBuilder append(StringBuilder sb,Object val){ if(val == null) return sb.append("null"); if(1 == type){ return sb.append(val); } return sb.append("'").append(val).append("'"); } }
指定表的名称,然后需要bean的对象的属性名称与表中的字段名称一致,这样就可以生成要插入的sql了.
相关文章推荐
- 安装数据库提示无法解析机器名处理
- 数据库分页查询技术以及实现查询随机数
- Mysql学习笔记(二)对表结构的增删改查
- Ubuntu下安装redis
- Oracle学习笔记20150915常见循环语句以及goto语句
- SQL占用CPU100%
- 有哪些SQL语句会导致CPU过高?
- sqlite3数据库基础语句
- 解决SQL 的Cannot resolve collation conflict for column 6 in SELECT statement.报错
- 服务器后端开发系列——《实战Memcached内存缓存系统》
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- redis异常解决:attempted to load session[xxx] which has been created but not yet serialized.
- 数据库连接池(数据源)1
- Sql Server Cpu 100% 的常见原因及优化
- Oracle冷备步骤
- MongoDB 3.0.6 安装 增删改查
- Redis, Memcache, Mysql区别
- MongoDB学习日记 - java代码(二):建立连接
- SQL学习记录
- 从一台2005数据库备份后 ,想到另一台2005数据恢复 但发生 备份集中的数据库备份与现有的XX 数据库不同 选择覆盖现有数据库 也不行