HIbernate 查询拼接参数

简介: public List findByEid(List trailids, String eid) { // TODO Auto-generated method stub String hql = " from TrailTestModel where 1=1 "; ArrayList cou...

public List<TrailTestModel> findByEid(List<String> trailids, String eid) {
// TODO Auto-generated method stub
String hql = " from TrailTestModel where 1=1 ";
ArrayList<String> count = new ArrayList<String>();
if (trailids != null && trailids.size() > 0) {
hql += " and trail.uid in (:trailids) ";
}
if (StringUtils.isNotBlank(eid)) {
hql += " and employee.uid = :eid ";
count.add(eid);
}
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
Object[] arr = new Object[trailids.size()];
for (int i = 0; i < trailids.size(); i++) {
arr[i] = trailids.get(i);
}
if (trailids != null && trailids.size() > 0) {
query.setParameterList("trailids", arr);
}
if (StringUtils.isNotBlank(eid)) {
query.setParameter("eid", eid);
}
List<TrailTestModel> list = query.list();
return list;
}

 

 

---------------------------------------------------------------------------------------------------------------------------

@Override
public Map<String, List<ZhuanTiModel>> list(Integer currentNo, Integer pageSize, String qc, String yunying_name, Integer toufang_state,
Integer page_type, Integer zhuanti_state, Integer qcstate, String dept_id) {
// TODO Auto-generated method stub
HashMap<String, List<ZhuanTiModel>> map = new HashMap<String, List<ZhuanTiModel>>();
List<Object> count = new ArrayList<Object>();
String sql = "select * from t_zhuanti where 1=1 ";
if (StringUtils.isNotBlank(yunying_name)) {
sql += " and yunying_name like ? ";
count.add("%"+yunying_name+"%");
}
if (StringUtils.isNotBlank(qc)) {
sql += " and qc_name = ? ";
count.add(qc);
}
if (toufang_state != null) {
sql += " and toufang_state= ? ";
count.add(toufang_state);
}
if (zhuanti_state != null) {
sql += " and zhuanti_url_state=? ";
count.add(zhuanti_state);
}
if (page_type != null) {
sql += " and page_type=? ";
count.add(page_type);
}
if (qcstate != null) {
sql += " and qc_state=? ";
count.add(qcstate);
}
if (StringUtils.isNotBlank(dept_id)) {
sql += " and dept_id=? ";
count.add(dept_id);
}
sql += " order by create_time desc";
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql).addEntity(ZhuanTiModel.class);
for (int i = 0; i < count.size(); i++) {
query.setParameter(i, count.get(i));
}
List<ZhuanTiModel> total = query.list();
query.setFirstResult((currentNo - 1) * pageSize);
query.setMaxResults(pageSize);
List<ZhuanTiModel> list = query.list();
map.put("total", total);
map.put("list", list);
return map;
}

目录
相关文章
|
4月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Dept.java)
Hibernate中使用Criteria查询及注解——(Dept.java)
|
17天前
|
API Java 数据库连接
从平凡到卓越:Hibernate Criteria API 让你的数据库查询瞬间高大上,彻底告别复杂SQL!
【8月更文挑战第31天】构建复杂查询是数据库应用开发中的常见需求。Hibernate 的 Criteria API 以其强大和灵活的特点,允许开发者以面向对象的方式构建查询逻辑,同时具备 SQL 的表达力。本文将介绍 Criteria API 的基本用法并通过示例展示其实际应用。此 API 通过 API 构建查询条件而非直接编写查询语句,提高了代码的可读性和安全性。无论是简单的条件过滤还是复杂的分页和连接查询,Criteria API 均能胜任,有助于提升开发效率和应用的健壮性。
26 0
|
27天前
|
SQL Java 数据库连接
|
27天前
|
缓存 Java 数据库连接
什么是 Hibernate 查询语言或 HQL?
【8月更文挑战第21天】
41 0
|
27天前
|
SQL Java 数据库连接
在 Hibernate 中何时使用条件查询?
【8月更文挑战第21天】
23 0
|
27天前
|
缓存 Java 数据库连接
Hibernate 中的查询缓存是什么?
【8月更文挑战第21天】
22 0
|
27天前
|
SQL 安全 Java
|
4月前
使用Hibernate-Validate进行参数校验
使用Hibernate-Validate进行参数校验
55 3
|
4月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
|
4月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——( EmpCondition)
Hibernate中使用Criteria查询及注解——( EmpCondition)