场景描述:对于同一张表例如:
需要展示的效果样式:
根据相册id选择,需要将该id相册内的照片的对时间排序分组,将同一天的照片放在起来,放在一个photolist中,且每一个photolist的时间提取出来就像List<Map<key,value>>一样
数据格式如下:
[{“time”:“20191012”,“photoList”:[{“id”:7,“albumId”:1,“userId”:1,“pdescribe”:null,“ptime”:“20191012”,“place”:“beij”,“path”:""},{“id”:8,“albumId”:1,“userId”:1,“pdescribe”:null,“ptime”:“20191012”,“place”:“北京”,“path”:""}]}
数据格式中包含两个类,一个是photo类,一个是定义的一个photo的集合类
public class Photo {
private int id;
private int albumId;
private int userId;
private String pdescribe;
private String ptime;
private String place;
private String path;
private String identify;
//以及getset方法toString方法,或者构造方法等
}
public class PhotoList {
private String ptime;
private List<Photo> photoLists;
//getset 、toString方法
}
PhotoController
@RequestMapping("/findByAlbum")
public String findByAlbum(@RequestParam("albumId") String albumId) {
System.out.println("根据相册查询图片");
List<PhotoList> lists=null;
Gson gson=new GsonBuilder().serializeNulls().create();
if (albumId != null && !albumId.equals("")) {
lists=photoService.findByAlbum(albumId);
}
String gsonString=gson.toJson(lists);
System.out.println(gsonString);
return gsonString;
}
PhotoMapper
public List<PhotoList> findByAlbum(String albumId);
PhotoServicce
public List<PhotoList> findByAlbum(String albumId){return photoMapper.findByAlbum(albumId);};
PhotoMapper.xml
<resultMap type="com.timeset.photo.entity.PhotoList" id="PhotoListMap">
<id property="ptime" column="ptime"/>
<collection property="photoLists" ofType="com.timeset.photo.entity.Photo">
<result property="id" column="id"/>
<result property="albumId" column="album_id"/>
<result property="userId" column="user_id"/>
<result property="ptime" column="ptime"/>
<result property="pdescribe" column="pdescribe"/>
<result property="place" column="place"/>
<result property="path" column="path"/>
<result property="identify" column="identify"/>
</collection>
</resultMap>
<select id="findByAlbum" resultMap="PhotoListMap">
select * from photo where album_id=#{albumId} order by ptime desc
</select>
功能到此便能够实现,需要注意的是select的时候只用orderby就能够实现对同一天的分组,不能再使用group by