my de sheng ho

/**
* 根据ID删除记录
*
* @param id
*/
public void removeByIdForAnnotation(Serializable id) {
T o = get(id);
if (o.getClass().isAnnotationPresent(Undeletable.class)) {
Undeletable anno = (Undeletable) o.getClass().getAnnotation(
Undeletable.class);
try {
Method method = o.getClass().getDeclaredMethod(
"set" + StringUtils.capitalize(anno.status()),
String.class);
try {
method.invoke(o, new Object[] { anno.value() });
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
save(o);
} else {
remove(o);
}
}

/**
* hql 删除
*
* @param hql
*/
public void deleteByHql(String hql) {
Query query = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createQuery(hql);
query.executeUpdate();
}

/**
* 条件查询(带缓存)
*
* @param hsql
* @param values
* @return 数据
*/
@SuppressWarnings("unchecked")
public List<T> findForCache(String hsql, Object... values) {
Query query = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createQuery(hsql).setCacheable(true);
if (values == null || values.length == 0) {
return query.list();
} else {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
return query.list();
}
}

/**
* 条件查询QBC(带缓存)
*
* @param criterions
* @return List<T>
*/
@SuppressWarnings("unchecked")
public List<T> findByQbcForCache(Criterion... criterions) {
Criteria criteria = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createCriteria(getEntityClass());
criteria.setCacheable(true);
if (criterions != null && criterions.length != 0) {
for (Criterion c : criterions) {
criteria.add(c);
}
}
return criteria.list();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值