1. 程式人生 > >springmvc+mybatis框架下,寫通用的操作,刪除等的操作

springmvc+mybatis框架下,寫通用的操作,刪除等的操作

/**
 * Created by ldz..attack on 2016/11/23 0023.
 * <p>
 * 配合mybatis的預編譯想用它做一個通用操作類
 * <p>
 * 目前來看只有:刪除/許可權修改兩種操作用此方法比較簡便
 * <p>
 * 如果是查詢,大量的不同欄位不易解決
 */
@Controller
@RequestMapping(value = "/general")
public class ControllerGeneral extends BaseController {
    public static final String shop = "ty_shop"
; public static final String usr = "ty_member"; @Resource(name = "ServiceGeneral") private ServiceGeneral serviceGeneral; /** * 通用操作 * <p> * 前端有屬性type: * <p> * type:1.刪除,2.禁用和啟用 */ @RequestMapping(value = "/operate") public void operate
(HttpServletResponse response, HttpServletRequest request) throws Exception { JSONObject jo = new JSONObject(); PageData pd = this.getPageData(); int i = 0; try { String ids = pd.get("ids") == null ? "" : pd.getString("ids"); int type = pd.get("type"
) == null ? 0 : Transform.string_int(pd.getString("type")); List<String> list_id = ArrayTool.str2list(ids, ","); for (String str_id : list_id) { switch (type) { case 1://刪除 pd = this.setVal(pd, type, str_id); serviceGeneral.del(pd); break; case 2://禁用啟用 pd = this.setVal4Ban(pd, type, str_id, true); int j = serviceGeneral.ban(pd); if (j == 0) { pd = this.setVal4Ban(pd, type, str_id, false); serviceGeneral.allow(pd); } break; } i++; } jo.put("code", "success"); jo.put("msg", "成功操作" + i + "條記錄"); } catch (Exception e) { jo.put("code", "error"); String msg = i == 0 ? "操作失敗" : "操作未全部完成,修改" + i + "條記錄"; jo.put("msg", msg); logger.error(e.toString(), e); } System.out.println(jo); JsonRender8QC.renderObj(response, jo); } //根據傳來的引數,來設定表名 PageData setVal(PageData pd, int type, String id) { String filter = pd.get("filter") == null ? "" : pd.getString("filter"); if ("shop".equals(filter)) { pd.put("tableName", shop);//寫表名 this.setConditionAndValue(pd, "isDel", "1", "id", id);//去寫要求改的欄位,欄位值和條件欄位名,欄位值 } if ("usr".equals(filter)) { pd.put("tableName", usr);//寫表名 this.setConditionAndValue(pd, "isDel", "1", "id", id);//去寫要求改的欄位,欄位值和條件欄位名,欄位值 } return pd; } //根據傳來的表名,來設定值 PageData setVal4Ban(PageData pd, int type, String id, boolean boo_ban) { String filter = pd.get("filter") == null ? "" : pd.getString("filter"); if ("shop".equals(filter)) { pd.put("tableName", shop);//寫表名 this.setConditionAndValue(pd, "isDisable", boo_ban ? "1" : "0", "id", id);//去寫要求改的欄位,欄位值和條件欄位名,欄位值 } if ("usr".equals(filter)) { pd.put("tableName", usr);//寫表名 this.setConditionAndValue(pd, "isDisabled", boo_ban ? "1" : "0", "id", id);//去寫要求改的欄位,欄位值和條件欄位名,欄位值 } return pd; } //賦值 PageData setConditionAndValue(PageData pd, String editFieldName,//被修改的欄位 String editFieldValue,//欄位值 String conditionFieldName,//條件欄位 String conditionFieldValue) {//條件值 pd.put("editFieldName", editFieldName); pd.put("editFieldValue", editFieldValue); pd.put("conditionFieldName", conditionFieldName); pd.put("conditionFieldValue", conditionFieldValue); return pd; } }
    <!--刪除(不真正刪除)-->
    <update id="del" parameterType="pd" statementType="STATEMENT">
        UPDATE ${tableName}
        SET ${editFieldName}=${editFieldValue}
        WHERE  ${conditionFieldName} = ${conditionFieldValue}
    </update>
    <!--禁用-->
    <update id="ban" parameterType="pd" statementType="STATEMENT">
        UPDATE ${tableName}
        SET ${editFieldName}=${editFieldValue}
        WHERE ${conditionFieldName} = ${conditionFieldValue}
        /*更好的解決方案是,在操作之前,查一下*/
        AND IFNULL(${editFieldName},0)=0
    </update>
    <!--啟用-->
    <update id="allow" parameterType="pd" statementType="STATEMENT">
        UPDATE ${tableName}
        SET ${editFieldName}=${editFieldValue}
        WHERE ${conditionFieldName} = ${conditionFieldValue}
        /*更好的解決方案是,在操作之前,查一下*/
        AND IFNULL(${editFieldName},0)=1
    </update>