|
|
@ -4,12 +4,14 @@ import cn.estsh.i3plus.pojo.base.common.Pager;
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
|
|
|
|
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository;
|
|
|
|
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository;
|
|
|
|
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
|
|
|
|
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
|
|
|
|
|
|
|
|
import org.hibernate.NonUniqueResultException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
|
|
|
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
import javax.persistence.Id;
|
|
|
|
import javax.persistence.Id;
|
|
|
|
|
|
|
|
import javax.persistence.NoResultException;
|
|
|
|
import javax.persistence.Query;
|
|
|
|
import javax.persistence.Query;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
@ -364,8 +366,12 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
String queryString = "from " + persistentClass.getSimpleName() + " as model where model." + propertyName + "= :" + propertyName;
|
|
|
|
String queryString = "from " + persistentClass.getSimpleName() + " as model where model." + propertyName + "= :" + propertyName;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return (T) entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult();
|
|
|
|
return (T) entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult();
|
|
|
|
}catch(Exception e){
|
|
|
|
}catch(NoResultException ne){
|
|
|
|
|
|
|
|
LOGGER.error("数据不存在,prop:{},value:{}",propertyName,value,ne);
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}catch(NonUniqueResultException ex){
|
|
|
|
|
|
|
|
LOGGER.error("查询单条记录,但出现多条。prop:{},value:{}",propertyName,value,ex);
|
|
|
|
|
|
|
|
throw new RuntimeException("存在多条记录:" + ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -397,25 +403,37 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
try{
|
|
|
|
return (T) queryObject.getSingleResult();
|
|
|
|
return (T) queryObject.getSingleResult();
|
|
|
|
}catch(Exception e){
|
|
|
|
}catch(NoResultException ne){
|
|
|
|
|
|
|
|
LOGGER.error("数据不存在",ne);
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}catch(NonUniqueResultException ex){
|
|
|
|
|
|
|
|
LOGGER.error("查询单条记录,但出现多条。",ex);
|
|
|
|
|
|
|
|
throw new RuntimeException("存在多条记录:" + ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int listCount() {
|
|
|
|
public int listCount() {
|
|
|
|
Long count = entityManager.createQuery("select count(distinct model) from " + persistentClass.getName() + " as model",Long.class)
|
|
|
|
try{
|
|
|
|
.getSingleResult();
|
|
|
|
Long count = entityManager.createQuery("select count(distinct model) from " + persistentClass.getName() + " as model",Long.class)
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
.getSingleResult();
|
|
|
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int findByPropertyCount(String propertyName, Object value) {
|
|
|
|
public int findByPropertyCount(String propertyName, Object value) {
|
|
|
|
String queryString = "select count(distinct model) from " + persistentClass.getName() + " as model where model." + propertyName + "= :" + propertyName;
|
|
|
|
try{
|
|
|
|
Long count = entityManager.createQuery(queryString, Long.class)
|
|
|
|
String queryString = "select count(distinct model) from " + persistentClass.getName() + " as model where model." + propertyName + "= :" + propertyName;
|
|
|
|
.setParameter(propertyName, value)
|
|
|
|
Long count = entityManager.createQuery(queryString, Long.class)
|
|
|
|
.getSingleResult();
|
|
|
|
.setParameter(propertyName, value)
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
.getSingleResult();
|
|
|
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -444,8 +462,12 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Long count = (Long) queryObject.getSingleResult();
|
|
|
|
try{
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
Long count = (Long) queryObject.getSingleResult();
|
|
|
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -560,8 +582,12 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Long count = entityManager.createQuery(queryString.toString(),Long.class).getSingleResult();
|
|
|
|
try{
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
Long count = entityManager.createQuery(queryString.toString(),Long.class).getSingleResult();
|
|
|
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -617,15 +643,22 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
if (hqlWhere != null && hqlWhere.length() > 0) {
|
|
|
|
if (hqlWhere != null && hqlWhere.length() > 0) {
|
|
|
|
queryString.append(hqlWhere);
|
|
|
|
queryString.append(hqlWhere);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
Long count = entityManager.createQuery(queryString.toString(), Long.class).getSingleResult();
|
|
|
|
Long count = entityManager.createQuery(queryString.toString(), Long.class).getSingleResult();
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int findByHqlCount(String hql) {
|
|
|
|
public int findByHqlCount(String hql) {
|
|
|
|
Long count = entityManager.createQuery(hql,Long.class).getSingleResult();
|
|
|
|
try{
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
Long count = entityManager.createQuery(hql,Long.class).getSingleResult();
|
|
|
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -636,8 +669,11 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
queryObject.setParameter(paramName[i], paramValue[i].toString());
|
|
|
|
queryObject.setParameter(paramName[i], paramValue[i].toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
return (int) queryObject.getSingleResult();
|
|
|
|
return (int) queryObject.getSingleResult();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -702,25 +738,44 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int findBySqlCount(String sql) {
|
|
|
|
public int findBySqlCount(String sql) {
|
|
|
|
Long count = (Long) entityManager.createNativeQuery("select count(*) from ( " + sql + " ) as usertable",Long.class).getSingleResult();
|
|
|
|
try{
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
Long count = (Long) entityManager
|
|
|
|
|
|
|
|
.createNativeQuery("select count(*) from ( " + sql + " ) as usertable",Long.class)
|
|
|
|
|
|
|
|
.getSingleResult();
|
|
|
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public double findBySqlSumDouble(String sql, String columnName) {
|
|
|
|
public double findBySqlSumDouble(String sql, String columnName) {
|
|
|
|
return (double) entityManager.createNativeQuery("select sum(" + columnName + ") from ( " + sql + " ) as usertable",Double.class)
|
|
|
|
try{
|
|
|
|
|
|
|
|
return (double) entityManager
|
|
|
|
|
|
|
|
.createNativeQuery("select sum(" + columnName + ") from ( " + sql + " ) as usertable",Double.class)
|
|
|
|
.getSingleResult();
|
|
|
|
.getSingleResult();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public double findByHqlDouble(String hql) {
|
|
|
|
public double findByHqlDouble(String hql) {
|
|
|
|
Double result = entityManager.createQuery(hql,Double.class).getSingleResult();
|
|
|
|
try{
|
|
|
|
return result == null ? 0 : result;
|
|
|
|
Double result = entityManager.createQuery(hql,Double.class).getSingleResult();
|
|
|
|
|
|
|
|
return result == null ? 0 : result;
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public long findByHqlLong(String hql) {
|
|
|
|
public long findByHqlLong(String hql) {
|
|
|
|
return entityManager.createQuery(hql,Long.class).getSingleResult();
|
|
|
|
try{
|
|
|
|
|
|
|
|
return entityManager.createQuery(hql,Long.class).getSingleResult();
|
|
|
|
|
|
|
|
}catch(NoResultException e){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|