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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值