springmvc+mybatis框架下,写通用的操作,删除等的操作

来源:互联网 发布:java多线程调用单例 编辑:程序博客网 时间:2024/06/04 23:27
/** * 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>
0 0
原创粉丝点击