Esempio n. 1
0
  /**
   * 根据多个id 查询数据
   *
   * @param <T>
   * @param ids 整形的id数组
   * @param c 要查询的表信息
   * @return List
   */
  public <T> List<T> searchByIds(Class<T> c, int[] ids, String orderby) {

    Entity<T> entity = dao.getEntity(c);

    String id = entity.getIdField().getColumnName();

    return dao.query(c, Cnd.where(id, "in", ids).desc(orderby), null);
  }
Esempio n. 2
0
  /**
   * 根据指定的字段模糊分页查询数据 记录总数
   *
   * @param <T>
   * @param c 查询的表
   * @param fieldName 字段名称
   * @param value 模糊条件
   * @param currentPage 当前页码
   * @param pageSize 每页数据量
   * @return List
   */
  public <T> int searchByPageLike(Class<T> c, String fieldName, String value) {

    Entity<T> entity = dao.getEntity(c);

    String column = entity.getField(fieldName).getColumnName();

    return dao.count(c, Cnd.where(column, "LIKE", "%" + value + "%"));
  }
Esempio n. 3
0
  /**
   * 根据多个id 查询数据条数
   *
   * @param <T>
   * @param ids 要查询的id,多个用","(逗号)分隔
   * @param c 要查询的表信息
   * @return List
   */
  public <T> int searchByIdsCount(Class<T> c, String ids, String orderby) {

    Entity<T> entity = dao.getEntity(c);

    String id = entity.getIdField().getColumnName();

    String sql = " " + id + " in (" + ids + ") order by " + orderby + " desc";

    return dao.count(c, Cnd.wrap(sql));
  }
Esempio n. 4
0
  /**
   * 根据多个id 查询数据
   *
   * @param <T>
   * @param ids 要查询的id,多个用","(逗号)分隔
   * @param c 要查询的表信息
   * @return List
   */
  public <T> List<T> searchByIds(Class<T> c, String ids, String orderby) {

    Entity<T> entity = dao.getEntity(c);

    String id = entity.getIdField().getColumnName();

    String sql = " " + id + " in (" + ids + ") order by " + orderby + " desc";

    return dao.query(c, Cnd.wrap(sql), null);
  }
Esempio n. 5
0
  /**
   * 根据某个条件分页查询数据
   *
   * @param <T>
   * @param c 查询的表
   * @param fieldName 匹配字段名
   * @param value 匹配的值
   * @param currentPage 当前页码
   * @param pageSize 每页数据量
   * @return List
   */
  public <T> List<T> searchByPage(
      Class<T> c, String fieldName, String value, int currentPage, int pageSize) {

    Entity<T> entity = dao.getEntity(c);

    String column = entity.getField(fieldName).getColumnName();

    Pager pager = dao.createPager(currentPage, pageSize);

    return dao.query(c, Cnd.where(column, "=", value), pager);
  }
Esempio n. 6
0
  /**
   * 根据多个id删除数据
   *
   * @param <T>
   * @param c 要操作的表信息
   * @param ids 要删除的id,多个用","(逗号)分隔
   * @return true 成功,false 失败
   */
  public <T> void deleteByIds(Class<T> c, String ids) {

    Entity<T> entity = dao.getEntity(c);

    String table = entity.getTableName();

    String id = entity.getIdField().getColumnName();

    Sql sql = Sqls.create("delete from " + table + " where " + id + " in(" + ids + ")");

    dao.execute(sql);
  }
Esempio n. 7
0
  /**
   * 根据多个id 查询数据并且分页
   *
   * @param <T>
   * @param ids 要查询的id,多个用","(逗号)分隔
   * @param c 要查询的表信息
   * @return List
   */
  public <T> int searchByIdsCount(
      Class<T> c, String ids, String orderby, int currentPage, int pageSize) {

    // Pager pager = dao.createPager(currentPage, pageSize);

    Entity<T> entity = dao.getEntity(c);

    String id = entity.getIdField().getColumnName();

    String sql = " " + id + " in (" + ids + ") order by " + orderby + " desc ";
    return dao.query(c, Cnd.wrap(sql), null).size();
  }
Esempio n. 8
0
  /**
   * 根据多个id 查询数据并且分页
   *
   * @param <T>
   * @param ids 要查询的id,多个用","(逗号)分隔
   * @param c 要查询的表信息
   * @return List
   */
  public <T> List<T> searchByIdsFenye(
      Class<T> c, String ids, String orderby, int currentPage, int pageSize) {

    // Pager pager = dao.createPager(currentPage, pageSize);

    Entity<T> entity = dao.getEntity(c);

    String id = entity.getIdField().getColumnName();

    String sql =
        " "
            + id
            + " in ("
            + ids
            + ") order by "
            + orderby
            + " desc limit "
            + (currentPage - 1) * pageSize
            + ", "
            + pageSize
            + "";

    return dao.query(c, Cnd.wrap(sql), null);
  }
Esempio n. 9
0
  /** 根据Pojo生成数据字典,zdoc格式 */
  public static StringBuilder dataDict(DataSource ds, String... packages) {
    StringBuilder sb = new StringBuilder();
    List<Class<?>> ks = new ArrayList<Class<?>>();
    for (String packageName : packages) {
      ks.addAll(Scans.me().scanPackage(packageName));
    }
    Iterator<Class<?>> it = ks.iterator();
    while (it.hasNext()) {
      Class<?> klass = it.next();
      if (klass.getAnnotation(Table.class) == null) it.remove();
    }
    // log.infof("Found %d table class", ks.size());

    JdbcExpert exp = Jdbcs.getExpert(ds);
    NutDao dao = new NutDao(ds);

    Method evalFieldType;
    try {
      evalFieldType = exp.getClass().getDeclaredMethod("evalFieldType", MappingField.class);
    } catch (Throwable e) {
      throw Lang.wrapThrow(e);
    }
    evalFieldType.setAccessible(true);
    Entity<?> entity = null;
    String line = "-------------------------------------------------------------------\n";
    sb.append("#title:数据字典\n");
    sb.append("#author:wendal\n");
    sb.append("#index:0,1\n").append(line);
    for (Class<?> klass : ks) {
      sb.append(line);
      entity = dao.getEntity(klass);
      sb.append("表名 ").append(entity.getTableName()).append("\n\n");
      if (!Strings.isBlank(entity.getTableComment()))
        sb.append("表注释: ").append(entity.getTableComment());
      sb.append("\t").append("Java类名 ").append(klass.getName()).append("\n\n");
      sb.append("\t||序号||列名||数据类型||主键||非空||默认值||java属性名||java类型||注释||\n");
      int index = 1;
      for (MappingField field : entity.getMappingFields()) {
        String dataType;
        try {
          dataType = (String) evalFieldType.invoke(exp, field);
        } catch (Throwable e) {
          throw Lang.wrapThrow(e); // 不可能发生的
        }
        sb.append("\t||")
            .append(index++)
            .append("||")
            .append(field.getColumnName())
            .append("||")
            .append(dataType)
            .append("||")
            .append(field.isPk())
            .append("||")
            .append(field.isNotNull())
            .append("||")
            .append(field.getDefaultValue(null) == null ? " " : field.getDefaultValue(null))
            .append("||")
            .append(field.getName())
            .append("||")
            .append(field.getTypeClass().getName())
            .append("||")
            .append(field.getColumnComment() == null ? " " : field.getColumnComment())
            .append("||\n");
      }
    }
    return sb;
  }