核心.
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import com.framework.dao.SqlDao;
import com.framework.logic.procurement.mapping.DynaTable;
import com.framework.util.BeanUtil;
import com.framework.util.StringUtil;
public class DynaTableUtil {
public SqlDao sqlDao;
public static DynaTableUtil tableUtil = null;
public static DynaTableUtil getInstance() {// lazy initialization
if (tableUtil == null) {
tableUtil = new DynaTableUtil();
}
return tableUtil;
}
public Object getDynaTableRecord(DynaTable table) throws Exception {
if (!table.getQueryForeignkey()) {// 查询主键
// 获取MAP ,将MAP转为实体对象
Map map = null;
map = (Map) sqlDao.getRecord("procurement.getDynamicTableByKey", table);
if (map != null) {
Object ob = createBean(table.getTableBeanClass(), map);
return ob;
} else {
return map;
}
} else {// 查询表外键,
List listMap = new ArrayList();
List list = sqlDao.getRecordList("procurement.getDynamicTableByForeginKey", table);
// 转换为javaBean
for (Object ob : list) {
Object temp = createBean(table.getTableBeanClass(), (Map) (ob));
listMap.add(ob);
}
return listMap;
}
}
public void insertRecord(DynaTable table) throws Exception {
// 创建INSERT_SQL
table.setSql(this.createInsertSql(table));
System.out.println(table.getSql());
sqlDao.insertRecord("procurement.insertDynamicTable", table);
}
public String createInsertSql(DynaTable table) throws InstantiationException, IllegalAccessException {
StringBuffer sb = new StringBuffer();
sb.append("insert into " + table.getTableName() + " ");
// 获取对象,判断属性值是否为NULL
Object objInstance = table.getTableBean();
BeanWrapper beanWrapper = new BeanWrapperImpl(objInstance);
String name;
String type;
Object value;
StringBuffer columnName = new StringBuffer();
StringBuffer columnValue = new StringBuffer();
// 循环属性说明
for (PropertyDescriptor descriptor : beanWrapper.getPropertyDescriptors()) {
name = descriptor.getName();
type = descriptor.getPropertyType().getName();
if (!beanWrapper.isWritableProperty(name))
continue;
columnName.append(name + ",");
try {
Object tempValue = descriptor.getReadMethod().invoke(objInstance, null);
tempValue = BeanUtil.convertType(type, String.valueOf(tempValue));
if (tempValue != "null" && tempValue != null) {
tempValue = "'" + tempValue + "'";
}
// 判断是否为主键
if (name.toUpperCase().equals(table.getTableKey().toUpperCase())) {
// 生成主键VALUE
// 保存至于table_key_value
table.setKeyValue(StringUtil.getPrimaryKey());
tempValue = "'" + table.getKeyValue() + "'";
}
columnValue.append(tempValue + ",");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
sb.append("(" + columnName.substring(0, columnName.length() - 1) + ")");
sb.append("values(" + columnValue.substring(0, columnValue.length() - 1) + ")");
return sb.toString();
}
public static Object createBean(Class beanClass, Map map) throws InstantiationException, IllegalAccessException {
Object objInstance = beanClass.newInstance();
BeanWrapper beanWrapper = new BeanWrapperImpl(objInstance);
String name;
String type;
Object value;
// 循环属性说明
for (PropertyDescriptor descriptor : beanWrapper.getPropertyDescriptors()) {
name = descriptor.getName();
type = descriptor.getPropertyType().getName();
if (!beanWrapper.isWritableProperty(name))
continue;
value = BeanUtil.convertType(type, String.valueOf(map.get(name)));// 从MAP中查询参数,并转换为相应的类型
if (value == null || value == "null")
continue;
beanWrapper.setPropertyValue(name, value);
}
return objInstance;
}
}
//动态TABLE需要的参数
public class DynaTable {
private Class tableBeanClass;// bean Class对象
private String tableName;// biaoming
private String tableKey;// 主键
private String keyValue;// VALUE
private String javaBennQualified;// 限定名,存JAVABEAN包下面
private String sql;
private Object tableBean;// bean对象
private String foreignkey, foreignkeyValue;// 设置外键,以及外键的值
private Boolean queryForeignkey =false;// 是否查询外键,注,设置为FALSE后,查询返回值为BEAN_LIST,否则为JAVABEAN
public String getForeignkey() {
return foreignkey;
}
public void setForeignkey(String foreignkey) {
this.foreignkey = foreignkey;
}
public String getForeignkeyValue() {
return foreignkeyValue;
}
public void setForeignkeyValue(String foreignkeyValue) {
this.foreignkeyValue = foreignkeyValue;
}
public Boolean getQueryForeignkey() {
return queryForeignkey;
}
public void setQueryForeignkey(Boolean queryForeignkey) {
this.queryForeignkey = queryForeignkey;
}
public Object getTableBean() {
return tableBean;
}
public void setTableBean(Object tableBean) {
this.tableBean = tableBean;
}
public void setTableBeanClass(Class tableBeanClass) {
this.tableBeanClass = tableBeanClass;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public String getJavaBennQualified() {
return javaBennQualified;
}
public void setJavaBennQualified(String javaBennQualified) {
this.javaBennQualified = javaBennQualified;
try {
this.tableBeanClass = Class.forName(javaBennQualified);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public String getTableName() {
return tableName;
}
public Class getTableBeanClass() {
return tableBeanClass;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTableKey() {
return tableKey;
}
public void setTableKey(String tableKey) {
this.tableKey = tableKey;
}
public String getKeyValue() {
return keyValue;
}
public void setKeyValue(String keyValue) {
this.keyValue = keyValue;
}
}
//添加
//处理方法,要处理的TABLE
public Map<String, DynaTable> getTableNameMap() {
this.tableNameMap = new HashMap<String, DynaTable>();
// 初始化要查询对象
DynaTable a = new DynaTable();
// 设置返回的实例对象
a.setJavaBennQualified("com.framework.logic.procurement.mapping.Procurement");
// 查询的表
a.setTableName("Procurement");
a.setTableKey("Procurementid");// 主键
a.setKeyValue(request.getParameter("procurementid"));
this.tableNameMap.put("Procurement", a);
}
//保存
public void procurementSave() throws Exception {
this.model = new HashMap();
this.viewName = "message.jsp";
String message = "";
String beanName = request.getParameter("beanName");
try {
// 获取TABLE对象
DynaTable table = this.tableNameMap.get(beanName);
// 基本参数
table.setTableBean((BeanUtil.createBean(table.getTableBeanClass(), request)));
// FILE文件设置值
// 插入数据库
DynaTableUtil.getInstance().insertRecord(table);
System.out.println(table.getKeyValue());
message="保存成功!<a href='procurementList.procurement' >返回</a>";
} catch (Exception e) {
e.printStackTrace();
message = "获取页面参数出现异常,异常原因为:" + e.getMessage();
}
this.model.put("message", message);
System.out.println(message);
this.modelAndView = new ModelAndView(this.viewName, this.model);
}
//查看
public void procurementMod() throws Exception {
this.model = new HashMap();
this.viewName = "procurement/procurementMod.jsp";
// 查询
DynaTableUtil tableUtil = DynaTableUtil.getInstance();
tableUtil.sqlDao = sqlDao;
getTableNameMap();
Iterator it = this.tableNameMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
DynaTable dt = (DynaTable) entry.getValue();
Object ob = tableUtil.getDynaTableRecord(dt);
this.model.put(dt.getTableName().toLowerCase(), ob);
}
this.modelAndView = new ModelAndView(this.viewName, this.model);
}
sqlmap
<insert id="insertDynamicTable" parameterClass="com.framework.logic.procurement.mapping.DynaTable" >
<![CDATA[
$sql$
]]>
</insert>
<!-- 此处增加remapResults="true"指每次查询都更新列名 -->
<select id="getDynamicTableByKey" parameterClass="com.framework.logic.procurement.mapping.DynaTable" resultClass="java.util.HashMap" remapResults="true">
<![CDATA[
select * from $tableName$ where $tableKey$ =#keyValue# limit 1
]]>
</select>
<!-- 此处增加remapResults="true"指每次查询都更新列名 -->
<select id="getDynamicTableByForeginKey" parameterClass="com.framework.logic.procurement.mapping.DynaTable" resultClass="java.util.HashMap" remapResults="true">
<![CDATA[
select * from $tableName$ where $foreignkey$ =#foreignkeyValue#
]]>
</select>