添加批量删除

添加批量修改
yun-zuoyi
wei.peng 7 years ago
parent 68b4778fd3
commit 44ea78b93a

@ -23,6 +23,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

@ -388,7 +388,6 @@ public class CommonEnumUtil {
}
}
/**
*
*/
@ -422,7 +421,6 @@ public class CommonEnumUtil {
}
}
/**
*
*/
@ -455,4 +453,39 @@ public class CommonEnumUtil {
return tmp;
}
}
/**
*
*/
public enum PARENT{
DEFAULT(-1L,"根节点");
private Long value = -1L;
private String description = null;
public Long getValue() {
return value;
}
public String getDescription() {
return description;
}
private PARENT(Long value, String description) {
this.value = value;
this.description = description;
}
public static String valueOf(int val) {
String tmp = null;
for (int i = 0; i < values().length; i++) {
if (values()[i].value == val) {
tmp = values()[i].description;
}
}
return tmp;
}
}
}

@ -77,6 +77,12 @@ public interface BaseRepository <T, ID extends Serializable> extends JpaReposito
public void deleteByProperties(String[] propName, Object[] propValue);
/**
* in
* @param ids
*/
public void deleteByIdIn(Long[] ids);
/**
*
* @param conditionName
* @param conditionValue
@ -112,6 +118,24 @@ public interface BaseRepository <T, ID extends Serializable> extends JpaReposito
*/
public int updateByProperties(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue);
/**
* hqlWhere
* @param hqlWhere
* @param propertyName
* @param propertyValue
* @return
*/
public int updateByHqlWhere(String hqlWhere,String propertyName, Object propertyValue);
/**
*
* @param hqlWhere HQL where
* @param propertyName
* @param propertyValue
* @return
*/
public int updateByHqlWhere(String hqlWhere,String[] propertyName, Object[] propertyValue);
public T getById(long id);
public List<T> list();

@ -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

@ -1,7 +1,5 @@
package cn.estsh.i3plus.pojo.base.tool;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import java.text.SimpleDateFormat;
import java.util.Date;

Loading…
Cancel
Save