gragon.xu 7 years ago
commit c20d801fa0

@ -72,26 +72,6 @@ public interface BaseRepository <T, ID extends Serializable> extends JpaReposito
public int deleteByProperties(String[] propNames, Object[] objValues);
/**
*
* @return count
*/
public int deleteWeaklyById(ID id);
public int deleteWeaklyByIds(Long[] ids) ;
public int deleteWeaklyByProperty(String propName, Object propValue);
public int deleteWeaklyByPropertyIn(String propName, Object[] propValues);
public int deleteWeaklyByProperties(String[] propNames, Object[] objValues);
/**
*
* @return count
*/
public int updateValidStatusById(ID id,int status);
public int updateValidStatusByIds(Long[] ids,int status) ;
public int updateValidStatusByProperty(String propName, Object propValue,int status);
public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status);
public int updateValidStatusByProperties(String[] propNames, Object[] objValues,int status);
/**
*
* @param conditionName
* @param conditionValue
@ -235,4 +215,26 @@ public interface BaseRepository <T, ID extends Serializable> extends JpaReposito
public List<Object[]> findBySqlObjList(String sql);
public List<Object[]> findBySqlObjListByPager(String sql,Pager pager);
/****************** 部分业务处理 *******************/
/**
*
* @return count
*/
public int deleteWeaklyById(ID id,String username);
public int deleteWeaklyByIds(Long[] ids,String username) ;
public int deleteWeaklyByProperty(String propName, Object propValue,String username);
public int deleteWeaklyByPropertyIn(String propName, Object[] propValues,String username);
public int deleteWeaklyByProperties(String[] propNames, Object[] objValues,String username);
/**
*
* @return count
*/
public int updateValidStatusById(ID id,int status,String username);
public int updateValidStatusByIds(Long[] ids,int status,String username) ;
public int updateValidStatusByProperty(String propName, Object propValue,int status,String username);
public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status,String username);
public int updateValidStatusByProperties(String[] propNames, Object[] objValues,int status,String username);
}

