|
|
|
@ -1,14 +1,15 @@
|
|
|
|
|
package cn.estsh.i3plus.pojo.base.jpa.daoimpl;
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.common.Pager;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.tool.SnowflakeIdMaker;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.common.Pager;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
|
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
import javax.persistence.Id;
|
|
|
|
|
import javax.persistence.Query;
|
|
|
|
|
import javax.persistence.TypedQuery;
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.util.*;
|
|
|
|
@ -22,6 +23,8 @@ import java.util.*;
|
|
|
|
|
**/
|
|
|
|
|
public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, Serializable>
|
|
|
|
|
implements BaseRepository<T, Serializable> {
|
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(BaseRepositoryImpl.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 持久化上下文
|
|
|
|
|
*/
|
|
|
|
@ -141,6 +144,19 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void deleteByIdIn(Long[] ids) {
|
|
|
|
|
if(ids != null && ids.length > 0){
|
|
|
|
|
String hql = "delete from " + persistentClass.getName() + " o where o.id in(:ids) ";
|
|
|
|
|
Query query = entityManager.createQuery(hql);
|
|
|
|
|
query.setParameter("ids", Arrays.asList(ids));
|
|
|
|
|
|
|
|
|
|
query.executeUpdate();
|
|
|
|
|
}else{
|
|
|
|
|
throw new IllegalArgumentException("Method deleteByPropertiesIn argument is illegal! ids:" + ids);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int updateByProperties(String conditionName, Object conditionValue, String propertyName, Object propertyValue) {
|
|
|
|
|
return updateByProperties(new String[] { conditionName }, new Object[] { conditionValue }, new String[] { propertyName }, new Object[] { propertyValue });
|
|
|
|
|
}
|
|
|
|
@ -161,17 +177,20 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
&& (propertyValue.length > 0) && (propertyName.length == propertyValue.length)
|
|
|
|
|
&& (conditionValue != null) && (conditionValue.length > 0)) {
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
sb.append("update " + persistentClass.getName() + " o set ");
|
|
|
|
|
for (int i = 0; i < propertyName.length; i++) {
|
|
|
|
|
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
|
|
|
|
|
}
|
|
|
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
|
|
|
|
|
|
|
|
sb.append(" where 1=1 ");
|
|
|
|
|
appendQL(sb, conditionName, conditionValue);
|
|
|
|
|
Query query = entityManager.createQuery(sb.toString());
|
|
|
|
|
for (int i = 0; i < propertyName.length; i++) {
|
|
|
|
|
query.setParameter("p_" + propertyName[i], propertyValue[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setParameter(query, conditionName, conditionValue);
|
|
|
|
|
return query.executeUpdate();
|
|
|
|
|
} else {
|
|
|
|
@ -181,6 +200,36 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int updateByHqlWhere(String hqlWhere, String propertyName, Object propertyValue) {
|
|
|
|
|
return updateByHqlWhere(hqlWhere, new String[]{propertyName}, new Object[]{propertyValue});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int updateByHqlWhere(String hqlWhere, String[] propertyName, Object[] propertyValue) {
|
|
|
|
|
if ((propertyName != null) && (propertyName.length > 0) && (propertyValue != null)
|
|
|
|
|
&& (propertyValue.length > 0) && (propertyName.length == propertyValue.length)) {
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
sb.append("update " + persistentClass.getName() + " o set ");
|
|
|
|
|
for (int i = 0; i < propertyName.length; i++) {
|
|
|
|
|
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
|
|
|
|
|
}
|
|
|
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
|
|
|
|
|
|
|
|
sb.append(" where 1=1 ");
|
|
|
|
|
sb.append(hqlWhere);
|
|
|
|
|
Query query = entityManager.createQuery(sb.toString());
|
|
|
|
|
for (int i = 0; i < propertyName.length; i++) {
|
|
|
|
|
query.setParameter("p_" + propertyName[i], propertyValue[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.executeUpdate();
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("Method updateByProperties argument is illegal! propertyName:" + propertyName + ",propertyValue:" + propertyValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public T getById(long id) {
|
|
|
|
|
return this.getOne(id);
|
|
|
|
|
}
|
|
|
|
@ -292,7 +341,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
return queryObject.getResultList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public T getByProperty(String propertyName, Object value) {
|
|
|
|
|
String queryString = "from " + persistentClass.getSimpleName() + " as model where model." + propertyName + "= :" + propertyName;
|
|
|
|
@ -548,7 +596,8 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
|
|
|
|
|
queryString.append(hqlWhere);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entityManager.createQuery(queryString.toString(),Integer.class).getSingleResult();
|
|
|
|
|
Long count = entityManager.createQuery(queryString.toString(), Long.class).getSingleResult();
|
|
|
|
|
return count == null ? 0 : count.intValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|