jpa 托管_JPA EntityManager详解

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值