Explorar el Código

album page list

xushengqiang hace 6 años
padre
commit
3d49c2f179

+ 3 - 7
src/main/java/cn/efunbox/audio/AudioApplication.java

@@ -1,18 +1,14 @@
 package cn.efunbox.audio;
 
+import cn.efunbox.audio.repository.base.ProjectSimpleJpaRepository;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
-import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-import org.springframework.cloud.client.loadbalancer.LoadBalanced;
-import org.springframework.cloud.netflix.feign.EnableFeignClients;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.web.client.RestTemplate;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 
 @SpringBootApplication
 //@EnableDiscoveryClient
 //@EnableFeignClients
+@EnableJpaRepositories(repositoryBaseClass = ProjectSimpleJpaRepository.class)
 public class AudioApplication {
 
 	public static void main(String[] args) {

+ 3 - 11
src/main/java/cn/efunbox/audio/controller/AlbumController.java

@@ -1,6 +1,7 @@
 package cn.efunbox.audio.controller;
 
 import cn.efunbox.audio.entity.Album;
+import cn.efunbox.audio.page.OnePage;
 import cn.efunbox.audio.service.AdminService;
 import cn.efunbox.audio.service.AlbumService;
 import cn.efunbox.audio.service.TrailService;
@@ -9,7 +10,6 @@ import cn.efunbox.audio.utils.Common;
 import cn.efunbox.audio.utils.HttpUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Page;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;
-import java.util.Objects;
 
 /**
  * AlbumController
@@ -70,16 +69,9 @@ public class AlbumController {
     }
 
     @RequestMapping(method = RequestMethod.GET)
-    public void list(HttpServletRequest request, HttpServletResponse response,Integer pageNo,Integer pageSize){
+    public void list(HttpServletRequest request, HttpServletResponse response,Album album, Integer pageNo, Integer pageSize){
 
-        if (Objects.isNull(pageNo)) {
-            pageNo = 1;
-        }
-        if (Objects.isNull(pageSize)) {
-            pageSize = 10;
-        }
-
-        Page<Album> albumPage = albumService.SearchAll(pageNo,pageSize);
+        OnePage<Album> albumPage = albumService.SearchAll(album,pageNo,pageSize);
 
         HttpUtil.responseOkData(request, response, albumPage);
         return;

+ 19 - 0
src/main/java/cn/efunbox/audio/entity/BaseOrderEnum.java

@@ -0,0 +1,19 @@
+package cn.efunbox.audio.entity;
+
+public enum BaseOrderEnum {
+
+    DESC("降序"), ASC("升序");
+    String name;
+    BaseOrderEnum(String name) {
+        this.name = name;  
+    }
+      
+    public String getName() {  
+        return name;  
+    }
+    @Override
+    public String toString() {
+        return this.name;
+    }
+
+}  

+ 35 - 0
src/main/java/cn/efunbox/audio/helper/SortHelper.java

@@ -0,0 +1,35 @@
+package cn.efunbox.audio.helper;
+
+import cn.efunbox.audio.entity.BaseOrderEnum;
+import org.springframework.data.domain.Sort;
+import org.springframework.util.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+
+/**
+ * SortHelper
+ * Created by xusq on 2017/5/22.
+ */
+public class SortHelper {
+
+
+    public static Sort sortMap2Sort(LinkedHashMap<String,BaseOrderEnum> sortMap){
+        if (CollectionUtils.isEmpty(sortMap)) {
+            return null;
+        }
+
+        List<Sort.Order> sorts = new ArrayList<>();
+        sortMap.forEach((prop, order) -> {
+            if (BaseOrderEnum.ASC.equals(order)) {
+                sorts.add(new Sort.Order(Sort.Direction.ASC, prop));
+            } else {
+                sorts.add(new Sort.Order(Sort.Direction.DESC, prop));
+            }
+        });
+
+        return new Sort(sorts);
+
+    }
+}

+ 28 - 7
src/main/java/cn/efunbox/audio/impl/AlbumServiceImpl.java

@@ -1,14 +1,16 @@
 package cn.efunbox.audio.impl;
 
 import cn.efunbox.audio.entity.Album;
+import cn.efunbox.audio.entity.BaseOrderEnum;
+import cn.efunbox.audio.helper.SortHelper;
+import cn.efunbox.audio.page.OnePage;
 import cn.efunbox.audio.repository.AlbumRepo;
 import cn.efunbox.audio.service.AlbumService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.data.domain.Pageable;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 
 /**
@@ -18,6 +20,9 @@ import java.util.List;
 @Service
 public class AlbumServiceImpl implements AlbumService {
 
+    @Value("${efunbox.oss.img.url}")
+    private String imgURL;
+
     @Autowired
     private AlbumRepo albumRepo;
 
@@ -27,10 +32,26 @@ public class AlbumServiceImpl implements AlbumService {
     }
 
     @Override
-    public Page<Album> SearchAll(int page, int size) {
-        Pageable pageable = new PageRequest(page, size);
-        Page<Album> list = albumRepo.findAll(pageable);
-        return list;
+    public OnePage<Album> SearchAll(Album album,Integer page, Integer pageSize) {
+
+        long count = albumRepo.count(album);
+        OnePage onePage = new OnePage(count,page,pageSize);
+
+        if (count == 0) {
+            return onePage;
+        }
+        List<Album> albumList = albumRepo.find(album, onePage.getStart(), onePage.getPageSize(), SortHelper.sortMap2Sort(new LinkedHashMap<String, BaseOrderEnum>() {{
+            put("modified", BaseOrderEnum.DESC);
+        }}));
+        albumList.stream().forEach(albumConsumer -> albumConsumer.setImage(imgURL + albumConsumer.getImage()));
+        onePage.setList(albumList);
+        return onePage;
+    }
+
+    private void fillAlbum(List<Album> list) {
+
+
+
     }
 
     @Override

+ 257 - 0
src/main/java/cn/efunbox/audio/page/OnePage.java

@@ -0,0 +1,257 @@
+/**
+ * 文件名:@OnePage.java <br/>
+ * 包名:cn.efunbox.afw.core.domain.page <br/>
+ * 项目名:afw-core <br/>
+ * @author xtwin <br/>
+ */
+package cn.efunbox.audio.page;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * 类名:OnePage  <br />
+ *
+ * 功能:分页,一页
+ *
+ * @author xtwin <br />
+ * 创建时间:2016年7月28日 上午11:26:12  <br />
+ * @version 2016年7月28日
+ */
+public class OnePage<E> implements Pageable<List<E>> ,Serializable {
+
+	private static final long serialVersionUID = 1L;
+	// 默认分页数据条数
+	public final static int DEFAULT_PAGE_SIZE = 10;
+	
+	// 当前页
+	private int pageNo = 1;
+	
+	// 当前页大小
+	private int pageSize = DEFAULT_PAGE_SIZE;
+	
+	// 总条数
+	private long totalSize;
+	
+	// 数据内容
+	private List<E> list;
+
+	/**
+	 * 构造方法
+	 */
+	protected OnePage() {
+		// 默认构造方法
+	}
+	
+	/**
+	 * 构造方法
+	 */
+	public OnePage(Long totalSize) {
+		this(totalSize, null, null);
+	}
+	
+	/**
+	 * 构造方法
+	 */
+	public OnePage(Long totalSize, Integer pageNo) {
+		this(totalSize, pageNo, null);
+	}
+	
+	/**
+	 * 构造方法
+	 */
+	public OnePage(Long totalSize, Integer pageNo, Integer pageSize) {
+		// 总记录数
+		if (null != totalSize) {
+			if (totalSize < 0) {
+				throw new RuntimeException("totalSize must be not less than zero!");
+			}
+			this.totalSize = totalSize;
+		}
+		
+		// 当前页码
+		if (null != pageNo) {
+			if (pageNo < 0) {
+				throw new RuntimeException("pageNo must be not less than zero!");
+			}
+			this.pageNo = pageNo;
+		}
+
+		
+		// 当前页数据条数
+		if (null != pageSize) {
+			if (pageSize < 0) {
+				throw new RuntimeException("pageSize must be not less than zero!");
+			}
+			
+			this.pageSize = pageSize;
+		}
+
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月31日 上午9:27:27<br/>
+	 * 
+	 * @return <br/>
+	 */
+	@Override
+	public int getTotalNo() {
+		return totalSize == 0 ? 1 : (int) ((totalSize / pageSize) + (totalSize % pageSize == 0 ? 0 : 1));
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月31日 上午9:27:27<br/>
+	 * 
+	 * @return <br/>
+	 */
+	@Override
+	public long getTotalSize() {
+		return totalSize;
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月31日 上午9:27:27<br/>
+	 * 
+	 * @return <br/>
+	 */
+	@Override
+	public int getPageNo() {
+		return pageNo;
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月31日 上午9:27:27<br/>
+	 * 
+	 * @return <br/>
+	 */
+	@Override
+	public int getPageSize() {
+		return pageSize;
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月31日 上午9:27:27<br/>
+	 * 
+	 * @return <br/>
+	 */
+	@Override
+	public boolean hasNext() {
+		return getPageNo() < getTotalNo();
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月31日 上午9:27:27<br/>
+	 * 
+	 * @return <br/>
+	 */
+	@Override
+	public boolean hasPrevious() {
+		return getPageNo() > 1;
+	}
+
+	/**
+	 * @version 2016年7月31日-上午10:57:49
+	 */
+	public void setPageNo(int pageNo) {
+		this.pageNo = pageNo;
+	}
+
+	/**
+	 * @version 2016年7月31日-上午10:57:49
+	 */
+	public void setPageSize(int pageSize) {
+		this.pageSize = pageSize;
+	}
+
+	/**
+	 * @version 2016年7月31日-上午10:57:49
+	 */
+	public void setTotalSize(int totalSize) {
+		/*if (totalSize < 0) {
+			throw new AfwRuntimeException("totalSize must be not less than zero!");
+		}*/
+		this.totalSize = totalSize;
+	}
+	
+	/**
+	 * 功能:bean规范 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月31日 下午12:54:59 <br/>
+	 */
+	public boolean isHasNext() {
+		return hasNext();
+	}
+	
+	/**
+	 * 功能:bean规范 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月31日 下午12:55:22 <br/>
+	 */
+	public boolean isHasPrevious() {
+		return hasPrevious();
+	}
+
+	/**
+	 * @version 2016年8月1日-上午10:00:32
+	 */
+	public void setList(List<E> list) {
+		this.list = list;
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年8月1日 上午9:49:32<br/>
+	 * 
+	 * @return <br/>
+	 * @see Pageable#getList()
+	 */
+	@Override
+	public List<E> getList() {
+		return null == list ? Collections.emptyList() : list;
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年8月1日 上午10:05:48<br/>
+	 * 
+	 * @return <br/>
+	 * @see Pageable#getStart()
+	 */
+	@Override
+	public long getStart() {
+		return (getPageNo() - 1) * getPageSize();
+	}
+}

+ 83 - 0
src/main/java/cn/efunbox/audio/page/Pageable.java

@@ -0,0 +1,83 @@
+/**
+ * 文件名:@Pageable.java <br/>
+ * 包名:cn.efunbox.afw.core.domain.page <br/>
+ * 项目名:afw-core <br/>
+ * @author xtwin <br/>
+ */
+package cn.efunbox.audio.page;
+
+/**
+ * 类名:Pageable  <br />
+ *
+ * 功能:分页接口
+ *
+ * @author xtwin <br />
+ * 创建时间:2016年7月28日 上午11:18:56  <br />
+ * @version 2016年7月28日
+ */
+public interface Pageable<E> {
+	
+	/**
+	 * 功能:当前页的数据内容 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月28日 上午11:43:27 <br/>
+	 */
+	E getList();
+
+	/**
+	 * 功能:总页数 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:47:45 <br/>
+	 */
+	int getTotalNo();
+	
+	/**
+	 * 功能:总记录数 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:48:40 <br/>
+	 */
+	long getTotalSize();
+
+	/**
+	 * 功能:页码 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:48:33 <br/>
+	 */
+	int getPageNo();
+	
+	/**
+	 * 功能:每页数据条数 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:48:25 <br/>
+	 */
+	int getPageSize();
+	
+	/**
+	 * 功能:起始位置 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午10:02:52 <br/>
+	 */
+	long getStart();
+	
+	/**
+	 * 功能:是否还有下一页 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:48:07 <br/>
+	 */
+	boolean hasNext();
+	
+	/**
+	 * 功能:是否还有上一页 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:47:58 <br/>
+	 */
+	boolean hasPrevious();
+}

+ 3 - 1
src/main/java/cn/efunbox/audio/repository/AlbumRepo.java

@@ -1,17 +1,19 @@
 package cn.efunbox.audio.repository;
 
 import cn.efunbox.audio.entity.Album;
+import cn.efunbox.audio.repository.base.ProjectJpaRepository;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
 
+import javax.rmi.CORBA.PortableRemoteObjectDelegate;
 import java.util.List;
 
 /**
  * Created by yao on 17-9-26.
  */
 @Repository
-public interface AlbumRepo extends JpaRepository<Album, Long> {
+public interface AlbumRepo extends ProjectJpaRepository<Album, Long> {
 
     @Query(value = "select a.id From album a where a.name like concat('%',?1,'%')", nativeQuery = true)
     List<Long> findIdsByNameLike(String name);

+ 2 - 1
src/main/java/cn/efunbox/audio/repository/AudioRepo.java

@@ -1,6 +1,7 @@
 package cn.efunbox.audio.repository;
 
 import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.repository.base.ProjectJpaRepository;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
@@ -14,7 +15,7 @@ import java.util.List;
  * Created by yao on 17-9-26.
  */
 @Repository
-public interface AudioRepo extends JpaRepository<Audio, Long> {
+public interface AudioRepo extends ProjectJpaRepository<Audio, Long> {
 
     Audio findById(Long id);
 

+ 26 - 0
src/main/java/cn/efunbox/audio/repository/base/BasicRepository.java

@@ -0,0 +1,26 @@
+/**
+ * 文件名:@BasicRepository.java <br/>
+ * 包名:tv.acfun.base.repository <br/>
+ * 项目名:acfun-base-provider <br/>
+ * @author xtwin <br/>
+ */
+package cn.efunbox.audio.repository.base;
+
+import org.springframework.data.repository.NoRepositoryBean;
+
+
+/**
+ * 类名:BaseRepository  <br />
+ *
+ * 功能:基础持久仓库
+ *
+ * @author xtwin <br />
+ * 创建时间:2016年7月27日 上午10:59:21  <br />
+ * @version 2016年7月27日
+ */
+@NoRepositoryBean
+public interface BasicRepository<E> extends ProjectJpaRepository<E,String> {
+
+
+
+}

+ 153 - 0
src/main/java/cn/efunbox/audio/repository/base/ProjectJpaRepository.java

@@ -0,0 +1,153 @@
+/**
+ * 文件名:@ProjectJpaRepository.java <br/>
+ * 包名:cn.efunbox.afw.data.jpa.spring.support <br/>
+ * 项目名:afw-data <br/>
+ * @author xtwin <br/>
+ */
+package cn.efunbox.audio.repository.base;
+
+import org.springframework.data.domain.Sort;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.repository.NoRepositoryBean;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * 类名:ProjectJpaRepository  <br />
+ *
+ * 功能:
+ *
+ * @author xtwin <br />
+ * 创建时间:2016年7月26日 下午3:20:36  <br />
+ * @version 2016年7月26日
+ */
+@NoRepositoryBean
+public interface ProjectJpaRepository<E, ID extends Serializable> extends JpaRepository<E, ID> {
+
+	/**
+	 * 功能:统计符合样板的数据条数 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月31日 下午4:56:01 <br/>
+	 */
+	long count(E sample);
+	
+	/**
+	 * 功能:检查是否存在 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月17日 下午12:04:48 <br/>
+	 */
+	boolean exists(E sample);
+
+	/**
+	 * 功能:根据id查询 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月30日 下午6:28:23 <br/>
+	 */
+	E find(ID id);
+	/**
+	 * 功能:查询符合条件的第一条 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月2日 上午11:55:36 <br/>
+	 */
+	E findFirst(E sample);
+
+	/**
+	 * 功能:根据ids查询 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月30日 下午6:28:23 <br/>
+	 */
+	List<E> findByIds(List<ID> id);
+
+	/**
+	 * 功能:查询符合条件的第一条 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月2日 上午11:58:10 <br/>
+	 */
+	E findFirst(E sample, Sort sort);
+
+	/**
+	 * 功能:分页查询所有数据,使用id降序排序 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月31日 下午4:52:30 <br/>
+	 */
+	List<E> find(Long start, Integer offset);
+	
+	/**
+	 * 功能:分页查询,使用sort规则排序 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月29日 上午10:19:05 <br/>
+	 */
+	List<E> find(Long start, Integer offset, Sort sort);
+	
+	/**
+	 * 功能:根据样板查询数据,使用id降序排序 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月30日 下午6:51:00 <br/>
+	 */
+	List<E> find(E sample);
+	
+	/**
+	 * 功能:根据样板查询数据,使用sort规则排序 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月29日 上午10:19:51 <br/>
+	 */
+	List<E> find(E sample, Sort sort);
+	
+	/**
+	 * 功能:分页查询,按id降序排序 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月31日 上午11:58:31 <br/>
+	 */
+	List<E> find(E sample, Long start, Integer offset);
+	
+	/**
+	 * 功能:分页查询,且排序 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月29日 上午9:44:26 <br/>
+	 */
+	List<E> find(E sample, Long start, Integer offset, Sort sort);
+	
+	/**
+	 * 功能:更新操作,不更新为null的字段 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:35:47 <br/>
+	 */
+	E update(E entity);
+	/**
+	 * 功能:基于原生merge方法的 批量更新操作
+	 *
+	 * @author  tomas <br/>
+	 * @version 2017年8月1日 上午9:35:47 <br/>
+	 */
+	<S extends E> List<S> update(Iterable<S> entities);
+
+	/**
+	 * 功能:基于原生merge方法的 批量save操作
+	 *
+	 * @author  tomas <br/>
+	 * @version 2017年8月1日 上午9:35:47 <br/>
+	 */
+	<S extends E> List<S> save(Iterable<S> entities);
+	
+	/**
+	 * 功能:更新操作,可以选择是否要忽略更新为null的字段 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年8月1日 上午9:36:20 <br/>
+	 */
+	E update(E entity, boolean ignoreNull);
+}

+ 574 - 0
src/main/java/cn/efunbox/audio/repository/base/ProjectSimpleJpaRepository.java

@@ -0,0 +1,574 @@
+/**
+ * 文件名:@AfwSimpleJpaRepository.java <br/>
+ * 包名:cn.efunbox.afw.data.jpa.spring.support <br/>
+ * 项目名:afw-data <br/>
+ * @author xtwin <br/>
+ */
+package cn.efunbox.audio.repository.base;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.domain.Sort.Direction;
+import org.springframework.data.domain.Sort.Order;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.data.jpa.repository.support.JpaEntityInformation;
+import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.ReflectionUtils;
+import org.springframework.util.StringUtils;
+
+import javax.persistence.EntityManager;
+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 javax.persistence.metamodel.Attribute;
+import javax.persistence.metamodel.Attribute.PersistentAttributeType;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * 类名:AfwSimpleJpaRepository  <br />
+ *
+ * 功能:Jpa仓库基础实现
+ *
+ * @author xtwin <br />
+ * 创建时间:2016年7月26日 下午3:19:42  <br />
+ * @version 2016年7月26日
+ */
+public class ProjectSimpleJpaRepository<E, ID extends Serializable> extends SimpleJpaRepository<E, ID> implements ProjectJpaRepository<E, ID> {
+	
+	// 日志记录器
+	private static final Logger logger = LoggerFactory.getLogger(ProjectSimpleJpaRepository.class);
+	
+	// jpa管理器对象
+	private EntityManager entityManager;
+	
+	// 实体信息
+	private JpaEntityInformation<E, ID> entityInformation;
+	
+	/**
+	 * 构造方法
+	 */
+	public ProjectSimpleJpaRepository(JpaEntityInformation<E, ID> entityInformation, EntityManager entityManager) {
+		super(entityInformation, entityManager);
+		
+		// 保留该对象,方便后续使用
+		this.entityInformation = entityInformation;
+		this.entityManager = entityManager;
+	}
+
+	/**
+	 * 功能: <br/>
+	 * 
+	 * 重写:xtwin <br/>
+	 * 
+	 * @version :2016年7月26日 下午3:23:06<br/>
+	 * 
+	 * @param sample
+	 * @return <br/>
+	 * @see ProjectJpaRepository#count(Object)
+	 */
+	@Override
+	public long count(E sample) {
+		return count(getSpecification(sample));
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年8月17日 下午12:05:21<br/>
+	 *
+	 * @param sample
+	 * @return <br/>
+	 * @see ProjectJpaRepository#exists(Object)
+	 */
+	@Override
+	public boolean exists(E sample) {
+		return count(sample) > 0L;
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月26日 下午3:23:06<br/>
+	 *
+	 * @param id
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Serializable)
+	 */
+	@Override
+	public E find(ID id) {
+		return findOne(id);
+	}
+	/**
+	 * 功能:根据ids查询 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年3月30日 下午6:28:23 <br/>
+	 */
+	public List<E> findByIds(List<ID> ids){
+		return findAll(ids);
+	}
+
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月26日 下午3:23:06<br/>
+	 *
+	 * @param start
+	 * @param offset
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Long, Integer)
+	 */
+	@Override
+	public List<E> find(Long start, Integer offset) {
+		return find(start, offset, null);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月29日 上午10:20:21<br/>
+	 *
+	 * @param start
+	 * @param offset
+	 * @param sort
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Long, Integer, Sort)
+	 */
+	@Override
+	public List<E> find(Long start, Integer offset, Sort sort) {
+		return find(null, start, offset, sort);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年8月2日 上午11:58:30<br/>
+	 *
+	 * @param sample
+	 * @return <br/>
+	 * @see ProjectJpaRepository#findFirst(Object)
+	 */
+	@Override
+	public E findFirst(E sample) {
+		return findFirst(sample, null);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年8月2日 上午11:58:30<br/>
+	 *
+	 * @param sample
+	 * @param sort
+	 * @return <br/>
+	 * @see ProjectJpaRepository#findFirst(Object, Sort)
+	 */
+	@Override
+	public E findFirst(E sample, Sort sort) {
+		List<E> list = find(sample, 0L, 1, sort);
+
+		return null == list || list.isEmpty() ? null : list.get(0);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月26日 下午3:23:06<br/>
+	 *
+	 * @param sample
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Object)
+	 */
+	@Override
+	public List<E> find(E sample) {
+		return find(sample, null);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月29日 上午10:20:21<br/>
+	 *
+	 * @param sample
+	 * @param sort
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Object, Sort)
+	 */
+	@Override
+	public List<E> find(E sample, Sort sort) {
+		return find(sample, null, null,sort);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月26日 下午3:23:06<br/>
+	 *
+	 * @param sample
+	 * @param start
+	 * @param offset
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Object, Long, Integer)
+	 */
+	@Override
+	public List<E> find(E sample, Long start, Integer offset) {
+		return find(sample, start, offset, null);
+	}
+
+	/**
+	 * 功能: <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年7月29日 上午9:44:56<br/>
+	 *
+	 * @param sample
+	 * @param start
+	 * @param offset
+	 * @param sort
+	 * @return <br/>
+	 * @see ProjectJpaRepository#find(Object, Long, Integer, Sort)
+	 */
+	@Override
+	public List<E> find(E sample, Long start, Integer offset, Sort sort) {
+		// 创建qbe
+		Specification<E> spec = getSpecification(sample);
+
+		if (null == sort) {
+			// 默认按id降序排序
+			sort = new Sort(new Order(Direction.DESC, entityInformation.getIdAttribute().getName()));
+		}
+
+		// 取得查询对象
+		TypedQuery<E> query = getQuery(spec, sort);
+
+		// 起始条数,从零开始
+		if (null != start) {
+			// TODO 此处只接受整形值
+			query.setFirstResult(start.intValue());
+		}
+
+		// 查询条数
+		if (null != offset) {
+			query.setMaxResults(offset);
+		}
+
+		// 执行查询
+		return query.getResultList();
+	}
+
+
+	/**
+	 * 功能: 更新操作,不更新为null的字段 <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年8月1日 上午9:37:15<br/>
+	 *
+	 * @param entity
+	 * @return <br/>
+	 * @see ProjectJpaRepository#update(Object)
+	 */
+	@Transactional
+	public E update(E entity) {
+		return update(entity, true);
+	}
+
+	/**
+	 * Saves all given entities.
+	 *
+	 * @param entities
+	 * @return the saved entities
+	 * @throws IllegalArgumentException in case the given domain is {@literal null}.
+	 */
+	@Override
+	@Transactional
+	public <S extends E> List<S> save(Iterable<S> entities) {
+
+		return insertOrUpdateBatch(entities);
+	}
+	/**
+	 * Saves a given domain. Use the returned instance for further operations as the save operation might have changed the
+	 * domain instance completely.
+	 *
+	 * @param entity
+	 * @return the saved domain
+	 */
+	@Transactional
+	public <S extends E> S save(S entity) {
+
+		if (entityInformation.isNew(entity)) {
+//			setSnowflakeIdId(entity);
+			entityManager.persist(entity);
+			return entity;
+		} else {
+			return entityManager.merge(entity);
+		}
+	}
+	/**
+	 * 功能: 动态数据更新,可以选择是否需要更新null字段 <br/>
+	 *
+	 * 重写:xtwin <br/>
+	 *
+	 * @version :2016年8月1日 上午9:37:05<br/>
+	 *
+	 * @param entity
+	 * @param ignoreNull
+	 * @return <br/>
+	 * @see ProjectJpaRepository#update(Object, boolean)
+	 */
+	@Transactional
+	public E update(E entity, boolean ignoreNull) {
+		// 取得当前实体的id
+		ID id = entityInformation.getId(entity);
+		
+		// 检查id是否为空Ø
+		if (null == id) {
+			throw new RuntimeException("The update domain id must be not empty !");
+		}
+		
+		// 查询库中当前对象的信息
+		E persist = find(id);
+		
+		// 检查该id对应的数据是否存在
+		if (null == persist) {
+			throw new RuntimeException("The update domain id is not exist : " + id);
+		}
+		
+		// 标识是否发生了变化
+		boolean isChanged = false;
+		
+		// 取得所有属性
+		for (Attribute<? super E, ?> attr : entityManager.getMetamodel().entity(getDomainClass()).getAttributes()) {
+			// 依次处理每个字段
+			/*PersistentAttributeType patype = attr.getPersistentAttributeType();
+
+			if (PersistentAttributeType.MANY_TO_ONE.equals(patype) 
+					|| PersistentAttributeType.ONE_TO_MANY.equals(patype)) {
+
+				// 忽略
+				continue;
+			}*/
+			
+			Member member = attr.getJavaMember();
+			
+			Field field = null;
+			if (member instanceof Field) {
+				field = (Field) member;
+			} else {
+				field = ReflectionUtils.findField(getDomainClass(), attr.getName());
+			}
+			
+			// 取得字段值
+			Object value = ReflectionUtils.getField(field, entity);
+			if (null != value || ! ignoreNull) {
+				// 旧值
+				Object oldValue = ReflectionUtils.getField(field, persist);
+				
+				if ((null == value && null != oldValue) 
+						|| (null != value && ! value.equals(oldValue))) {
+					
+					// 将新值更新到持久化对象中
+					ReflectionUtils.setField(field, persist, value);
+					
+					// 标记为发生了更新
+					isChanged = true;
+				}
+			}
+		}
+		
+		if (isChanged) {
+			persist = save(persist);
+		} else {
+			// 没有发生变更,忽略更新
+			logger.debug("has no changed : {}", id);
+		}
+		
+		// 执行保存并返回
+		return persist;
+	}
+	/**
+	 * update all given entities.
+	 *
+	 * @param entities
+	 * @return the saved entities
+	 * @throws IllegalArgumentException in case the given domain is {@literal null}.
+	 */
+	@Transactional
+	public <S extends E> List<S> update(Iterable<S> entities) {
+		return insertOrUpdateBatch(entities);
+	}
+
+	private <S extends E> List<S> insertOrUpdateBatch(Iterable<S> entities) {
+		List<S> result = new ArrayList<>();
+		if (entities == null) {
+			return result;
+		}
+		Iterator<S> iterator = entities.iterator();
+		int i = 0;
+		int count = 0;
+		//遍历循环 每20个 insert 批量插入/更新 一次库
+		while (iterator.hasNext()) {
+			S entity = iterator.next();
+			if (entityInformation.isNew(entity)) {
+//				setSnowflakeIdId(entity);
+				entityManager.persist(entity);
+			} else {
+				update(entity);
+			}
+			result.add(entity);
+			i++;
+			if (i % 20 == 0) {
+				entityManager.flush();
+				entityManager.clear();
+
+				count=0;
+			}else {
+				count++;
+			}
+		}
+		//判断 是否有剩余未flush的 最后flush
+		if (count>0){
+			entityManager.flush();
+			entityManager.clear();
+		}
+		return result;
+	}
+
+	/**
+	 * 功能:获取qbe <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月29日 上午9:43:25 <br/>
+	 */
+	protected Specification<E> getSpecification(E sample) {
+		return null == sample ? null : new Specification<E>() {
+			@Override
+			public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
+				try {
+					// 获取qbe
+					return ProjectSimpleJpaRepository.this.toPredicate(sample, root, query, cb);
+				} catch (Exception e) {
+					throw new RuntimeException("toPredicate fail with error : " + String.valueOf(e), e);
+				}
+			}
+		};
+	}
+
+	/**
+	 * 功能:转为查询样板,可以由子类重写 <br/>
+	 *
+	 * @author xtwin <br/>
+	 * @version 2016年7月29日 上午10:15:27 <br/>
+	 * @throws IllegalAccessException 
+	 * @throws IllegalArgumentException 
+	 */
+	protected Predicate toPredicate(E sample, Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) throws Exception {
+		// 存放查询条件
+		List<Predicate> list = new ArrayList<Predicate>();
+		
+		// 处理实体中的所有字段
+		for (Attribute<? super E, ?> attr : entityManager.getMetamodel().entity(getDomainClass()).getAttributes()) {
+			// 依次处理每个字段
+			PersistentAttributeType patype = attr.getPersistentAttributeType();
+
+			// 打印日志
+			logger.debug("the attr type is : {}", patype);
+
+			if (PersistentAttributeType.MANY_TO_ONE.equals(patype) 
+					|| PersistentAttributeType.ONE_TO_MANY.equals(patype)) {
+
+				// 忽略
+				continue;
+			}
+
+			Object value = null;
+			
+			Member member = attr.getJavaMember();
+			if (member instanceof Method) {
+				value = ReflectionUtils.invokeMethod((Method) member, sample);
+			} else if (member instanceof Field) {
+				//((Field) member).setAccessible(true);
+				// 确保可以访问
+				ReflectionUtils.makeAccessible((Field) member);
+				
+				// 取得字段的值
+				value = ((Field) member).get(sample);
+			}
+			
+			if (null != value) {
+				Predicate tmp = null;
+				//like 查询放在最前面
+				List<Predicate> likePredicate = new ArrayList<Predicate>();
+				if (String.class.isAssignableFrom(attr.getJavaType()) ) {
+					if(!StringUtils.isEmpty(value)){
+						StringBuilder stringBuilder=new StringBuilder(((String) value).trim());
+						//如果以 % 开头和结尾 表示 like 查询
+						if("%".equals(stringBuilder.substring(0,1)) || stringBuilder.toString().endsWith("%")){
+							tmp = cb.like(root.get(attr.getName()), (String) value);
+							likePredicate.add(tmp);
+						}else{
+							tmp = cb.equal(root.get(attr.getName()), value);
+							list.add(tmp);
+						}
+					}
+				} else  {
+					tmp = cb.equal(root.get(attr.getName()), value);
+					list.add(tmp);
+				}
+				if(likePredicate.size()>0){
+					list.addAll(0,likePredicate);
+				}
+			}
+		}
+		
+		// where条件
+		return list.isEmpty() ? null : cb.and(list.toArray(new Predicate[list.size()]));
+	}
+
+
+	/*protected void setSnowflakeIdId(E entity){
+		try {
+			Field field = ReflectionUtils.findField(entity.getClass(),entityInformation.getIdAttribute().getName());
+			if(entityInformation.getIdAttribute().getType().getJavaType().getName().equals("java.lang.Long")){
+				ReflectionUtils.makeAccessible(field);
+				ReflectionUtils.setField(field, entity, SnowflakeIdUtil.getSnowflakeIdUtil().nextId());
+			}else if(entityInformation.getIdAttribute().getType().getJavaType().getName().equals("java.lang.String")) {
+				logger.debug(" uuid  entityInformation.getIdAttribute().getName() ={}",entityInformation.getIdAttribute().getName());
+				ReflectionUtils.makeAccessible(field);
+				ReflectionUtils.setField(field, entity, UUID.randomUUID().toString());
+			}
+		}catch (Exception e){
+			logger.error("Reflect set id 出错 mag ={}",e.getMessage(),e);
+		}
+	}*/
+}

+ 2 - 2
src/main/java/cn/efunbox/audio/service/AlbumService.java

@@ -1,7 +1,7 @@
 package cn.efunbox.audio.service;
 
 import cn.efunbox.audio.entity.Album;
-import org.springframework.data.domain.Page;
+import cn.efunbox.audio.page.OnePage;
 
 import java.util.List;
 
@@ -13,7 +13,7 @@ public interface AlbumService {
 
     public Album GetOne(Long id);
 
-    public Page<Album> SearchAll(int page, int size);
+    public OnePage<Album> SearchAll(Album album,Integer page, Integer size);
 
     public Album Insert(Album album);