每次新增service的时候,都需要把方法再写一遍,所以尝试着写了一个基础的service,这样每次引用就可以了,麻烦给看看写的有什么错误或者不合适的地方没有,谢谢。
public abstract class BaseService { protected Model tableClass; //javabean名称 protected String tableName; //表名称 protected Map<String, Object> inbeforeMap; //新增状态加载数据前插入 protected Map<String, Object> inafterMap; //新增状态加载数据后插入 protected Map<String, Object> upbeforeMap; //更新状态加载数据前插入 protected Map<String, Object> upafterMap; //更新状态加载数据后插入 //单条记录查询 public Map<String,Object> getDataById(String id) throws Exception{ Map<String, Object> map = new HashMap<String, Object>(); map.put("data", tableClass.dao().findById(id)); map.put("status", "sucess"); return map; } //查询 public Map<String, Object> getData(String json) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> data = JSONObject.parseObject(deCoderBase64(json)); //先base64解码然后在解析 int pageNumber = Integer.parseInt(data.get("pagenumber").toString()); int pageSize = Integer.parseInt(data.get("pagesize").toString()); String select = "select *", where = "", order = ""; if (!data.get("select").equals("")) select = "select " + data.get("select"); if (!data.get("where").equals("")) where = " where " + data.get("where").toString(); if (!data.get("order").equals("")) order = " order by " + data.get("order").toString(); map.put("data", tableClass.dao().paginate(pageNumber, pageSize, select, " from " + tableName + where + order)); map.put("status", "sucess"); return map; } //新增,更新 @Before(Tx.class) //如果新增或者更新有问题,直接回滚 public Map<String, Object> upData(String json) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); List<Map> dataList = JSONObject.parseArray(json, Map.class); for (int i = 0; i < dataList.size(); i++) { Map<String, Object> mapData = dataList.get(i); if (mapData.get("_state").equals("added")) { //加载前 tableClass.put(inbeforeMap); //加载数据 tableClass.put(JSONObject.parseObject(JSONObject.toJSONString(mapData), tableClass.getClass())); //加载后 tableClass.put(inafterMap); //插入新增 tableClass.save(); } else if (mapData.get("_state").equals("modified")) { //加载前 tableClass.put(upbeforeMap); //加载数据 tableClass.put(JSONObject.parseObject(JSONObject.toJSONString(mapData), tableClass.getClass())); //加载后 tableClass.put(upafterMap); //数据更新标识更新 tableClass._setAttrs(tableClass); //更新数据 tableClass.update(); } } map.put("status", "sucess"); return map; } //删除 @Before(Tx.class) //如果删除有问题,直接回滚 public Map<String, Object> delData(String list) throws Exception { if (list == null || list.equals("")) { throw new RuntimeException("无数据删除"); } Map<String, Object> map = new HashMap<String, Object>(); String[] datalist = list.split(","); for (String data : datalist) { tableClass.dao().deleteById(data); } map.put("status", "sucess"); return map; } }
其他service引用
public class SysMenuService extends BaseService { public SysMenuService() { /*============= 初始化数据,不用动 ================*/ Map<String, Object> inbeforeMap = new HashMap<String, Object>(); Map<String, Object> inafterMap = new HashMap<String, Object>(); Map<String, Object> upbeforeMap = new HashMap<String, Object>(); Map<String, Object> upafterMap = new HashMap<String, Object>(); /*================= ****需要修改的*** ==================*/ super.tableName = "sys_menu"; super.tableClass = new SysMenu(); //新增加载前赋值字段 inbeforeMap.put("vid", java.util.UUID.randomUUID().toString()); //主键 inbeforeMap.put("create_date", new Date()); //创建日期 //新增加载后赋值字段 inafterMap.put("update_date", new Date()); //更新日期 //更新加载前赋值字段 upbeforeMap.put("update_date", new Date()); //更新日期 //更新加载后赋值字段 /*=====================================================*/ /*============= 初始化数据,不用动 ================*/ super.inbeforeMap = inbeforeMap; super.inafterMap = inafterMap; super.upbeforeMap = upbeforeMap; super.upafterMap = upafterMap; } }