@ -12,6 +12,8 @@ import com.mongodb.client.model.Sorts;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mapping.context.MappingContext;
@ -20,6 +22,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import springfox.documentation.spring.web.json.Json;
import javax.persistence.Id;
import java.io.Serializable;
@ -47,6 +50,7 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
private final MongoEntityInformation<T, ID> entityInformation;
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mongoContext;
private final Class<T> entityClass;
private SnowflakeIdMaker snowflakeIdMaker;
public BaseMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
@ -54,6 +58,7 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
this.mongoOperations = mongoOperations;
this.entityClass = this.entityInformation.getJavaType();
mongoContext = mongoOperations.getConverter().getMappingContext();
snowflakeIdMaker = new SnowflakeIdMaker();
}
/**
@ -99,7 +104,7 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
Object val = idField.get(item);
if((type == long.class || type == Long.class) && (val == null || Long.parseLong(val.toString()) == 0)){
// long类型主键以snowflake为主键
idField.set(item, new SnowflakeIdMaker().nextId());
idField.set(item, snowflakeIdMaker.nextId());
} else if(type == String.class && (val==null || "".equals(val))){
// String类型主键以UUID为主键
idField.set(item, UUID.randomUUID().toString().replace("-", "").toLowerCase());
@ -222,8 +227,10 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
private List<T> packObjectListFromDocument(List<Document> dList) {
//将获取的document转为对象
List<T> resultList = new ArrayList<>();
// 设置为宽松模式
JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
for(Document d : dList){
resultList.add(JSONObject.parseObject(d.toJson(), entityClass));
resultList.add(JSONObject.parseObject(d.toJson(jsonWriterSettings), entityClass));
}
return resultList;

@ -2,7 +2,6 @@ package cn.estsh.i3plus.pojo.base.jpa.daoimpl;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository;
import cn.estsh.i3plus.pojo.base.tool.SnowflakeIdMaker;
import org.slf4j.Logger;
@ -14,6 +13,7 @@ import javax.persistence.Id;
import javax.persistence.Query;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@ -33,11 +33,13 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
private final EntityManager entityManager;
private Class<T> persistentClass;
private SnowflakeIdMaker snowflakeIdMaker;
public BaseRepositoryImpl(Class<T> clz, EntityManager em) {
super(clz, em);
this.entityManager = em;
this.persistentClass = clz;
this.snowflakeIdMaker = new SnowflakeIdMaker();
}
private void setParameter(Query query, String[] propName, Object[] propValue) {
@ -146,7 +148,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
return query.executeUpdate();
}else{
throw new IllegalArgumentException("Method deleteByProperties argument is illegal!propName:" + propName + ",propValue:" + propValue);
throw new IllegalArgumentException("删除错误!propName:" + propName + ",propValue:" + propValue);
}
}
@ -169,112 +171,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
}
@Override
public int deleteWeaklyById(ID id) {
return deleteWeaklyByProperty("id", id);
}
@Override
public int deleteWeaklyByIds(Long[] ids) {
return deleteWeaklyByPropertyIn("id", ids);
}
@Override
public int deleteWeaklyByProperty(String propName, Object propValue) {
return deleteWeaklyByProperties(new String[] { propName }, new Object[] { propValue });
}
/**
*
* @return
*/
@Override
public int deleteWeaklyByPropertyIn(String propName, Object[] propValues) {
if(propValues != null && propValues.length > 0){
String hql = "update " + persistentClass.getName() + " model set model.isValid = :isValid where model."+propName+" in(:"+propName+") ";
Query query = entityManager.createQuery(hql);
query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue());
query.setParameter(propName, Arrays.asList(propValues));
return query.executeUpdate();
}else{
throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValues);
}
}
/**
*
* @return
*/
@Override
public int deleteWeaklyByProperties(String[] propName, Object[] propValue) {
if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) {
StringBuffer sb = new StringBuffer("update " + persistentClass.getName() + " model set model.isValid = :isValid where 1=1 ");
appendQL(sb,propName,propValue);
Query query = entityManager.createQuery(sb.toString());
query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue());
setParameter(query,propName,propValue);
return query.executeUpdate();
}else{
throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValue);
}
}
@Override
public int updateValidStatusById(ID id,int status) {
return updateValidStatusByProperty("id", id,status);
}
@Override
public int updateValidStatusByIds(Long[] ids,int status) {
return updateValidStatusByPropertyIn("id", ids,status);
}
@Override
public int updateValidStatusByProperty(String propName, Object propValue,int status) {
return updateValidStatusByProperties(new String[] { propName }, new Object[] { propValue },status);
}
/**
*
* @return
*/
@Override
public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status) {
if(propValues != null && propValues.length > 0){
String hql = "update " + persistentClass.getName() + " model set model.isValid = :isValid where model."+propName+" in(:"+propName+") ";
Query query = entityManager.createQuery(hql);
query.setParameter("isValid",status);
query.setParameter(propName, Arrays.asList(propValues));
return query.executeUpdate();
}else{
throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValues);
}
}
/**
*
* @return
*/
@Override
public int updateValidStatusByProperties(String[] propName, Object[] propValue,int status) {
if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) {
StringBuffer sb = new StringBuffer("update " + persistentClass.getName() + " model set model.isValid = :isValid where 1=1 ");
appendQL(sb,propName,propValue);
Query query = entityManager.createQuery(sb.toString());
query.setParameter("isValid",status);
setParameter(query,propName,propValue);
return query.executeUpdate();
}else{
throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValue);
}
}
@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 });
}
@ -312,7 +208,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
setParameter(query, conditionName, conditionValue);
return query.executeUpdate();
} else {
throw new IllegalArgumentException("Method updateByProperties argument is illegal!conditionName:" + conditionName
throw new IllegalArgumentException("参数值错误!conditionName:" + conditionName
+ ",conditionValue:" + conditionValue + ",propertyName:" + propertyName + ",propertyValue:" + propertyValue);
}
}
@ -343,7 +239,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
return query.executeUpdate();
} else {
throw new IllegalArgumentException("Method updateByProperties argument is illegal! propertyName:" + propertyName + ",propertyValue:" + propertyValue);
throw new IllegalArgumentException("参数值错误! propertyName:" + propertyName + ",propertyValue:" + propertyValue);
}
}
@ -925,7 +821,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
Object val = idField.get(item);
if((type == long.class || type == Long.class) && (val == null || Long.parseLong(val.toString()) == 0)){
// long类型主键以snowflake为主键
idField.set(item, new SnowflakeIdMaker().nextId());
idField.set(item, snowflakeIdMaker.nextId());
} else if(type == String.class && (val==null || "".equals(val))){
// String类型主键以UUID为主键
idField.set(item, UUID.randomUUID().toString().replace("-", "").toLowerCase());
@ -944,4 +840,140 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
throw new RuntimeException(e);
}
}
/************** 部分业务 *************/
private String getNowTime(boolean needSecond) {
return needSecond ? getNowTime("yyyy-MM-dd HH:mm:ss") : getNowTime("yyyy-MM-dd HH:mm");
}
private String getNowTime(String format) {
String dateTime;
try {
dateTime = (new SimpleDateFormat(format)).format(new Date());
} catch (Exception e) {
dateTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
}
return dateTime;
}
@Override
public int deleteWeaklyById(ID id,String username) {
return deleteWeaklyByProperty("id", id,username);
}
@Override
public int deleteWeaklyByIds(Long[] ids,String username) {
return deleteWeaklyByPropertyIn("id", ids,username);
}
@Override
public int deleteWeaklyByProperty(String propName, Object propValue,String username) {
return deleteWeaklyByProperties(new String[] { propName }, new Object[] { propValue },username);
}
/**
*
* @return
*/
@Override
public int deleteWeaklyByPropertyIn(String propName, Object[] propValues,String username) {
if(propValues != null && propValues.length > 0){
String hql = "update " + persistentClass.getName()
+ " model set model.isValid = :isValid,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where model."
+ propName + " in(:"+propName+") ";
Query query = entityManager.createQuery(hql);
query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue());
query.setParameter("modifyUser",username);
query.setParameter("modifyDatetime",getNowTime(true));
query.setParameter(propName, Arrays.asList(propValues));
return query.executeUpdate();
}else{
throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValues);
}
}
/**
*
* @return
*/
@Override
public int deleteWeaklyByProperties(String[] propName, Object[] propValue,String username) {
if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) {
StringBuffer sb = new StringBuffer("update " + persistentClass.getName()
+ " model set model.isValid = :isValid,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where 1=1 ");
appendQL(sb,propName,propValue);
Query query = entityManager.createQuery(sb.toString());
query.setParameter("modifyUser",username);
query.setParameter("modifyDatetime",getNowTime(true));
query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue());
setParameter(query,propName,propValue);
return query.executeUpdate();
}else{
throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValue);
}
}
@Override
public int updateValidStatusById(ID id,int status,String username) {
return updateValidStatusByProperty("id", id,status,username);
}
@Override
public int updateValidStatusByIds(Long[] ids,int status,String username) {
return updateValidStatusByPropertyIn("id", ids,status,username);
}
@Override
public int updateValidStatusByProperty(String propName, Object propValue,int status,String username) {
return updateValidStatusByProperties(new String[] { propName }, new Object[] { propValue },status,username);
}
/**
*
* @return
*/
@Override
public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status,String username) {
if(propValues != null && propValues.length > 0){
String hql = "update " + persistentClass.getName()
+ " model set model.isValid = :isValid ,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where model."
+ propName + " in(:"+propName+") ";
Query query = entityManager.createQuery(hql);
query.setParameter("modifyUser",username);
query.setParameter("modifyDatetime",getNowTime(true));
query.setParameter("isValid",status);
query.setParameter(propName, Arrays.asList(propValues));
return query.executeUpdate();
}else{
throw new IllegalArgumentException("修改状态失败:"+propName+":" + propValues);
}
}
/**
*
* @return
*/
@Override
public int updateValidStatusByProperties(String[] propName, Object[] propValue,int status,String username) {
if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) {
StringBuffer sb = new StringBuffer("update " + persistentClass.getName()
+ " model set model.isValid = :isValid,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where 1=1 ");
appendQL(sb,propName,propValue);
Query query = entityManager.createQuery(sb.toString());
query.setParameter("modifyUser",username);
query.setParameter("modifyDatetime",getNowTime(true));
query.setParameter("isValid",status);
setParameter(query,propName,propValue);
return query.executeUpdate();
}else{
throw new IllegalArgumentException("修改状态失败:"+propName+":" + propValue);
}
}
}

