CRUD

The Definition of DataAccess Interface

DataAccess interface provides methods for accessing the database.

public interface DataAccess<E extends Persistable<I>, I extends Serializable, Q extends DoytoQuery> {
    List<E> query(Q query);
    long count(Q query);
    PageList<E> page(Q query);
    <V> List<V> queryColumns(Q q, Class<V> clazz, String... columns);
    List<I> queryIds(Q query);

    default E get(I id) {
        return get(IdWrapper.build(id));
    }
    E get(IdWrapper<I> w);

    default int delete(I id) {
        return delete(IdWrapper.build(id));
    }
    int delete(IdWrapper<I> w);
    int delete(Q query);

    void create(E e);
    default int batchInsert(Iterable<E> entities, String... columns) {
        int count = 0;
        for (E entity : entities) {
            create(entity);
            count++;
        }
        return count;
    }

    int update(E e);
    int patch(E e);
    int patch(E e, Q q);
}

The DataAccess interface contains methods that accept only four categories of parameters in total:

  • id - the primary key of the entity;

  • Entity - an entity object, used to map to a table name and column names;

  • Query - a query object, used to dynamically construct query conditions and pagination statements. It needs to extend PageQuery.

  • IdWrapper - a sharding primary key object, used for sharded table queries;

For the definition of Entity, refer to:

Entity Object

For the definition of Query, refer to:

Query Object

Example

The following interface calls are demonstrated based on the entity object UserEntity and the query object UserQuery:

Get

Query data by id:

Query

Query data by query conditions:

Count

Query the total number of data based on the query conditions:

Page

Paging based on the query conditions:

Delete

Delete data by id:

DeleteByQuery

Delete data by query conditions:

Create

Create one record:

CreateMulti

Create multiple records:

Update

Update all columns by id:

Patch

Update non-null columns by id:

PatchByQuery

Update non-null columns by query conditions:

Last updated