|
@ -38,19 +38,20 @@ import java.util.*;
|
|
*/
|
|
*/
|
|
@Transactional(propagation = Propagation.SUPPORTS)
|
|
@Transactional(propagation = Propagation.SUPPORTS)
|
|
public class BaseJpaService<T, R> {
|
|
public class BaseJpaService<T, R> {
|
|
|
|
Class<R> repoClass;
|
|
|
|
|
|
@PersistenceContext
|
|
@PersistenceContext
|
|
protected EntityManager entityManager;
|
|
protected EntityManager entityManager;
|
|
@Autowired
|
|
@Autowired
|
|
protected JdbcTemplate jdbcTemplate;
|
|
protected JdbcTemplate jdbcTemplate;
|
|
@Autowired
|
|
@Autowired
|
|
protected ObjectMapper objectMapper;
|
|
protected ObjectMapper objectMapper;
|
|
Class<R> repoClass;
|
|
|
|
|
|
|
|
public BaseJpaService() {
|
|
|
|
|
|
public BaseJpaService(){
|
|
Type genType = getClass().getGenericSuperclass();
|
|
Type genType = getClass().getGenericSuperclass();
|
|
if ((genType instanceof ParameterizedType)) {
|
|
if ((genType instanceof ParameterizedType)) {
|
|
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
|
|
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
|
|
if (params.length == 2) {
|
|
|
|
|
|
if (params.length==2) {
|
|
repoClass = (Class) params[1];
|
|
repoClass = (Class) params[1];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@ -83,44 +84,36 @@ public class BaseJpaService<T, R> {
|
|
return (Class) parameters[0];
|
|
return (Class) parameters[0];
|
|
}
|
|
}
|
|
|
|
|
|
public List<T> search(String fields, String filters, String sorts, Integer page, Integer size) throws ParseException {
|
|
|
|
|
|
public List search(String fields, String filters, String sorts, Integer page, Integer size) throws ParseException {
|
|
URLQueryParser queryParser = createQueryParser(fields, filters, sorts);
|
|
URLQueryParser queryParser = createQueryParser(fields, filters, sorts);
|
|
CriteriaQuery query = queryParser.makeCriteriaQuery();
|
|
CriteriaQuery query = queryParser.makeCriteriaQuery();
|
|
|
|
|
|
if (page == null || page <= 0) {
|
|
|
|
page = PageArg.DefaultPage;
|
|
|
|
}
|
|
|
|
if (size == null || size <= 0 || size > 10000) {
|
|
|
|
size = PageArg.DefaultSize;
|
|
|
|
}
|
|
|
|
|
|
if (page == null || page <= 0) page = PageArg.DefaultPage;
|
|
|
|
if (size == null || size <= 0 || size > 10000) size = PageArg.DefaultSize;
|
|
|
|
|
|
List resultList = entityManager
|
|
|
|
|
|
return entityManager
|
|
.createQuery(query)
|
|
.createQuery(query)
|
|
.setFirstResult((page - 1) * size)
|
|
.setFirstResult((page - 1) * size)
|
|
.setMaxResults(size)
|
|
.setMaxResults(size)
|
|
.getResultList();
|
|
.getResultList();
|
|
|
|
|
|
return (List<T>) resultList;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public List<T> search(String filters) throws ParseException {
|
|
|
|
|
|
public List search(String filters) throws ParseException {
|
|
URLQueryParser queryParser = createQueryParser("", filters, "");
|
|
URLQueryParser queryParser = createQueryParser("", filters, "");
|
|
CriteriaQuery query = queryParser.makeCriteriaQuery();
|
|
CriteriaQuery query = queryParser.makeCriteriaQuery();
|
|
|
|
|
|
List resultList = entityManager
|
|
|
|
|
|
return entityManager
|
|
.createQuery(query)
|
|
.createQuery(query)
|
|
.getResultList();
|
|
.getResultList();
|
|
return (List<T>) resultList;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public List<T> search(String filters, String sorts) throws ParseException {
|
|
|
|
|
|
public List search(String filters,String sorts) throws ParseException {
|
|
URLQueryParser queryParser = createQueryParser("", filters, sorts);
|
|
URLQueryParser queryParser = createQueryParser("", filters, sorts);
|
|
CriteriaQuery query = queryParser.makeCriteriaQuery();
|
|
CriteriaQuery query = queryParser.makeCriteriaQuery();
|
|
|
|
|
|
List resultList = entityManager
|
|
|
|
|
|
return entityManager
|
|
.createQuery(query)
|
|
.createQuery(query)
|
|
.getResultList();
|
|
.getResultList();
|
|
return (List<T>) resultList;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public long getCount(String filters) throws ParseException {
|
|
public long getCount(String filters) throws ParseException {
|
|
@ -130,15 +123,51 @@ public class BaseJpaService<T, R> {
|
|
return (long) entityManager.createQuery(query).getSingleResult();
|
|
return (long) entityManager.createQuery(query).getSingleResult();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
protected <T> URLQueryParser createQueryParser(String fields, String filters, String orders) {
|
|
|
|
URLQueryParser queryParser = new URLQueryParser<T>(fields, filters, orders)
|
|
|
|
.setEntityManager(entityManager)
|
|
|
|
.setEntityClass(getEntityClass());
|
|
|
|
|
|
|
|
return queryParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <T> URLQueryParser createQueryParser(String filters) {
|
|
|
|
URLQueryParser queryParser = new URLQueryParser<T>(filters)
|
|
|
|
.setEntityManager(entityManager)
|
|
|
|
.setEntityClass(getEntityClass());
|
|
|
|
|
|
|
|
return queryParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Sort parseSorts(String sorter){
|
|
|
|
if (StringUtils.isNotEmpty(sorter)) {
|
|
|
|
String[] orderArray = sorter.split(",");
|
|
|
|
|
|
|
|
List<Sort.Order> orderList = new ArrayList<>(orderArray.length);
|
|
|
|
Arrays.stream(orderArray).forEach(
|
|
|
|
elem -> orderList.add(
|
|
|
|
elem.startsWith("+") ? new Sort.Order(Sort.Direction.ASC, elem.substring(1)):
|
|
|
|
new Sort.Order(Sort.Direction.DESC, elem.substring(1))));
|
|
|
|
|
|
|
|
return new Sort(orderList);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Session currentSession() {
|
|
|
|
return entityManager.unwrap(Session.class);
|
|
|
|
}
|
|
|
|
|
|
public PagingAndSortingRepository getRepository() {
|
|
public PagingAndSortingRepository getRepository() {
|
|
return (PagingAndSortingRepository) SpringContext.getService(repoClass);
|
|
return (PagingAndSortingRepository) SpringContext.getService(repoClass);
|
|
}
|
|
}
|
|
|
|
|
|
public JpaRepository getJpaRepository() {
|
|
|
|
|
|
public JpaRepository getJpaRepository(){
|
|
return (JpaRepository) SpringContext.getService(repoClass);
|
|
return (JpaRepository) SpringContext.getService(repoClass);
|
|
}
|
|
}
|
|
|
|
|
|
public List<T> findByField(String field, Object value) {
|
|
|
|
|
|
public List<T> findByField(String field, Object value){
|
|
|
|
|
|
return findByFields(
|
|
return findByFields(
|
|
new String[]{field},
|
|
new String[]{field},
|
|
@ -146,38 +175,37 @@ public class BaseJpaService<T, R> {
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
public List<T> findByFields(String[] fields, Object[] values) {
|
|
|
|
|
|
public List<T> findByFields(String[] fields, Object[] values){
|
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
|
CriteriaQuery query = criteriaBuilder.createQuery(getEntityClass());
|
|
CriteriaQuery query = criteriaBuilder.createQuery(getEntityClass());
|
|
Root<T> root = query.from(getEntityClass());
|
|
Root<T> root = query.from(getEntityClass());
|
|
List<Predicate> ls = new ArrayList<>();
|
|
List<Predicate> ls = new ArrayList<>();
|
|
for (int i = 0; i < fields.length; i++) {
|
|
|
|
if (values[i].getClass().isArray()) {
|
|
|
|
ls.add(criteriaBuilder.in(root.get(fields[i]).in((Object[]) values[i])));
|
|
|
|
} else {
|
|
|
|
|
|
for(int i=0; i< fields.length; i++){
|
|
|
|
if(values[i].getClass().isArray())
|
|
|
|
ls.add(criteriaBuilder.in(root.get(fields[i]).in((Object[])values[i])));
|
|
|
|
else
|
|
ls.add(criteriaBuilder.equal(root.get(fields[i]), values[i]));
|
|
ls.add(criteriaBuilder.equal(root.get(fields[i]), values[i]));
|
|
}
|
|
|
|
}
|
|
}
|
|
query.where(ls.toArray(new Predicate[ls.size()]));
|
|
query.where(ls.toArray(new Predicate[ls.size()]));
|
|
List resultList = entityManager
|
|
|
|
|
|
return entityManager
|
|
.createQuery(query)
|
|
.createQuery(query)
|
|
.getResultList();
|
|
|
|
return (List<T>) resultList;
|
|
|
|
|
|
.getResultList() ;
|
|
}
|
|
}
|
|
|
|
|
|
public String getClzName() {
|
|
|
|
|
|
public String getClzName(){
|
|
return getEntityClass().getName();
|
|
return getEntityClass().getName();
|
|
}
|
|
}
|
|
|
|
|
|
public String getEntityIdFiled() {
|
|
|
|
|
|
public String getEntityIdFiled(){
|
|
EntityType entityType = entityManager.getMetamodel().entity(getEntityClass());
|
|
EntityType entityType = entityManager.getMetamodel().entity(getEntityClass());
|
|
javax.persistence.metamodel.Type type = entityType.getIdType();
|
|
javax.persistence.metamodel.Type type = entityType.getIdType();
|
|
String s = entityType.getId(type.getJavaType()).getName();
|
|
String s = entityType.getId(type.getJavaType()).getName();
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
|
|
|
|
public int delete(Object[] ids) {
|
|
|
|
String hql = " DELETE FROM " + getEntityClass().getName() + " WHERE " + getEntityIdFiled() + " in(:ids)";
|
|
|
|
|
|
|
|
|
|
public int delete(Object[] ids){
|
|
|
|
String hql = " DELETE FROM "+getEntityClass().getName()+" WHERE "+getEntityIdFiled()+" in(:ids)";
|
|
Query query = currentSession().createQuery(hql);
|
|
Query query = currentSession().createQuery(hql);
|
|
query.setParameterList("ids", ids);
|
|
query.setParameterList("ids", ids);
|
|
return query.executeUpdate();
|
|
return query.executeUpdate();
|
|
@ -197,38 +225,6 @@ public class BaseJpaService<T, R> {
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
}
|
|
}
|
|
|
|
|
|
protected <t> URLQueryParser createQueryParser(String fields, String filters, String orders) {
|
|
|
|
return new URLQueryParser<t>(fields, filters, orders)
|
|
|
|
.setEntityManager(entityManager)
|
|
|
|
.setEntityClass(getEntityClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <t> URLQueryParser createQueryParser(String filters) {
|
|
|
|
return new URLQueryParser<t>(filters)
|
|
|
|
.setEntityManager(entityManager)
|
|
|
|
.setEntityClass(getEntityClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Sort parseSorts(String sorter) {
|
|
|
|
if (StringUtils.isNotEmpty(sorter)) {
|
|
|
|
String[] orderArray = sorter.split(",");
|
|
|
|
|
|
|
|
List<Sort.Order> orderList = new ArrayList<>(orderArray.length);
|
|
|
|
Arrays.stream(orderArray).forEach(
|
|
|
|
elem -> orderList.add(
|
|
|
|
elem.startsWith("+") ? new Sort.Order(Sort.Direction.ASC, elem.substring(1)) :
|
|
|
|
new Sort.Order(Sort.Direction.DESC, elem.substring(1))));
|
|
|
|
|
|
|
|
return new Sort(orderList);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Session currentSession() {
|
|
|
|
return entityManager.unwrap(Session.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取指定长度的随机字符串
|
|
* 获取指定长度的随机字符串
|
|
* @param length
|
|
* @param length
|
|
@ -244,4 +240,5 @@ public class BaseJpaService<T, R> {
|
|
}
|
|
}
|
|
return buffer.toString();
|
|
return buffer.toString();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|