// 默认顺序createtime desc public List<T> findByMapWithCond(Map<String, Object> params, String orderBy, Boolean asc) { StringBuilder hql = new StringBuilder("from " + className); String orderStr; if (null == orderBy) { orderStr = " order by createtime desc"; } else { String ascStr = asc ? " asc" : " desc"; orderStr = " order by " + orderBy + ascStr; } Map<String, Object> alterParams = new HashMap<>(); Query query; if (null != params && !params.isEmpty()) { hql.append(" where "); for (String field : params.keySet()) { int i = field.indexOf(" "); Assert.isTrue(i != -1, "Wrong condition, must have space inside!"); String ramdonName = "_" + Utils.getRandomString(8); hql.append(field).append(" :").append(ramdonName).append(" and "); if (field.contains("like")) { String likeStr = "%" + params.get(field) + "%"; alterParams.put(ramdonName, likeStr); } else alterParams.put(ramdonName, params.get(field)); } hql.append("1=1"); query = getSession().createQuery(hql.append(orderStr).toString()); for (String field : alterParams.keySet()) query.setParameter(field, alterParams.get(field)); } else query = getSession().createQuery(hql.append(orderStr).toString()); return query.list(); }
public void txDelete(String idList, Person oper) { delete(idList); Syslog syslog = new Syslog(); syslog.setUserid(oper.getName()); syslog.setName(OperEnum.DELETE.toString()); syslog.setComment("用户" + oper.getName() + "删除" + className); syslog.setCreatetime(Utils.getNow()); syslog.setId(Utils.getNewUUID()); getSession().save(syslog); }
public void txUpdate(T entity, Person oper) { saveOrUpdate(entity); Syslog syslog = new Syslog(); syslog.setUserid(oper.getName()); syslog.setName(OperEnum.UPDATE.toString()); syslog.setComment("用户" + oper.getName() + "修改" + className + "-->" + entity.getName()); syslog.setCreatetime(Utils.getNow()); syslog.setId(Utils.getNewUUID()); getSession().save(syslog); }
public Serializable txSave(T entity, Person person) { String id = (String) save(entity); Syslog syslog = new Syslog(); syslog.setUserid(person.getName()); syslog.setName(OperEnum.CREATE.toString()); syslog.setComment("用户" + person.getName() + "添加" + className + "-->" + entity.getName()); syslog.setCreatetime(Utils.getNow()); syslog.setId(Utils.getNewUUID()); getSession().save(syslog); return id; }
@Override public void saveOrUpdate(T entity, boolean ct) { if (ct) { if (entity.getId() == null) save(entity); else { entity.setCreatetime(Utils.getNow()); getSession().saveOrUpdate(entity); } } else getSession().saveOrUpdate(entity); }
public Map<String, Object> findByMapWithCond( final Map<String, Object> params, int page, int rows, String order, boolean asc) throws HibernateException { StringBuilder hql = new StringBuilder("from " + className); Query query, countQuery; Long total; Map<String, Object> alterParams = new HashMap<>(); if (null != params && !params.isEmpty()) { hql.append(" where "); for (String field : params.keySet()) { int i = field.indexOf(" "); Assert.isTrue(i != -1, "Wrong condition, must have space inside!"); String ramdonName = "_" + Utils.getRandomString(8); hql.append(field).append(" :").append(ramdonName).append(" and "); alterParams.put(ramdonName, params.get(field)); } hql.append("1=1"); countQuery = getSession().createQuery("SELECT count(*) " + hql.toString()); if (null != order) { hql.append(" order by ").append(order); if (asc) hql.append(" asc"); else hql.append(" desc"); } query = getSession().createQuery(hql.toString()); for (String field : alterParams.keySet()) { query.setParameter(field, alterParams.get(field)); countQuery.setParameter(field, alterParams.get(field)); } } else { countQuery = getSession().createQuery("SELECT count(*) " + hql.toString()); if (null != order) { hql.append(" order by ").append(order); if (asc) hql.append(" asc"); else hql.append(" desc"); } query = getSession().createQuery(hql.toString()); } total = (Long) countQuery.uniqueResult(); if (page > 0 && rows > 0) { query.setFirstResult((page - 1) * rows).setMaxResults(rows); } Map<String, Object> result = new HashMap<String, Object>(); result.put("total", total); result.put("rows", query.list()); return result; }
@Override public Serializable save(T entity) { if (entity.getId() == null) entity.setId(Utils.getNewUUID()); if (entity.getCreatetime() == null) entity.setCreatetime(Utils.getNow()); return getSession().save(entity); }