@ -0,0 +1,45 @@
package cn.estsh.i3plus.pojo.platform.bean;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @Description :
* @Reference :
* @Author : Frin
* @Date : 2018-11-8 12:30:00
* @Modify :
**/
@Data
@Entity
@Table(name="SYS_EXCEPTION")
@DynamicInsert
@DynamicUpdate
@Api(value="系统异常表",description = "记录系统出现的异常")
public class SysException extends BaseBean {
@Column(name="EXC_MODULE_ID")
@ApiParam(value ="系统模块(枚举)", example = "1")
//CommonEnumUtil.SOFT_TYPE
private Integer excModuleId;
@Column(name="EXC_CLASS_NAME")
@ApiParam(value ="异常类名")
private String excClassName;
@Column(name="EXC_MESSAGE")
@ApiParam(value ="异常信息")
private String excMessage;
@Column(name="EXC_STACK")
@ApiParam(value ="堆栈信息")
private String excStack;
}

@ -0,0 +1,14 @@
package cn.estsh.i3plus.pojo.platform.repositorymongo;
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseMongoRepository;
import cn.estsh.i3plus.pojo.platform.bean.SysException;
/**
* @Description : (使Mongodb)
* @Reference :
* @Author : frin
* @Date : 2018-11-8 12:03:00
* @Modify :
**/
public interface SysExceptionRepository extends BaseMongoRepository<SysException, Long> {
}
Loading…
Cancel
Save