예제 #1
0
  /**
   * 删除指定个Id的数据
   *
   * @param id
   * @return
   */
  public boolean deleteById(Serializable id, Class classz) {
    DBTableMeta dbTableMeta = DBTableMetaFactory.getDBTableMeta(classz);
    String sql = getSqlAnalyzer().analyzeDeleteByPrimaryKey(dbTableMeta);

    printSQL(sql, new Object[] {id});

    QueryRunner queryRunner = new QueryRunner();

    Connection conn = getDbConnectionFactory().getConnection();

    try {
      int num = queryRunner.update(conn, sql, id);
      if (num == 1) {
        return true;
      } else if (num == 0) {
        return false;
      } else {
        throw new SQLException("数据删除异常,错误的sql与参数导致删除了" + num + "条数据");
      }
    } catch (SQLException e) {
      throw new RuntimeException("删除数据失败", e);
    } finally {
      getDbConnectionFactory().releaseConnection(conn);
    }
  }
예제 #2
0
  /**
   * 更新指定Id的数据
   *
   * @param model
   * @return
   */
  public boolean update(Object model) {

    if (model == null) {
      log.warn("更新数据失败,传入的model对象为null");
      return false;
    }

    DBTableMeta dbTableMeta = DBTableMetaFactory.getDBTableMeta(model.getClass());
    AnalyzeResult analyzeResult = getSqlAnalyzer().analyzeUpdateByPrimaryKey(dbTableMeta, model);

    return doUpdate(analyzeResult);
  }
예제 #3
0
  /**
   * 根据表达式统计长度
   *
   * @param example
   * @param classz
   * @param <T>
   * @return
   */
  public <T> long countByExample(Example example, Class<T> classz) {
    DBTableMeta dbTableMeta = DBTableMetaFactory.getDBTableMeta(classz);
    AnalyzeResult analyzeResult = getSqlAnalyzer().analyzeCountByExample(dbTableMeta, example);

    printSQL(analyzeResult.getSql(), analyzeResult.getParams());

    Connection conn = getDbConnectionFactory().getConnection();

    QueryRunner queryRunner = new QueryRunner();
    try {
      return queryRunner.query(
          conn, analyzeResult.getSql(), new ScalarHandler<Long>(1), analyzeResult.getParams());
    } catch (SQLException e) {
      throw new RuntimeException(e);
    } finally {
      getDbConnectionFactory().releaseConnection(conn);
    }
  }
예제 #4
0
  /**
   * 查询指定Id的数据
   *
   * @param id
   * @param classz
   * @param <T>
   * @return
   */
  public <T extends Model> T findById(Serializable id, Class<T> classz) {

    DBTableMeta dbTableMeta = DBTableMetaFactory.getDBTableMeta(classz);
    String sql = getSqlAnalyzer().analyzeSelectByPrimaryKey(dbTableMeta);

    printSQL(sql, new Object[] {id});

    Connection conn = getDbConnectionFactory().getConnection();

    QueryRunner queryRunner = new QueryRunner();
    try {
      T obj = queryRunner.query(conn, sql, new AnnotationBeanHandler<T>(classz), id);
      return obj;
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      getDbConnectionFactory().releaseConnection(conn);
    }
  }