BaseJpaService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package com.yihu.mysql.query;
  2. import com.yihu.utils.context.SpringContextUtils;
  3. import org.hibernate.Query;
  4. import org.hibernate.Session;
  5. import org.springframework.beans.BeanUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.domain.PageRequest;
  8. import org.springframework.data.domain.Pageable;
  9. import org.springframework.data.domain.Sort;
  10. import org.springframework.data.jpa.repository.JpaRepository;
  11. import org.springframework.data.repository.CrudRepository;
  12. import org.springframework.jdbc.core.JdbcTemplate;
  13. import org.springframework.transaction.annotation.Propagation;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import org.springframework.util.StringUtils;
  16. import javax.persistence.EntityManager;
  17. import javax.persistence.PersistenceContext;
  18. import javax.persistence.criteria.CriteriaBuilder;
  19. import javax.persistence.criteria.CriteriaQuery;
  20. import javax.persistence.criteria.Predicate;
  21. import javax.persistence.criteria.Root;
  22. import javax.persistence.metamodel.EntityType;
  23. import java.io.Serializable;
  24. import java.lang.reflect.ParameterizedType;
  25. import java.lang.reflect.Type;
  26. import java.text.ParseException;
  27. import java.util.*;
  28. /**
  29. * Service基础类。此类基于Spring Data JPA进行封装(Spring Data JPA又是基于JPA封装,EHR平台使用Hibernate作为JPA实现者)。
  30. * 需要注意的是,部分功能会跳过JPA接口而直接使用Hibernate接口,比如访问Hibernate的Session接口,因为它把JPA的EntityManager功能强大。
  31. *
  32. * @author lincl
  33. * @author Sand
  34. * @version 1.0
  35. * @created 2016.2.3。
  36. */
  37. @Transactional(propagation = Propagation.SUPPORTS)
  38. public abstract class BaseJpaService<T, R extends CrudRepository> {
  39. protected final int defaultPage = 1;
  40. protected final int defaultSize = 15;
  41. protected Class<T> tClass;
  42. protected Class<R> repoClass;
  43. @PersistenceContext
  44. protected EntityManager entityManager;
  45. @Autowired
  46. protected JdbcTemplate jdbcTemplate;
  47. public BaseJpaService() {
  48. Type genType = getClass().getGenericSuperclass();
  49. if (genType instanceof ParameterizedType) {
  50. Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
  51. if (params.length == 2) {
  52. tClass = (Class<T>) params[0];
  53. repoClass = (Class<R>) params[1];
  54. }
  55. }
  56. }
  57. public T save (T entity) {
  58. return (T) getRepository().save(entity);
  59. }
  60. public void batchInsert (List list) {
  61. for (int i = 0; i < list.size(); i++) {
  62. entityManager.persist(list.get(i));
  63. if (i % 30 == 0) {
  64. entityManager.flush();
  65. entityManager.clear();
  66. }
  67. }
  68. }
  69. public void delete(Serializable id) {
  70. getRepository().delete(id);
  71. }
  72. public void delete(T entity) {
  73. getRepository().delete(entity);
  74. }
  75. public void delete(Iterable ids) {
  76. getRepository().delete(ids);
  77. }
  78. public int delete(Object[] ids){
  79. if (Integer.class.getName().equals(getEntityIdType())) {
  80. Integer [] _ids = new Integer[ids.length];
  81. for (int i = 0; i < ids.length; i ++) {
  82. _ids[i] = new Integer(ids[i].toString());
  83. }
  84. ids = _ids;
  85. }
  86. String hql = "DELETE FROM " + getEntityClass().getName() + " WHERE " + getEntityIdFiled() + " in(:ids)";
  87. Query query = currentSession().createQuery(hql);
  88. query.setParameterList("ids", ids);
  89. return query.executeUpdate();
  90. }
  91. public T retrieve(Serializable id) {
  92. return (T) getRepository().findOne(id);
  93. }
  94. public List<T> search(String filters) throws ParseException {
  95. URLQueryParser queryParser = createQueryParser("", filters, "");
  96. CriteriaQuery query = queryParser.makeCriteriaQuery();
  97. return entityManager
  98. .createQuery(query)
  99. .getResultList();
  100. }
  101. public List<T> search(String filters, String sorts) throws ParseException {
  102. URLQueryParser queryParser = createQueryParser("", filters, sorts);
  103. CriteriaQuery query = queryParser.makeCriteriaQuery();
  104. return entityManager
  105. .createQuery(query)
  106. .getResultList();
  107. }
  108. public List<T> search(String fields, String filters, String sorts) throws ParseException {
  109. URLQueryParser queryParser = createQueryParser(fields, filters, sorts);
  110. CriteriaQuery query = queryParser.makeCriteriaQuery();
  111. return entityManager
  112. .createQuery(query)
  113. .getResultList();
  114. }
  115. public List<T> search(String fields, String filters, String sorts, Integer page, Integer size) throws ParseException {
  116. URLQueryParser queryParser = createQueryParser(fields, filters, sorts);
  117. CriteriaQuery query = queryParser.makeCriteriaQuery();
  118. if (page == null || page <= 0) {
  119. page = defaultPage;
  120. }
  121. if (size == null || size <= 0 || size > 10000) {
  122. size = defaultSize;
  123. }
  124. return entityManager
  125. .createQuery(query)
  126. .setFirstResult((page - 1) * size)
  127. .setMaxResults(size)
  128. .getResultList();
  129. }
  130. public List<T> findByField(String field, Object value){
  131. return findByFields(
  132. new String[]{field},
  133. new Object[]{value}
  134. );
  135. }
  136. public List<T> findByFields(String[] fields, Object[] values){
  137. CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
  138. CriteriaQuery query = criteriaBuilder.createQuery(getEntityClass());
  139. Root<T> root = query.from(getEntityClass());
  140. List<Predicate> ls = new ArrayList<>();
  141. for(int i=0; i< fields.length; i++){
  142. if (values[i].getClass().isArray()) {
  143. ls.add(criteriaBuilder.in(root.get(fields[i]).in((Object[]) values[i])));
  144. } else {
  145. ls.add(criteriaBuilder.equal(root.get(fields[i]), values[i]));
  146. }
  147. }
  148. query.where(ls.toArray(new Predicate[ls.size()]));
  149. return entityManager
  150. .createQuery(query)
  151. .getResultList() ;
  152. }
  153. public long getCount(String filters) throws ParseException {
  154. URLQueryParser queryParser = createQueryParser(filters);
  155. CriteriaQuery query = queryParser.makeCriteriaCountQuery();
  156. return (long) entityManager.createQuery(query).getSingleResult();
  157. }
  158. public Class<T> getEntityClass() {
  159. return tClass;
  160. }
  161. public <T> URLQueryParser createQueryParser(String fields, String filters, String orders) {
  162. URLQueryParser queryParser = new URLQueryParser<T>(fields, filters, orders)
  163. .setEntityManager(entityManager)
  164. .setEntityClass(getEntityClass());
  165. return queryParser;
  166. }
  167. public <T> URLQueryParser createQueryParser(String filters) {
  168. URLQueryParser queryParser = new URLQueryParser<T>(filters)
  169. .setEntityManager(entityManager)
  170. .setEntityClass(getEntityClass());
  171. return queryParser;
  172. }
  173. public Sort parseSorts(String sorter){
  174. if (!StringUtils.isEmpty(sorter)) {
  175. String[] orderArray = sorter.split(",");
  176. List<Sort.Order> orderList = new ArrayList<>(orderArray.length);
  177. Arrays.stream(orderArray).forEach(
  178. elem -> orderList.add(
  179. elem.startsWith("+") ? new Sort.Order(Sort.Direction.ASC, elem.substring(1)):
  180. new Sort.Order(Sort.Direction.DESC, elem.substring(1))));
  181. return new Sort(orderList);
  182. }
  183. return null;
  184. }
  185. public JpaRepository getJpaRepository(){
  186. return (JpaRepository) SpringContextUtils.getService(repoClass);
  187. }
  188. public R getRepository() {
  189. return SpringContextUtils.getService(repoClass);
  190. }
  191. public Session currentSession() {
  192. return entityManager.unwrap(Session.class);
  193. }
  194. public String getEntityName(){
  195. return getEntityClass().getName();
  196. }
  197. public String getEntityIdFiled(){
  198. EntityType entityType = entityManager.getMetamodel().entity(getEntityClass());
  199. javax.persistence.metamodel.Type type = entityType.getIdType();
  200. return entityType.getId(type.getJavaType()).getName();
  201. }
  202. public String getEntityIdType(){
  203. EntityType entityType = entityManager.getMetamodel().entity(getEntityClass());
  204. javax.persistence.metamodel.Type type = entityType.getIdType();
  205. return type.getJavaType().getName();
  206. }
  207. /**
  208. * 将实体转换为模型。
  209. *
  210. * @param source
  211. * @param target
  212. * @param <T>
  213. * @return
  214. */
  215. public <T> T convertToModel(Object source, Class<T> target) {
  216. if (source == null) {
  217. return null;
  218. }
  219. T _target = BeanUtils.instantiate(target);
  220. BeanUtils.copyProperties(source, _target);
  221. return _target;
  222. }
  223. /**
  224. * 将实体集合转换为模型集合。
  225. * @param sources
  226. * @param targets
  227. * @param target
  228. * @param <T>
  229. * @return
  230. */
  231. public <T> List<T> convertToModels(Collection sources, List<T> targets, Class<T> target){
  232. if (null == sources) {
  233. return null;
  234. }
  235. sources.forEach(item -> {
  236. T _target = BeanUtils.instantiate(target);
  237. BeanUtils.copyProperties(item, _target);
  238. targets.add(_target);
  239. });
  240. return targets;
  241. }
  242. public String getCode() {
  243. return UUID.randomUUID().toString().replaceAll("-", "");
  244. }
  245. /**
  246. * 获取指定长度的随机数字字符串
  247. * @param length
  248. * @return
  249. */
  250. public String randomInt(int length) {
  251. String str = "0123456789";
  252. StringBuffer buffer = new StringBuffer();
  253. Random random = new Random();
  254. for(int i = 0; i < length; ++i) {
  255. int pos = random.nextInt(str.length());
  256. buffer.append(str.charAt(pos));
  257. }
  258. return buffer.toString();
  259. }
  260. /**
  261. * 获取指定长度的随机字符串
  262. * @param length
  263. * @return
  264. */
  265. public String randomString(int length) {
  266. String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  267. StringBuffer buffer = new StringBuffer();
  268. Random random = new Random();
  269. for (int i = 0; i < length; i++) {
  270. int pos = random.nextInt(str.length());
  271. buffer.append(str.charAt(pos));
  272. }
  273. return buffer.toString();
  274. }
  275. /**
  276. * 排序条件 第一个字符为 + 表示升序,第一个字符为 - 表示减序,不包含 + 或者 - 默认升序
  277. * @param sorts
  278. * @return
  279. */
  280. public Sort createSort(String sorts){
  281. Sort sort = null;
  282. if (!org.springframework.util.StringUtils.isEmpty(sorts)) {
  283. // 默认升序
  284. if(sorts.contains("-")){
  285. sort = new Sort(Sort.Direction.DESC,sorts.substring(1,sorts.length()).split(","));
  286. }else if(sorts.contains("+")){
  287. sort = new Sort(Sort.Direction.ASC,sorts.substring(1,sorts.length()).split(","));
  288. }else{
  289. sort = new Sort(Sort.Direction.ASC,sorts.split(","));
  290. }
  291. }
  292. return sort;
  293. }
  294. /**
  295. * 创建分页对象
  296. * @param page
  297. * @param size
  298. * @param sorts
  299. * @return
  300. */
  301. public Pageable createPage(Integer page, Integer size, String sorts){
  302. PageRequest pageRequest = null;
  303. if(null == page || page <= 0){
  304. page = defaultPage;
  305. }
  306. if(null == size || size <= 0){
  307. size = defaultSize;
  308. }
  309. if(!org.apache.commons.lang3.StringUtils.isEmpty(sorts)){
  310. pageRequest = new PageRequest(page,size,createSort(sorts));
  311. }else{
  312. pageRequest = new PageRequest(page,size);
  313. }
  314. return pageRequest;
  315. }
  316. }