|
@ -11,12 +11,9 @@ import com.yihu.jw.util.common.NumberUtil;
|
|
|
import com.yihu.jw.util.common.StringUtil;
|
|
|
import com.yihu.jw.util.sql.SqlCreator;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.hibernate.Criteria;
|
|
|
import org.hibernate.Query;
|
|
|
import org.hibernate.SQLQuery;
|
|
|
import org.hibernate.query.NativeQuery;
|
|
|
import org.hibernate.query.Query;
|
|
|
import org.hibernate.Session;
|
|
|
import org.hibernate.criterion.Order;
|
|
|
import org.hibernate.criterion.Restrictions;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -29,6 +26,11 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import javax.persistence.EntityManager;
|
|
|
import javax.persistence.PersistenceContext;
|
|
|
import javax.persistence.Table;
|
|
|
import javax.persistence.TypedQuery;
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.persistence.criteria.Root;
|
|
|
import java.io.IOException;
|
|
|
import java.io.Serializable;
|
|
|
import java.math.BigInteger;
|
|
@ -207,9 +209,10 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
}
|
|
|
|
|
|
public <T> List getEntityList(Class<T> cls, String condition, String order, Integer limit, Integer offset) throws IOException {
|
|
|
Session session = this.getCurrentSession();
|
|
|
Criteria criteria = session.createCriteria(cls);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
CriteriaBuilder criteriaBuilder = getCurrentSession().getCriteriaBuilder();
|
|
|
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(cls);
|
|
|
Root<T> root = criteriaQuery.from(cls);
|
|
|
List<Predicate> predicateList = new ArrayList<>();
|
|
|
JsonNode jsonNode;
|
|
|
Iterator fieldNames;
|
|
|
String fieldName;
|
|
@ -220,9 +223,11 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
while (fieldNames.hasNext()) {
|
|
|
fieldName = (String) fieldNames.next();
|
|
|
if (jsonNode.get(fieldName).isInt()) {
|
|
|
criteria.add(Restrictions.eq(fieldName, Integer.valueOf(jsonNode.get(fieldName).asInt())));
|
|
|
Predicate predicate1 = criteriaBuilder.equal(root.get(fieldName), jsonNode.get(fieldName).asInt());
|
|
|
predicateList.add(predicate1);
|
|
|
} else {
|
|
|
criteria.add(Restrictions.eq(fieldName, jsonNode.get(fieldName).asText()));
|
|
|
Predicate predicate1 = criteriaBuilder.equal(root.get(fieldName), jsonNode.get(fieldName).asText());
|
|
|
predicateList.add(predicate1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -231,31 +236,45 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
jsonNode = objectMapper.readTree(order);
|
|
|
fieldNames = jsonNode.fieldNames();
|
|
|
|
|
|
List<javax.persistence.criteria.Order> orderList = new ArrayList<>();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
fieldName = (String) fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText().toUpperCase();
|
|
|
if (value.equals("ASC")) {
|
|
|
criteria.addOrder(Order.asc(fieldName));
|
|
|
orderList.add(criteriaBuilder.asc(root.get(fieldName)));
|
|
|
} else if (value.equals("DESC")) {
|
|
|
criteria.addOrder(Order.desc(fieldName));
|
|
|
orderList.add(criteriaBuilder.desc(root.get(fieldName)));
|
|
|
}
|
|
|
}
|
|
|
criteriaQuery.orderBy(orderList);
|
|
|
}
|
|
|
|
|
|
if(predicateList.size()>0){
|
|
|
Predicate[] predicates = new Predicate[predicateList.size()];
|
|
|
for (int i = 0; i < predicates.length; i++) {
|
|
|
predicates[i] = predicateList.get(i);
|
|
|
}
|
|
|
criteriaQuery.where(predicates);
|
|
|
}
|
|
|
TypedQuery<T> typedQuery = getCurrentSession().createQuery(criteriaQuery);
|
|
|
if (limit != null) {
|
|
|
criteria.setMaxResults(limit.intValue());
|
|
|
typedQuery.setMaxResults(limit);
|
|
|
if (offset != null &&offset>0) {
|
|
|
criteria.setFirstResult((offset.intValue() - 1) * limit.intValue());
|
|
|
typedQuery.setFirstResult((offset - 1) * limit);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return criteria.list();
|
|
|
return typedQuery.getResultList();
|
|
|
}
|
|
|
|
|
|
public <T> List<T> getEntityListByParentId(Class<T> cls, String parentFiledName, Long patentId, String condition, String order, Integer limit, Integer offset) throws IOException {
|
|
|
Session session = this.getCurrentSession();
|
|
|
Criteria criteria = session.createCriteria(cls);
|
|
|
criteria.add(Restrictions.eq(parentFiledName, patentId));
|
|
|
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
|
|
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(cls);
|
|
|
Root<T> root = criteriaQuery.from(cls);
|
|
|
List<Predicate> predicateList = new ArrayList<>();
|
|
|
Predicate predicate = criteriaBuilder.equal(root.get(parentFiledName), patentId);
|
|
|
predicateList.add(predicate);
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
JsonNode jsonNode;
|
|
|
Iterator fieldNames;
|
|
@ -267,9 +286,11 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
while (fieldNames.hasNext()) {
|
|
|
fieldName = (String) fieldNames.next();
|
|
|
if (jsonNode.get(fieldName).isInt()) {
|
|
|
criteria.add(Restrictions.eq(fieldName, Integer.valueOf(jsonNode.get(fieldName).asInt())));
|
|
|
Predicate predicate1 = criteriaBuilder.equal(root.get(fieldName), jsonNode.get(fieldName).asInt());
|
|
|
predicateList.add(predicate1);
|
|
|
} else {
|
|
|
criteria.add(Restrictions.eq(fieldName, jsonNode.get(fieldName).asText()));
|
|
|
Predicate predicate1 = criteriaBuilder.equal(root.get(fieldName), jsonNode.get(fieldName).asText());
|
|
|
predicateList.add(predicate1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -277,26 +298,34 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
if (!StringUtil.isEmpty(order)) {
|
|
|
jsonNode = objectMapper.readTree(order);
|
|
|
fieldNames = jsonNode.fieldNames();
|
|
|
|
|
|
List<javax.persistence.criteria.Order> orderList = new ArrayList<>();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
fieldName = (String) fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText().toUpperCase();
|
|
|
if (value.equals("ASC")) {
|
|
|
criteria.addOrder(Order.asc(fieldName));
|
|
|
orderList.add(criteriaBuilder.asc(root.get(fieldName)));
|
|
|
} else if (value.equals("DESC")) {
|
|
|
criteria.addOrder(Order.desc(fieldName));
|
|
|
orderList.add(criteriaBuilder.desc(root.get(fieldName)));
|
|
|
}
|
|
|
}
|
|
|
criteriaQuery.orderBy(orderList);
|
|
|
}
|
|
|
|
|
|
Predicate[] predicates = new Predicate[predicateList.size()];
|
|
|
for (int i = 0; i < predicates.length; i++) {
|
|
|
predicates[i] = predicateList.get(i);
|
|
|
}
|
|
|
criteriaQuery.where(predicates);
|
|
|
|
|
|
TypedQuery<T> typedQuery = session.createQuery(criteriaQuery);
|
|
|
|
|
|
if (limit != null) {
|
|
|
criteria.setMaxResults(limit.intValue());
|
|
|
typedQuery.setMaxResults(limit);
|
|
|
if (offset != null &&offset>0) {
|
|
|
criteria.setFirstResult((offset.intValue() - 1) * limit.intValue());
|
|
|
typedQuery.setFirstResult((offset - 1) * limit);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return criteria.list();
|
|
|
return typedQuery.getResultList();
|
|
|
}
|
|
|
|
|
|
public List getList(Class tClass, String tableName, String condition, String order, Integer limit, Integer offset) {
|
|
@ -316,7 +345,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
fieldName = (String) query.next();
|
|
|
value = sql.get(fieldName).asText();
|
|
|
if (sql.get(fieldName).getNodeType().equals(JsonNodeType.NUMBER)) {
|
|
|
sqlCreator.equalCondition(fieldName, Integer.valueOf(Integer.parseInt(value)));
|
|
|
sqlCreator.equalCondition(fieldName, Integer.parseInt(value));
|
|
|
} else {
|
|
|
sqlCreator.equalCondition(fieldName, value);
|
|
|
}
|
|
@ -395,8 +424,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
sqlCreator.equalCondition("id", id);
|
|
|
String sql = sqlCreator.selectData(tableName);
|
|
|
Query query = this.getQuery(sqlCreator, sql);
|
|
|
return query.uniqueResult();
|
|
|
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(tClass)).get(0);
|
|
|
}
|
|
|
|
|
|
public Object get(Class tClass, String stdVersion, String dataType, String id) {
|
|
@ -407,8 +435,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
sqlCreator.equalCondition("id", id);
|
|
|
String sql = sqlCreator.selectData(tableName);
|
|
|
Query query = getQuery(sqlCreator, sql);
|
|
|
return query.uniqueResult();
|
|
|
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(tClass)).get(0);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@ -416,34 +443,10 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
return Envelop.getError("获取标准数据元失败");
|
|
|
}
|
|
|
|
|
|
private Query getQuery(SqlCreator sqlCreator, String sql) {
|
|
|
SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
|
|
|
Iterator var4 = sqlCreator.getKeyValueMap().keySet().iterator();
|
|
|
|
|
|
while (var4.hasNext()) {
|
|
|
String key = (String) var4.next();
|
|
|
Object value = sqlCreator.getKeyValueMap().get(key);
|
|
|
if (value instanceof Collection) {
|
|
|
query.setParameterList(key, (Collection) value);
|
|
|
} else {
|
|
|
query.setParameter(key, value);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
query.setResultTransformer(sqlCreator.getTransformer());
|
|
|
return query;
|
|
|
}
|
|
|
|
|
|
public void insertBatch(final List<String> insertSqlList) {
|
|
|
// Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
|
|
|
String[] sqlStr = new String[insertSqlList.size()];
|
|
|
String[] sqlArr = insertSqlList.toArray(sqlStr);
|
|
|
this.jdbcTemplate.batchUpdate(sqlArr);
|
|
|
|
|
|
}
|
|
|
|
|
|
public Session openSession() {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public Session getCurrentSession() {
|
|
@ -452,14 +455,14 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
|
|
|
public Long getMaxId(String tableName) {
|
|
|
String sql = "select max(id) from " + tableName;
|
|
|
SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
|
|
|
NativeQuery query = this.getCurrentSession().createSQLQuery(sql);
|
|
|
Object object = query.uniqueResult();
|
|
|
Long maxId = object == null ? 1L : (Long.parseLong(object.toString()) + 1);
|
|
|
return maxId;
|
|
|
}
|
|
|
|
|
|
public Query getExeuteQuery(SqlCreator sqlCreator, String sql) {
|
|
|
SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
|
|
|
public NativeQuery getExeuteQuery(SqlCreator sqlCreator, String sql) {
|
|
|
NativeQuery query = this.getCurrentSession().createSQLQuery(sql);
|
|
|
Iterator var4 = sqlCreator.getKeyValueMap().keySet().iterator();
|
|
|
|
|
|
while (var4.hasNext()) {
|