Search in sources :

Example 1 with Db

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

the class BaseDbCommonService method insert.

/**
 * 插入数据
 *
 * @param t 数据
 */
public void insert(Collection<T> t) {
    if (CollUtil.isEmpty(t)) {
        return;
    }
    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 {
        List<Entity> entities = t.stream().map(this::dataBeanToEntity).collect(Collectors.toList());
        db.insert(entities);
    } 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 2 with Db

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

the class BaseDbCommonService method findByCondition.

/**
 * 查询列表
 *
 * @param wheres 条件
 * @return List
 */
public List<T> findByCondition(Condition... wheres) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return null;
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        List<Entity> entities = db.findBy(getTableName(), wheres);
        return this.entityToBeanList(entities);
    } 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 3 with Db

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

the class BaseDbCommonService method update.

/**
 * 修改数据
 *
 * @param entity 要修改的数据
 * @param where  条件
 * @return 影响行数
 */
public int update(Entity entity, 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;
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    if (where.isEmpty()) {
        throw new JpomRuntimeException("没有更新条件");
    }
    entity.setTableName(tableName);
    where.setTableName(tableName);
    try {
        return db.update(entity, where);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 4 with Db

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

the class BaseDbCommonService method del.

/**
 * 根据条件删除
 *
 * @param where 条件
 * @return 影响行数
 */
public int del(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(tableName);
    if (where.isEmpty()) {
        throw new JpomRuntimeException("没有删除条件");
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        return db.del(where);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 5 with Db

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

the class BaseDbCommonService method insert.

/**
 * 插入数据
 *
 * @param entity 要修改的数据
 * @return 影响行数
 */
public int insert(Entity entity) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return 0;
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    entity.setTableName(tableName);
    try {
        return db.insert(entity);
    } 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