top.wboost.common.util.CommonDao Maven / Gradle / Ivy
package top.wboost.common.util;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.beanutils.ConvertUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.util.CollectionUtils;
//@Transactional
//@Repository
public class CommonDao {
@PersistenceContext
EntityManager entityManager;
/**
* * 查询数据集合
* @param sql 查询sql sql中的参数用:name格式
* @param params 查询参数map格式,key对应参数中的:name
* @param clazz 实体类型为空则直接转换为map格式
* @return
*/
@SuppressWarnings("unchecked")
public List> queryListEntity(String sql,Map params, Class> clazz){
Session session = entityManager.unwrap(org.hibernate.Session.class);
SQLQuery query = session.createSQLQuery(sql);
if (params != null) {
for (String key : params.keySet()) {
query.setParameter(key, params.get(key));
}
}
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List