1. 程式人生 > >資料庫操作sql封裝(insert)

資料庫操作sql封裝(insert)

**

資料庫插入(insert)sql封裝

**

1. 封裝工具類

public class DBUtil {
    public String column;  //插入列名
    public String values;   //插入值
    public String condition;
public static DBUtil insert(String seqName, Object obj){
......}
}

2. insert具體實現

public static DBUtil insert(String seqName, Object obj){

    //seqName 表對應ID索引
    Field[] fields = obj.getClass().getDeclaredFields();   //類列值
    
    //父類
    Field[] parentFields = obj.getClass().getSuperclass().getDeclaredFields();  
    
    String columnString = BaseEntity.FIELD_CREATE_DATE + "," ;
    String valuesString = "SYSDATE,";


    
    for(int i = 0, j=parentFields.length; i <j; i++){
        String fieldName = parentFields[i].getName();
        Object fieldValue = DBUtil.getFieldValueByName(parentFields[i].getName(), obj);


        if(!ValidatorUtil.isObjectEmpty(fieldValue)){
            if(fieldName.equals(BaseEntity.FIELD_REMARK)){
                columnString = columnString + BaseEntity.FIELD_REMARK + ",";
                valuesString = valuesString + "'" + fieldValue + "',";
            }

            if(fieldName.equals(BaseEntity.FIELD_CREATE_USER_ID)){
                columnString = columnString + BaseEntity.FIELD_CREATE_USER_ID + ",";
                valuesString = valuesString + fieldValue + ",";
            }

            if(fieldName.equals(BaseEntity.FIELD_CREATE_USER)){
                columnString = columnString + BaseEntity.FIELD_CREATE_USER + ",";
                valuesString = valuesString + "'" + fieldValue + "',";
            }

            if(fieldName.equals(BaseEntity.FIELD_STATUS)){
                columnString = columnString + BaseEntity.FIELD_STATUS + ",";
                valuesString = valuesString + fieldValue + ",";
            }

            if(fieldName.equals(BaseEntity.FIELD_DWDM)){
                columnString = columnString + BaseEntity.FIELD_DWDM + ",";
                valuesString = valuesString + "'" + fieldValue + "',";
            }

            if (fieldName.equals(BaseEntity.FIELD_UPDATE_USER)) {
                columnString = columnString + BaseEntity.FIELD_UPDATE_USER + ",";
                valuesString = valuesString + "'" + fieldValue + "',";
            }

            if (fieldName.equals(BaseEntity.FIELD_UPDATE_USER_ID)) {
                columnString = columnString + BaseEntity.FIELD_UPDATE_USER_ID + ",";
                valuesString = valuesString + "'" + fieldValue + "',";
            }
        }

    }
    
    for (int i = 0, j=fields.length; i < j; i++) {
        String fieldType = fields[i].getType().toString();
        String fieldName = fields[i].getName();
        Object fieldValue = DBUtil.getFieldValueByName(fields[i].getName(), obj);
        
        if (fieldType.equals("class java.lang.Long") && fieldName.equals("ID")) {
            if(!ValidatorUtil.isObjectEmpty(fieldValue)){
                columnString = columnString + fieldName + ",";
                valuesString = valuesString + fieldValue + ",";
            }else{
                columnString = columnString + fieldName + ",";
                valuesString = valuesString + seqName + ".nextval,";
            }
        }
        
        // 如果值不為null
        if (!ValidatorUtil.isObjectEmpty(fieldValue)) {
            if (fieldType.equals("class java.lang.String")) {
                columnString = columnString + fieldName + ",";
                valuesString = valuesString + "'" + fieldValue + "',";
            } else if (fieldType.equals("class java.util.Date")) {
                columnString = columnString + fieldName + ",";
                String dateString = DateUtil.defaultStringToString(fieldValue.toString(), "yyyy-MM-dd HH:mm:ss");
                valuesString = valuesString + " to_date('" + dateString + "', 'yyyy/mm/dd hh24:mi:ss'),";
            }else if (!fieldName.equals("ID")) {
                columnString = columnString + fieldName + ",";
                valuesString = valuesString + fieldValue + ",";
            }
        }
    }
    
    DBUtil db = new DBUtil();
    if(!ValidatorUtil.isEmpty(columnString) && !ValidatorUtil.isEmpty(valuesString)){
        db.setColumn(columnString.substring(0, columnString.lastIndexOf(",")));
        db.setValues(valuesString.substring(0, valuesString.lastIndexOf(",")));
    }
    
    System.out.println(db.getColumn());
    System.out.println(db.getValues());
    
    return db;
}

3. insert具體實現

this.bankAccountDao.insertBankAccount(DBUtil.insert(entity.SEQ_NAME, entity));