@Test public void testCreate() { }
public <TModel> int create(Class<TModel> modelClass, TModel entity) { int result = 0; if (entity == null) { return result; } ClassMeta classMeta = Metadata.get(modelClass); Attribute primaryKey = classMeta.checkPrimaryKey(); GenerationType strategy; String pkFieldName; boolean autoGeneratedPK = false; Sql sql = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; pkFieldName = primaryKey.getFieldName(); strategy = classMeta.getStrategy(); if (strategy == GenerationType.IDENTITY) { autoGeneratedPK = true; } else { Object pkVal = ReflectUtil.getFieldValue(entity, pkFieldName); if (pkVal == null) { if (strategy == GenerationType.ASSIGNED) { throw new DBException("Assigned ID can not be null."); } else if (strategy == GenerationType.UUID) { pkVal = uuid(); } else if (strategy == GenerationType.SNOW_FLAKE) { pkVal = snowFlakeId(); } ReflectUtil.setFieldValue(entity, pkFieldName, pkVal); } } sql = getDialect().insert(modelClass, entity); try { getSqlLog().showSql(sql.getSqlString(), sql.getParams()); conn = this.open(); pstmt = DBUtil.getPstForUpdate(conn, autoGeneratedPK, sql.getSqlString(), sql.getParams()); result = DBUtil.executeUpdate(pstmt); getSqlLog().showAffectedRows(result); if (autoGeneratedPK) { rs = pstmt.getGeneratedKeys(); if (rs.next()) { ReflectUtil.setFieldValue(entity, pkFieldName, rs.getObject(1)); } } } catch (Exception ex) { throw new DBException(ex); } finally { DBUtil.close(pstmt, rs); this.close(); } return result; }
DB { public <TModel> int create(Class<TModel> modelClass, TModel entity) { int result = 0; if (entity == null) { return result; } ClassMeta classMeta = Metadata.get(modelClass); Attribute primaryKey = classMeta.checkPrimaryKey(); GenerationType strategy; String pkFieldName; boolean autoGeneratedPK = false; Sql sql = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; pkFieldName = primaryKey.getFieldName(); strategy = classMeta.getStrategy(); if (strategy == GenerationType.IDENTITY) { autoGeneratedPK = true; } else { Object pkVal = ReflectUtil.getFieldValue(entity, pkFieldName); if (pkVal == null) { if (strategy == GenerationType.ASSIGNED) { throw new DBException("Assigned ID can not be null."); } else if (strategy == GenerationType.UUID) { pkVal = uuid(); } else if (strategy == GenerationType.SNOW_FLAKE) { pkVal = snowFlakeId(); } ReflectUtil.setFieldValue(entity, pkFieldName, pkVal); } } sql = getDialect().insert(modelClass, entity); try { getSqlLog().showSql(sql.getSqlString(), sql.getParams()); conn = this.open(); pstmt = DBUtil.getPstForUpdate(conn, autoGeneratedPK, sql.getSqlString(), sql.getParams()); result = DBUtil.executeUpdate(pstmt); getSqlLog().showAffectedRows(result); if (autoGeneratedPK) { rs = pstmt.getGeneratedKeys(); if (rs.next()) { ReflectUtil.setFieldValue(entity, pkFieldName, rs.getObject(1)); } } } catch (Exception ex) { throw new DBException(ex); } finally { DBUtil.close(pstmt, rs); this.close(); } return result; } }
DB { public <TModel> int create(Class<TModel> modelClass, TModel entity) { int result = 0; if (entity == null) { return result; } ClassMeta classMeta = Metadata.get(modelClass); Attribute primaryKey = classMeta.checkPrimaryKey(); GenerationType strategy; String pkFieldName; boolean autoGeneratedPK = false; Sql sql = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; pkFieldName = primaryKey.getFieldName(); strategy = classMeta.getStrategy(); if (strategy == GenerationType.IDENTITY) { autoGeneratedPK = true; } else { Object pkVal = ReflectUtil.getFieldValue(entity, pkFieldName); if (pkVal == null) { if (strategy == GenerationType.ASSIGNED) { throw new DBException("Assigned ID can not be null."); } else if (strategy == GenerationType.UUID) { pkVal = uuid(); } else if (strategy == GenerationType.SNOW_FLAKE) { pkVal = snowFlakeId(); } ReflectUtil.setFieldValue(entity, pkFieldName, pkVal); } } sql = getDialect().insert(modelClass, entity); try { getSqlLog().showSql(sql.getSqlString(), sql.getParams()); conn = this.open(); pstmt = DBUtil.getPstForUpdate(conn, autoGeneratedPK, sql.getSqlString(), sql.getParams()); result = DBUtil.executeUpdate(pstmt); getSqlLog().showAffectedRows(result); if (autoGeneratedPK) { rs = pstmt.getGeneratedKeys(); if (rs.next()) { ReflectUtil.setFieldValue(entity, pkFieldName, rs.getObject(1)); } } } catch (Exception ex) { throw new DBException(ex); } finally { DBUtil.close(pstmt, rs); this.close(); } return result; } private  DB(DBTemplate dbTemplate); }
DB { public <TModel> int create(Class<TModel> modelClass, TModel entity) { int result = 0; if (entity == null) { return result; } ClassMeta classMeta = Metadata.get(modelClass); Attribute primaryKey = classMeta.checkPrimaryKey(); GenerationType strategy; String pkFieldName; boolean autoGeneratedPK = false; Sql sql = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; pkFieldName = primaryKey.getFieldName(); strategy = classMeta.getStrategy(); if (strategy == GenerationType.IDENTITY) { autoGeneratedPK = true; } else { Object pkVal = ReflectUtil.getFieldValue(entity, pkFieldName); if (pkVal == null) { if (strategy == GenerationType.ASSIGNED) { throw new DBException("Assigned ID can not be null."); } else if (strategy == GenerationType.UUID) { pkVal = uuid(); } else if (strategy == GenerationType.SNOW_FLAKE) { pkVal = snowFlakeId(); } ReflectUtil.setFieldValue(entity, pkFieldName, pkVal); } } sql = getDialect().insert(modelClass, entity); try { getSqlLog().showSql(sql.getSqlString(), sql.getParams()); conn = this.open(); pstmt = DBUtil.getPstForUpdate(conn, autoGeneratedPK, sql.getSqlString(), sql.getParams()); result = DBUtil.executeUpdate(pstmt); getSqlLog().showAffectedRows(result); if (autoGeneratedPK) { rs = pstmt.getGeneratedKeys(); if (rs.next()) { ReflectUtil.setFieldValue(entity, pkFieldName, rs.getObject(1)); } } } catch (Exception ex) { throw new DBException(ex); } finally { DBUtil.close(pstmt, rs); this.close(); } return result; } private  DB(DBTemplate dbTemplate); static DB conn(DBTemplate dbTemplate); static DB conn(String dsName); static DB conn(); R tx(Function<DB, R> function); void tx(Consumer<DB> consumer); Connection open(); void close(); void commit(); void rollback(); List<TView> find(Class<TView> viewClass,
      String sql,
      Collection params); List<TView> find(Class<TView> viewClass,
      String sql); List<TView> find(Class<TView> viewClass,
      Sql sql); List<Record> find(String sql,
      Collection params); List<Record> find(Sql sql); Map<TKey, TView> findMap(Class<TView> viewClass,
      String keyField, String sql,
      Collection params); Map<TKey, TView> findMap(Class<TView> viewClass,
      String keyField, Sql sql); List<TView> findTop(Class<TView> viewClass,
      int top,
      String sql,
      Collection params); List<TView> findTop(Class<TView> viewClass,
      int top,
      Sql sql); List<Record> findTop(int top,
      String sql,
      Collection params); List<Record> findTop(int top,
      Sql sql); TView get(Class<TView> viewClass,
      String sql,
      Collection params); TView get(Class<TView> viewClass,
      Sql sql); Record get(String sql,
      Collection params); Record get(Sql sql); TView getById(Class<TView> viewClass,
      Object id); TView getByField(Class<TView> viewClass,
      String field,
      Object param); TView getByCond(Class<TView> viewClass,
      Cond cond); TView getByCondition(Class<TView> viewClass,
      Object object); TView getByCondition(Class<TView> viewClass,
      Object object,
      String conditionGroup); List<TView> findByIds(Class<TView> viewClass,
      Collection ids); List<TView> findByField(Class<TView> viewClass,
      String field,
      Object param); List<TView> findByField(Class<TView> viewClass,
      String field,
      Collection params); List<TView> findByCond(Class<TView> viewClass,
      Cond cond); List<TView> findByCondition(Class<TView> viewClass,
      Object object); List<TView> findByCondition(Class<TView> viewClass,
      Object object,
      String conditionGroup); long count(String sql,
      Collection params); long count(Sql sql); long countByCond(Class<TView> viewClass,
      Cond cond); boolean exists(Sql sql); boolean exists(String sql,
      Collection params); boolean existsByCond(Class<TView> viewClass,
      Cond cond); PageLite<TView> findPageLite(Class<TView> viewClass,
      String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<TView> findPageLite(Class<TView> viewClass,
      Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<TView> findPageLite(Class<TView> viewClass,
      String sql,
      Collection params,
      IPageable pageable); PageLite<TView> findPageLite(Class<TView> viewClass,
      Sql sql,
      IPageable pageable); PageLite<Record> findPageLite(String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<Record> findPageLite(Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<Record> findPageLite(String sql,
      Collection params,
      IPageable pageable); PageLite<Record> findPageLite(Sql sql,
      IPageable pageable); Page<TView> findPage(Class<TView> viewClass,
      String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<TView> findPage(Class<TView> viewClass,
      Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<TView> findPage(Class<TView> viewClass,
      String sql,
      Collection params,
      IPageable pageable); Page<TView> findPage(Class<TView> viewClass,
      Sql sql,
      IPageable pageable); Page<Record> findPage(String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<Record> findPage(Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<Record> findPage(String sql,
      Collection params,
      IPageable pageable); Page<Record> findPage(Sql sql,
      IPageable pageable); int execute(String sql,
      Collection params); int execute(Sql sql); int[] execute(List<Sql> sqlList); int create(Class<TModel> modelClass,
      TModel entity); long snowFlakeId(); int create(Class<TModel> modelClass,
      Collection<TModel> entities); String uuid(); int update(Class<TModel> modelClass,
      TModel entity); int updateIgnoreNull(Class<TModel> modelClass,
      TModel entity); int update(Class<TModel> modelClass,
      TModel entity,
      String[] columns); int update(Class<TModel> modelClass,
      Collection<TModel> entities,
      String[] columns); int update(Class<TModel> modelClass,
      Collection<TModel> entities); int delete(Class<TModel> modelClass,
      TModel entity); int delete(Class<TModel> modelClass,
      Collection<TModel> entities); int deleteByIds(Class<TModel> modelClass,
      Collection ids); int deleteById(Class<TModel> modelClass,
      Object id); int deleteByCond(Class<TModel> modelClass,
      Cond cond); }
DB { public <TModel> int create(Class<TModel> modelClass, TModel entity) { int result = 0; if (entity == null) { return result; } ClassMeta classMeta = Metadata.get(modelClass); Attribute primaryKey = classMeta.checkPrimaryKey(); GenerationType strategy; String pkFieldName; boolean autoGeneratedPK = false; Sql sql = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; pkFieldName = primaryKey.getFieldName(); strategy = classMeta.getStrategy(); if (strategy == GenerationType.IDENTITY) { autoGeneratedPK = true; } else { Object pkVal = ReflectUtil.getFieldValue(entity, pkFieldName); if (pkVal == null) { if (strategy == GenerationType.ASSIGNED) { throw new DBException("Assigned ID can not be null."); } else if (strategy == GenerationType.UUID) { pkVal = uuid(); } else if (strategy == GenerationType.SNOW_FLAKE) { pkVal = snowFlakeId(); } ReflectUtil.setFieldValue(entity, pkFieldName, pkVal); } } sql = getDialect().insert(modelClass, entity); try { getSqlLog().showSql(sql.getSqlString(), sql.getParams()); conn = this.open(); pstmt = DBUtil.getPstForUpdate(conn, autoGeneratedPK, sql.getSqlString(), sql.getParams()); result = DBUtil.executeUpdate(pstmt); getSqlLog().showAffectedRows(result); if (autoGeneratedPK) { rs = pstmt.getGeneratedKeys(); if (rs.next()) { ReflectUtil.setFieldValue(entity, pkFieldName, rs.getObject(1)); } } } catch (Exception ex) { throw new DBException(ex); } finally { DBUtil.close(pstmt, rs); this.close(); } return result; } private  DB(DBTemplate dbTemplate); static DB conn(DBTemplate dbTemplate); static DB conn(String dsName); static DB conn(); R tx(Function<DB, R> function); void tx(Consumer<DB> consumer); Connection open(); void close(); void commit(); void rollback(); List<TView> find(Class<TView> viewClass,
      String sql,
      Collection params); List<TView> find(Class<TView> viewClass,
      String sql); List<TView> find(Class<TView> viewClass,
      Sql sql); List<Record> find(String sql,
      Collection params); List<Record> find(Sql sql); Map<TKey, TView> findMap(Class<TView> viewClass,
      String keyField, String sql,
      Collection params); Map<TKey, TView> findMap(Class<TView> viewClass,
      String keyField, Sql sql); List<TView> findTop(Class<TView> viewClass,
      int top,
      String sql,
      Collection params); List<TView> findTop(Class<TView> viewClass,
      int top,
      Sql sql); List<Record> findTop(int top,
      String sql,
      Collection params); List<Record> findTop(int top,
      Sql sql); TView get(Class<TView> viewClass,
      String sql,
      Collection params); TView get(Class<TView> viewClass,
      Sql sql); Record get(String sql,
      Collection params); Record get(Sql sql); TView getById(Class<TView> viewClass,
      Object id); TView getByField(Class<TView> viewClass,
      String field,
      Object param); TView getByCond(Class<TView> viewClass,
      Cond cond); TView getByCondition(Class<TView> viewClass,
      Object object); TView getByCondition(Class<TView> viewClass,
      Object object,
      String conditionGroup); List<TView> findByIds(Class<TView> viewClass,
      Collection ids); List<TView> findByField(Class<TView> viewClass,
      String field,
      Object param); List<TView> findByField(Class<TView> viewClass,
      String field,
      Collection params); List<TView> findByCond(Class<TView> viewClass,
      Cond cond); List<TView> findByCondition(Class<TView> viewClass,
      Object object); List<TView> findByCondition(Class<TView> viewClass,
      Object object,
      String conditionGroup); long count(String sql,
      Collection params); long count(Sql sql); long countByCond(Class<TView> viewClass,
      Cond cond); boolean exists(Sql sql); boolean exists(String sql,
      Collection params); boolean existsByCond(Class<TView> viewClass,
      Cond cond); PageLite<TView> findPageLite(Class<TView> viewClass,
      String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<TView> findPageLite(Class<TView> viewClass,
      Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<TView> findPageLite(Class<TView> viewClass,
      String sql,
      Collection params,
      IPageable pageable); PageLite<TView> findPageLite(Class<TView> viewClass,
      Sql sql,
      IPageable pageable); PageLite<Record> findPageLite(String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<Record> findPageLite(Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); PageLite<Record> findPageLite(String sql,
      Collection params,
      IPageable pageable); PageLite<Record> findPageLite(Sql sql,
      IPageable pageable); Page<TView> findPage(Class<TView> viewClass,
      String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<TView> findPage(Class<TView> viewClass,
      Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<TView> findPage(Class<TView> viewClass,
      String sql,
      Collection params,
      IPageable pageable); Page<TView> findPage(Class<TView> viewClass,
      Sql sql,
      IPageable pageable); Page<Record> findPage(String sql,
      Collection params,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<Record> findPage(Sql sql,
      boolean enablePage,
      int currentPage,
      int pageSize); Page<Record> findPage(String sql,
      Collection params,
      IPageable pageable); Page<Record> findPage(Sql sql,
      IPageable pageable); int execute(String sql,
      Collection params); int execute(Sql sql); int[] execute(List<Sql> sqlList); int create(Class<TModel> modelClass,
      TModel entity); long snowFlakeId(); int create(Class<TModel> modelClass,
      Collection<TModel> entities); String uuid(); int update(Class<TModel> modelClass,
      TModel entity); int updateIgnoreNull(Class<TModel> modelClass,
      TModel entity); int update(Class<TModel> modelClass,
      TModel entity,
      String[] columns); int update(Class<TModel> modelClass,
      Collection<TModel> entities,
      String[] columns); int update(Class<TModel> modelClass,
      Collection<TModel> entities); int delete(Class<TModel> modelClass,
      TModel entity); int delete(Class<TModel> modelClass,
      Collection<TModel> entities); int deleteByIds(Class<TModel> modelClass,
      Collection ids); int deleteById(Class<TModel> modelClass,
      Object id); int deleteByCond(Class<TModel> modelClass,
      Cond cond);  }