Search in sources :

Example 6 with Db

use of cn.hutool.db.Db in project Jpom by dromara.

the class BaseDbCommonService method insert.

/**
 * 插入数据
 *
 * @param t 数据
 */
public void insert(T t) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return;
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        Entity entity = this.dataBeanToEntity(t);
        db.insert(entity);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : Entity(cn.hutool.db.Entity) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 7 with Db

use of cn.hutool.db.Db in project Jpom by dromara.

the class BaseDbCommonService method listPage.

/**
 * 分页查询
 *
 * @param where 条件
 * @param page  分页
 * @return 结果
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public PageResultDto<T> listPage(Entity where, Page page) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return PageResultDto.EMPTY;
    }
    where.setTableName(getTableName());
    PageResult<Entity> pageResult;
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        pageResult = db.page(where, page);
    } catch (Exception e) {
        throw warpException(e);
    }
    // 
    List<T> list = pageResult.stream().map(entity -> {
        T entityToBean = this.entityToBean(entity, this.tClass);
        this.fillSelectResult(entityToBean);
        return entityToBean;
    }).collect(Collectors.toList());
    PageResultDto<T> pageResultDto = new PageResultDto(pageResult);
    pageResultDto.setResult(list);
    if (pageResultDto.isEmpty() && pageResultDto.getPage() > 1) {
        Assert.state(pageResultDto.getTotal() <= 0, "筛选的分页有问题,当前页码查询不到任何数据");
    }
    return pageResultDto;
}
Also used : PageResultDto(io.jpom.model.PageResultDto) PageResult(cn.hutool.db.PageResult) ExceptionUtil(cn.hutool.core.exceptions.ExceptionUtil) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) BeanUtil(cn.hutool.core.bean.BeanUtil) Page(cn.hutool.db.Page) Order(cn.hutool.db.sql.Order) JpomRuntimeException(io.jpom.system.JpomRuntimeException) LinkedHashMap(java.util.LinkedHashMap) TypeUtil(cn.hutool.core.util.TypeUtil) Map(java.util.Map) PageUtil(cn.hutool.core.util.PageUtil) DbConfig(io.jpom.system.db.DbConfig) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Db(cn.hutool.db.Db) Consumer(java.util.function.Consumer) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Condition(cn.hutool.db.sql.Condition) CopyOptions(cn.hutool.core.bean.copier.CopyOptions) Entity(cn.hutool.db.Entity) Assert(org.springframework.util.Assert) Entity(cn.hutool.db.Entity) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException) PageResultDto(io.jpom.model.PageResultDto)

Example 8 with Db

use of cn.hutool.db.Db in project Jpom by dromara.

the class BaseDbCommonService method getByKey.

/**
 * 根据主键查询实体
 *
 * @param keyValue 主键值
 * @return 数据
 */
public T getByKey(String keyValue, boolean fill, Consumer<Entity> consumer) {
    if (StrUtil.isEmpty(keyValue)) {
        return null;
    }
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return null;
    }
    Entity where = new Entity(tableName);
    where.set(key, keyValue);
    Db db = Db.use();
    db.setWrapper((Character) null);
    if (consumer != null) {
        consumer.accept(where);
    }
    Entity entity;
    try {
        entity = db.get(where);
    } catch (Exception e) {
        throw warpException(e);
    }
    T entityToBean = this.entityToBean(entity, this.tClass);
    if (fill) {
        this.fillSelectResult(entityToBean);
    }
    return entityToBean;
}
Also used : Entity(cn.hutool.db.Entity) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 9 with Db

use of cn.hutool.db.Db in project Jpom by dromara.

the class BaseDbCommonService method count.

/**
 * 查询记录条数
 *
 * @param where 条件
 * @return count
 */
public long count(Entity where) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return 0;
    }
    where.setTableName(getTableName());
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        return db.count(where);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 10 with Db

use of cn.hutool.db.Db in project Jpom by dromara.

the class BaseDbCommonService method queryList.

/**
 * 查询列表
 *
 * @param where 条件
 * @return List
 */
public List<Entity> queryList(Entity where) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return null;
    }
    where.setTableName(getTableName());
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        return db.find(where);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Aggregations

Db (cn.hutool.db.Db)11 JpomRuntimeException (io.jpom.system.JpomRuntimeException)10 JdbcSQLNonTransientException (org.h2.jdbc.JdbcSQLNonTransientException)10 Entity (cn.hutool.db.Entity)5 StrUtil (cn.hutool.core.util.StrUtil)2 DbConfig (io.jpom.system.db.DbConfig)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Slf4j (lombok.extern.slf4j.Slf4j)2 BeanUtil (cn.hutool.core.bean.BeanUtil)1 CopyOptions (cn.hutool.core.bean.copier.CopyOptions)1 CollUtil (cn.hutool.core.collection.CollUtil)1 ExceptionUtil (cn.hutool.core.exceptions.ExceptionUtil)1 IoUtil (cn.hutool.core.io.IoUtil)1 Console (cn.hutool.core.lang.Console)1 CharsetUtil (cn.hutool.core.util.CharsetUtil)1 PageUtil (cn.hutool.core.util.PageUtil)1 TypeUtil (cn.hutool.core.util.TypeUtil)1 SecureUtil (cn.hutool.crypto.SecureUtil)1