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
- springmvc+mybatis框架下,写通用的操作,删除等的操作
- SpringMvc中对Controller层进行拦截统计通用的耗时,操作日记记录等。
- mybatis框架下mysql多表更新的操作
- 关于mybatis的批量删除操作
- Cookies的设定删除等操作
- 关于文件的删除复制等操作
- 单链表的创建、插入、删除等操作
- 单链表的初始化,插入删除等操作
- 单链表的创建,插入删除等操作
- 单链表的创建、插入删除等操作
- 文件的复制删除移动等操作
- Foundation框架(5)——NSMutableString的创建、拼接、替换、插入、删除等操作
- 上移下移等操作的通用存储过程
- 容器的通用操作
- vim的通用操作
- SpringMVC和mybatis的框架
- mybatis不用spring下的操作
- 利用springmvc+freemarker+mybatis实现数据库的操作
- javascript代码运行分两个阶段:预解析,执行
- 获取版本号 版本名称
- 使用Shell脚本查找程序对应的进程ID,并杀死进程
- Java学习路线
- Qt连接SqlSever2014数据库教程(非dsn法)手把手
- springmvc+mybatis框架下,写通用的操作,删除等的操作
- Codeforces Round #381 (Div. 2) Alyona and mex
- ios 后台无限心跳实现:GCDAsyncSocket使用的 Voip、NSTimer、10分钟超长链接
- 一个项目流程(从前端到后台)服务器相关
- Footer
- java 使用xsd文件 校验 xml文件
- OpenNLP入门实验
- 浅谈LDAP服务
- hadoop输入分片计算(Map Task个数的确定) - 有无之中