package com.lightinit.hsdatashow.common; import com.lightinit.hsdatashow.entity.BaseExample; import com.lightinit.hsdatashow.model.ResultState; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * @Author: sunxiang * @Date: 2018/9/3 10:52 * @Description: 通过反射对单表的删,改,查进行操作的工具类 **/ public class SingleChartMapperUtils { public static List list(List clazz,String tableType,BaseExample.Page pager) { ArrayList mappers = new ArrayList(); for (Object o : clazz) { mappers.add(o.getClass().getInterfaces()[0]); } for(int i=0;i< mappers.size();i++){ Class name = mappers.get(i); if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){ try{ Class aClass = Class.forName("com.lightinit.hsdatashow.entity."+tableType+ "Example"); Object obj=aClass.newInstance(); Method orderByClause = obj.getClass().getDeclaredMethod("setOrderByClause",String.class); orderByClause.invoke(obj,"data_year desc limit "+pager.getBeginIndex()+","+pager.getPageSize()); Object o= clazz.get(i); Method selectByExample = o.getClass().getDeclaredMethod("selectByExample", aClass); return (List)selectByExample.invoke(o,obj); }catch (Exception e){ e.printStackTrace(); throw new RuntimeException("查询失败"); } } } return null; } public static List list(List clazz,String tableType,BaseExample.Page pager,String orederClause) { ArrayList mappers = new ArrayList(); for (Object o : clazz) { mappers.add(o.getClass().getInterfaces()[0]); } for(int i=0;i< mappers.size();i++){ Class name = mappers.get(i); if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){ try{ Class aClass = Class.forName("com.lightinit.hsdatashow.entity."+tableType+ "Example"); Object obj=aClass.newInstance(); Method orderByClause = obj.getClass().getDeclaredMethod("setOrderByClause",String.class); orderByClause.invoke(obj,orederClause+" desc limit "+pager.getBeginIndex()+","+pager.getPageSize()); Object o= clazz.get(i); Method selectByExample = o.getClass().getDeclaredMethod("selectByExample", aClass); return (List)selectByExample.invoke(o,obj); }catch (Exception e){ e.printStackTrace(); throw new RuntimeException("查询失败"); } } } return null; } public static ResultState add(V v, boolean isEdit,List clazz) { ArrayList mappers = new ArrayList(); for (Object o : clazz) { mappers.add(o.getClass().getInterfaces()[0]); } ResultState resultState = new ResultState(); int i=0; try{ List names = mappers; for(int j=0;j< names.size();j++){ if(names.get(j).getSimpleName().equalsIgnoreCase(v.getClass().getSimpleName()+"Mapper")){ Object o= clazz.get(j); if(isEdit){ Method selectByExample = o.getClass().getDeclaredMethod("updateByPrimaryKeySelective", v.getClass()); i=(Integer) selectByExample.invoke(o,v); }else{ Method selectByExample = o.getClass().getDeclaredMethod("insert", v.getClass()); i=(Integer) selectByExample.invoke(o,v); } break; } } }catch (Exception e){ e.printStackTrace(); } if(i<1){ resultState.setStateCode(400); resultState.setMsg("操作失败"); return resultState; } resultState.setMsg("操作成功"); return resultState; } public static Object findById(List clazz,String tableType, Long id) { ArrayList mappers = new ArrayList(); for (Object o : clazz) { mappers.add(o.getClass().getInterfaces()[0]); } for(int i=0;i< mappers.size();i++){ Class name = mappers.get(i); if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){ try { Object o= clazz.get(i); Method selectByPrimaryKey=o.getClass().getDeclaredMethod("selectByPrimaryKey",Long.class); return selectByPrimaryKey.invoke(o,id); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("查询失败"); } } } return null; } public static long queryCount(List clazz,String tableType) { ArrayList mappers = new ArrayList(); for (Object o : clazz) { mappers.add(o.getClass().getInterfaces()[0]); } for(int i=0;i< mappers.size();i++){ Class name = mappers.get(i); if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){ try{ Class aClass = Class.forName("com.lightinit.hsdatashow.entity."+tableType+ "Example"); Object obj=aClass.newInstance(); Object o= clazz.get(i); Method selectByExample = o.getClass().getDeclaredMethod("countByExample", aClass); return (long)selectByExample.invoke(o,obj); }catch (Exception e){ e.printStackTrace(); throw new RuntimeException("查询失败"); } } } return 0; } public static ResultState delete(List clazz,String tableType, Long id) { ArrayList mappers = new ArrayList(); for (Object o : clazz) { mappers.add(o.getClass().getInterfaces()[0]); } ResultState resultState = new ResultState(); int j=0; for(int i=0;i< mappers.size();i++){ Class name = mappers.get(i); if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){ try { Object o= clazz.get(i); Method deleteByPrimaryKey=o.getClass().getDeclaredMethod("deleteByPrimaryKey",Long.class); j=(Integer) deleteByPrimaryKey.invoke(o,id); } catch (Exception e) { e.printStackTrace(); } break; } } if(j<1){ resultState.setStateCode(400); resultState.setMsg("删除失败"); return resultState; } resultState.setMsg("删除成功"); return resultState; } }