
com.gitee.qdbp.jdbc.stream.CrudOnAfterSelectStream Maven / Gradle / Ivy
package com.gitee.qdbp.jdbc.stream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import com.gitee.qdbp.able.enums.LogicType;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.condition.DbWhere;
import com.gitee.qdbp.able.jdbc.condition.DbWhereForFieldCore;
import com.gitee.qdbp.able.jdbc.condition.DbWhereForOnFieldless;
import com.gitee.qdbp.able.jdbc.condition.DbWhereForStartCore;
import com.gitee.qdbp.able.jdbc.condition.SkipType;
import com.gitee.qdbp.able.jdbc.fields.Fields;
import com.gitee.qdbp.jdbc.api.CrudDao;
import com.gitee.qdbp.jdbc.exception.DbErrorCode;
import com.gitee.qdbp.jdbc.model.SimpleFieldColumn;
import com.gitee.qdbp.jdbc.utils.ParseTools;
import com.gitee.qdbp.tools.utils.VerifyTools;
/**
* Select查询后续操作
* 后续操作有: where / orderBy / paging / list ( 还有find在where后续 )
*
* @author zhaohuihua
* @version 20210529
*/
public class CrudOnAfterSelectStream extends CrudOnSelectWhereByStream {
CrudOnAfterSelectStream(CrudDao dao, Fields fields) {
super(dao, fields, DbWhere.NONE);
}
/**
* Where条件
*
List<SysUser> users = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.where("userType").equals(1)
.and("createTime").between(DateTools.addDay(now, -1), DateTools.addDay(now, +1))
.and().subCondition((w) -> {
w.on("userCode").like("test")
.or("realName").like("test");
})
.end()
.orderBy("createTime desc")
.list();
*
*
* @param fieldName 字段名称
* @return 返回后续流式操作对象
*/
public DbWhereForFieldCore> where(String fieldName) {
DbWhere where = DbWhere.newEmptiableWhere();
CrudOnSelectWhereByStream next = new CrudOnSelectWhereByStream<>(dao, fields, where);
DbWhereForStartCore> start = new DbWhereForStartCore<>(next, where);
return new DbWhereForFieldCore<>(start, SkipType.NONE, LogicType.AND, fieldName);
}
/**
* 查询条件
*
List<SysUser> users = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.where().subCondition((w) -> {
w.on("userCode").like("test")
.or("realName").like("test");
})
.and("userType").equals(1)
.and("createTime").between(DateTools.addDay(now, -1), DateTools.addDay(now, +1))
.end()
.orderBy("createTime desc")
.list();
*
*
* @return 返回后续流式操作对象
*/
public DbWhereForOnFieldless> where() {
DbWhere where = DbWhere.newEmptiableWhere();
CrudOnSelectWhereByStream next = new CrudOnSelectWhereByStream<>(dao, fields, where);
DbWhereForStartCore> start = new DbWhereForStartCore<>(next, where);
return new DbWhereForOnFieldless<>(start, SkipType.NONE);
}
/**
* 设置Where条件
*
* @param where Where条件
* @return 返回后续流式操作对象
*/
public CrudOnSelectWhereByStream whereBy(DbWhere where) {
return new CrudOnSelectWhereByStream<>(dao, fields, where);
}
/**
* 从实体类构建Where对象
*
List<SysUser> users = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.whereBy(user)
.list();
*
*
* @param entity 实体类
* @return 返回后续流式操作对象
* @see ParseTools#parseBeanToDbWhere(Object)
*/
public CrudOnSelectWhereByStream whereBy(T entity) {
DbWhere where = dao.plugins().parseTools().parseBeanToDbWhere(entity);
return new CrudOnSelectWhereByStream<>(dao, fields, where);
}
/**
* 从Map构建Where对象
*
List<SysUser> users = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.whereBy(map)
.list();
*
* 只会包含clazz注解中通过@Column指定的字段名
* 应注意, 此时参数由前端传入, 条件不可控, 也有可能条件为空, 需要仔细检查条件内容, 防止越权操作
* 转换规则:
fieldName$Equals(=), fieldName$NotEquals(!=),
fieldName$LessThen(<), fieldName$LessEqualsThen(<=),
fieldName$GreaterThen(>), fieldName$GreaterEqualsThen(>=),
fieldName$IsNull, fieldName$IsNotNull,
fieldName$Like, fieldName$NotLike, fieldName$Starts, fieldName$Ends,
fieldName$In, fieldName$NotIn, fieldName$Between
*
*
* @param map Map参数
* @return 返回后续流式操作对象
* @see ParseTools#parseBeanToDbWhere(Object)
*/
public CrudOnSelectWhereByStream whereBy(Map map) {
DbWhere where = dao.plugins().parseTools().parseBeanToDbWhere(map);
return new CrudOnSelectWhereByStream<>(dao, fields, where);
}
/**
* 以ID列表作为查询条件
*
List<SysUser> users = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.whereByIds("U0000001", "U0000002", ...)
.orderBy("createTime desc")
.list();
*
*
* @param ids ID列表
* @return 返回后续流式操作对象
*/
public CrudOnSelectWhereByStream whereByIds(String... ids) {
VerifyTools.requireNotBlank(ids, "ids");
return doWhereByIds(Arrays.asList(ids));
}
/**
* 以ID列表作为查询条件
*
List<SysUser> users = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.whereByIds("U0000001", "U0000002", ...)
.orderBy("createTime desc")
.list();
*
*
* @param ids ID列表
* @return 返回后续流式操作对象
*/
public CrudOnSelectWhereByStream whereByIds(Collection extends Serializable> ids) {
VerifyTools.requireNotBlank(ids, "ids");
return doWhereByIds(ids);
}
protected CrudOnSelectWhereByStream doWhereByIds(Collection extends Serializable> ids) {
SimpleFieldColumn pk = dao.sqlHelper().fragment().getPrimaryKey();
if (pk == null) { // 没有找到主键字段
String details = "UnsupportedWhereByIds, class=" + dao.getBeanClass().getName();
throw new ServiceException(DbErrorCode.DB_PRIMARY_KEY_FIELD_IS_UNRESOLVED).setDetails(details);
}
String primaryField = pk.getFieldName();
DbWhere where = DbWhere.newEmptiableWhere();
where.on(primaryField).in(ids);
return new CrudOnSelectWhereByStream<>(dao, fields, where);
}
/**
* 根据主键编号获取对象
*
SysUser user = qdbcBoot.crudStream(SysUser.class)
.select("id,userCode,realName") // 只查某些字段
// .selectExclude("password") // 排除掉密码字段
.findById("U0000001");
*
*
* @param id 主键编号
* @return 实体对象
*/
public T findById(Serializable id) {
VerifyTools.requireNotBlank(id, "id");
return dao.findById(fields, id);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy