packagecom.idea.repository.impl;importjava.math.BigDecimal;importjava.math.BigInteger;importjava.util.Collections;importjava.util.List;importjava.util.Map;importjavax.persistence.EntityManager;importjavax.persistence.PersistenceContext;importjavax.persistence.Query;importorg.apache.commons.collections.MapUtils;importorg.hibernate.transform.Transformers;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.data.domain.Page;importorg.springframework.data.domain.PageImpl;importorg.springframework.data.domain.Pageable;importorg.springframework.util.Assert;
@SuppressWarnings({"unchecked", "rawtypes"})public classBaseRepositoryImpl {protected final Logger LOG =LoggerFactory.getLogger(getClass());
@PersistenceContextprivateEntityManager entityManager;protected List getResultList(String sql, Mapparam) {
Query nativeQuery=entityManager.createNativeQuery(sql);//设置参数
if(MapUtils.isNotEmpty(param)) {for (Map.Entryentry : param.entrySet()) {
nativeQuery.setParameter(entry.getKey(), entry.getValue());
}
}
nativeQuery.unwrap(org.hibernate.SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List mapList=nativeQuery.getResultList();returnmapList;
}protected Object getSingleResult(String sql, Mapparam) {
Query nativeQuery=entityManager.createNativeQuery(sql);if(MapUtils.isNotEmpty(param)) {for (Map.Entryentry : param.entrySet()) {
nativeQuery.setParameter(entry.getKey(), entry.getValue());
}
}
nativeQuery.unwrap(org.hibernate.SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);returnnativeQuery.getSingleResult();
}protected Page getPageResultList(String sql, Mapparam, Pageable pageRequest) {
Query nativeQuery=entityManager.createNativeQuery(sql);//设置参数
if(MapUtils.isNotEmpty(param)) {for (Map.Entryentry : param.entrySet()) {
nativeQuery.setParameter(entry.getKey(), entry.getValue());
}
}
Assert.notNull(nativeQuery,"SQL执行");
Long total=count(sql, param);if (total == null) {return null;
}//设置分页参数
nativeQuery.setFirstResult(pageRequest.getOffset());
nativeQuery.setMaxResults(pageRequest.getPageSize());
nativeQuery.unwrap(org.hibernate.SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List mapList=nativeQuery.getResultList();
List> content = total > pageRequest.getOffset() ?mapList : Collections.emptyList();return new PageImpl<>(content, pageRequest, total);
}/*** 批量删除操作*/
public int excuteBatchDelete(String sql, Mapparam) {
Query nativeQuery=entityManager.createNativeQuery(sql);/** if (map != null && map.size() > 0) { for (Map.Entry>
* entry : map.entrySet()) { nativeQuery.setParameter(entry.getKey(),
* entry.getValue()); } }*/
if(MapUtils.isNotEmpty(param)) {for (Map.Entryentry : param.entrySet()) {
nativeQuery.setParameter(entry.getKey(), entry.getValue());
}
}returnnativeQuery.executeUpdate();
}protected Long count(String sql, Mapparam) {
String countSql= "select count(*) from (" + sql + ") a";
Query countQuery=entityManager.createNativeQuery(countSql);//设置参数
if(MapUtils.isNotEmpty(param)) {for (Map.Entryentry : param.entrySet()) {
countQuery.setParameter(entry.getKey(), entry.getValue());
}
}
Assert.notNull(countQuery,"SQL执行");
Long total= 0L;
Object totalObj=countQuery.getSingleResult();try{if (totalObj != null) {if (totalObj instanceofBigDecimal) {
BigDecimal bd=(BigDecimal) totalObj;
total=bd.longValue();
}else if (totalObj instanceofBigInteger) {
BigInteger bd=(BigInteger) totalObj;
total=bd.longValue();
}else{
total=Long.parseLong(totalObj.toString());
}
}
}catch(Exception e) {
LOG.error("不支持该数据库count返回类型!!");
}returntotal;
}
}