content: package com.vua.common.base;
  
  import com.github.pagehelper.PageHelper;
  import com.vua.common.util.SpringContextUtil;
  import org.apache.commons.lang.StringUtils;
  import org.apache.ibatis.annotations.Param;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.ParameterizedType;
  import java.util.List;
  
  /**
   * Created by liunian on 6/23/17.
   */
  public class BaseServiceImpl<Mapper, Record, Example> implements BaseService<Record, Example> {
  
      public Mapper mapper;
  
      @Override
      public int countByExample(Example example) {
          try {
              Method countByExample = mapper.getClass().getDeclaredMethod("countByExample", example.getClass());
              Object result = countByExample.invoke(mapper, example);
              return  Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              System.out.println("coutByExample fail.. ");
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int deleteByExample(Example example) {
          try {
              Method deleteByExample = mapper.getClass().getDeclaredMethod("deleteByExample", example.getClass());
              Object result = deleteByExample.invoke(mapper, example);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int deleteByPrimarykey(Integer id) {
          try {
              Method deleteByPrimaryKey = mapper.getClass().getDeclaredMethod("deleteByPrimaryKey", id.getClass());
              Object result = deleteByPrimaryKey.invoke(mapper, id);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int insert(Record record) {
          try {
              Method insert = mapper.getClass().getDeclaredMethod("insert", record.getClass());
              Object result = insert.invoke(mapper, record);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int insertSelective(Record record) {
          try {
              Method insertSelective = mapper.getClass().getDeclaredMethod("insertSelective", record.getClass());
              Object result = insertSelective.invoke(mapper, record);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public List<Record> selectByExampleWithBLOBs(Example example) {
          try {
              Method selectByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs", example.getClass());
              Object result = selectByExampleWithBLOBs.invoke(mapper, example);
              return (List<Record>)result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public List<Record> selectByExample(Example example) {
          try {
              Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
              Object result = selectByExample.invoke(mapper, example);
              return (List<Record>)result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public List<Record> selectByExampleWithBLOBsForStartPage(Example example, Integer pageNum, Integer pageSize) {
          try {
              Method selectByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs", example.getClass());
              PageHelper.startPage(pageNum, pageSize, false);
              Object result = selectByExampleWithBLOBs.invoke(mapper, example);
              return (List<Record>)result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public List<Record> selectByExampleForStartPage(Example example, Integer pageNum, Integer pageSize) {
          try {
              Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
              PageHelper.startPage(pageNum, pageSize, false);
              Object result = selectByExample.invoke(mapper, example);
              return (List<Record>)result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public List<Record> selectByExampleWithBLOBsForOffsetPage(Example example, Integer offset, Integer limit) {
          try {
              Method selectByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs", example.getClass());
              PageHelper.offsetPage(offset, limit);
              Object result = selectByExampleWithBLOBs.invoke(mapper, example);
              return (List<Record>)result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public List<Record> selectByExampleForOffsetPage(Example example, Integer offset, Integer limit) {
          try {
              Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
              PageHelper.offsetPage(offset, limit, false);
              Object result = selectByExample.invoke(mapper, example);
              return (List<Record>) result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public Record selectFirstByExample(Example example) {
          try {
              Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
              List<Record> result = (List<Record>) selectByExample.invoke(mapper, example);
              if (null != result && result.size() > 0) {
                  return result.get(0);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public Record selectFirstByExampleWithBLOBs(Example example) {
          try {
              Method selectByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs", example.getClass());
              List<Record> result = (List<Record>) selectByExampleWithBLOBs.invoke(mapper, example);
              if (null != result && result.size() > 0) {
                  return result.get(0);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public Record selectByPrimaryKey(Integer id) {
          try {
              Method selectByPrimaryKey = mapper.getClass().getDeclaredMethod("selectByPrimaryKey", id.getClass());
              Object result = selectByPrimaryKey.invoke(mapper, id);
              return (Record) result;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public int updateByExampleSelective(@Param("record") Record record, @Param("example") Example example) {
          try {
              Method updateByExampleSelective = mapper.getClass().getDeclaredMethod("updateByExampleSelective", record.getClass(), example.getClass());
              Object result = updateByExampleSelective.invoke(mapper, record, example);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int updateByExampleWithBLOBs(@Param("record") Record record, @Param("example") Example example) {
          try {
              Method updateByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("updateByExampleWithBLOBs", record.getClass(), example.getClass());
              Object result = updateByExampleWithBLOBs.invoke(mapper, record, example);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int updateByExample(@Param("record") Record record, @Param("example") Example example) {
          try {
              Method updateByExample = mapper.getClass().getDeclaredMethod("updateByExample", record.getClass(), example.getClass());
              Object result = updateByExample.invoke(mapper, record, example);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int updateByPrimaryKeySelective(Record record) {
          try {
              Method updateByPrimaryKeySelective = mapper.getClass().getDeclaredMethod("updateByPrimaryKeySelective", record.getClass());
              Object result = updateByPrimaryKeySelective.invoke(mapper, record);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int updateByPrimaryKeyWithBLOBs(Record record) {
          try {
              Method updateByPrimaryKeyWithBLOBs = mapper.getClass().getDeclaredMethod("updateByPrimaryKeyWithBLOBs", record.getClass());
              Object result = updateByPrimaryKeyWithBLOBs.invoke(mapper, record);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int updateByPrimaryKey(Record record) {
          try {
              Method updateByPrimaryKey = mapper.getClass().getDeclaredMethod("updateByPrimaryKey", record.getClass());
              Object result = updateByPrimaryKey.invoke(mapper, record);
              return Integer.parseInt(String.valueOf(result));
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public int deleteByPrimaryKeys(String ids) {
          try {
              if (StringUtils.isBlank(ids)) {
                  return 0;
              }
              String[] idArray = ids.split("-");
              int count = 0;
              for (String idStr : idArray) {
                  if (StringUtils.isBlank(idStr)) {
                      continue;
                  }
                  Integer id = Integer.parseInt(idStr);
                  Method deleteByPrimaryKey = mapper.getClass().getDeclaredMethod("deleteByPrimaryKey", id.getClass());
                  Object result = deleteByPrimaryKey.invoke(mapper, id);
                  count += Integer.parseInt(String.valueOf(result));
              }
              return count;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return 0;
      }
  
      @Override
      public void initMapper() {
          this.mapper = SpringContextUtil.getBean(getMapperClass());
      }
  
      /**
       * 获取类泛型class
       * @return
       * getClass().getGenericSuperclass()返回表示此 Class 所表示的实体（类、接口、基本类型或 void）的直接超类的 Type
       * getActualTypeArguments()返回表示此类型实际类型参数的 Type 对象的数组。[0]就是这个数组中第一个了。。
       * 简而言之就是获得超类的泛型参数的实际类型。
       */
      public Class<Mapper> getMapperClass() {
          return (Class<Mapper>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
      }
  }
  