需求:有实体类UserInfo存储用户的信息,Confession存储表白墙的信息。在confession用字段userId存储用户的id,然后在表白墙页面展示表白内容时需要同时有发布该内容的用户姓名。
页面效果:
实体类UserInfo:
package com.zongmao.school.entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
* @Author: ZongMao
* @Date: 12:56 2020/2/16
* @Description:
* @Modified By:
*/
@Entity
public class UserInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String account;
private String password;
private String email;
private String sex;
private String headImg; /*用以存储头像存储的链接*/
private String sign; /*用户的个性签名*/
private String changePwLink; /*changePwLink指通过邮箱进行密码修改的链接*/
private Integer studentId; /*学号id*/
private Integer dormitory; /*寝室id*/
//get和set方法省略
}
实体类Confession:
package com.zongmao.school.entity;
import javax.persistence.*;
/**
* @Author: ZongMao
* @Date: 13:03 2020/3/8
* @Description:
* @Modified By:
*/
@Entity
public class Confession {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer userId;/*发布者id*/
private String content;/*内容*/
private String time;/*时间*/
private String state;/*状态,用以判断是否匿名*/
//get和set省略
}
定义一个实体类ConfessionVo存放存储查询的结果:
package com.zongmao.school.VoEntity;
/**
* @Author: ZongMao
* @Date: 12:04 2020/3/10
* @Description:
* @Modified By:
*/
public class ConfessionVo {
private Integer userId;/*发布者id*/
private String content;/*内容*/
private String time;/*时间*/
private String state;/*状态,用以判断是否匿名*/
private String account;/*发布者姓名*/
public ConfessionVo(){
}
public ConfessionVo(Integer userId,String content,String time,String state,String account){
this.userId = userId;
this.content = content;
this.time = time;
this.state = state;
this.account= account;
}
//get和set省略
}
Dao层:此处需与ConfessionVo属性一一对应
/**
* @Author: ZongMao
* @Date: 13:12 2020/3/8
* @Description:
* @Modified By:
*/
public interface ConfessionRepositories extends JpaRepository {
@Query("select new com.zongmao.school.VoEntity.ConfessionVo(c.userId,c.content,c.time,c.state,u.account) from Confession c , UserInfo u where c.userId=u.id order by c.time desc")
public List listConfession();
}
service层:
List confessionVos = confessionRepositories.listConfession();
此时已经可以查询出所有的信息。但是前端需要分页,每次只需要固定的条数,需求升级。
此时尝试了单表查询时的limit,无法实现,所以写一个方法对查询出来的list集合进行分页。
分页方法ListPage:
package com.zongmao.school.config;
import com.alipay.api.internal.parser.json.ObjectJsonParser;
import com.zongmao.school.VoEntity.ConfessionVo;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: ZongMao
* @Date: 14:31 2020/3/10
* @Description:自定义List分页方法
* list 需要分页的list
* pageNum 页码
* pageSize 每页需要多少数据
* @Modified By:
*/
public class ListPage {
public static List startPage(List list, Integer pageNum, Integer pageSize) {
if (list == null) {
return null;
}
if (list.size() == 0) {
return null;
}
Integer count = list.size(); // 记录总数
Integer pageCount = 0; // 页数
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = 0; // 开始索引
int toIndex = 0; // 结束索引
if (pageNum != pageCount) {
fromIndex = (pageNum - 1) * pageSize;
toIndex = fromIndex + pageSize;
} else {
fromIndex = (pageNum - 1) * pageSize;
toIndex = count;
}
List pageList = list.subList(fromIndex, toIndex);
return pageList;
}
}
server层:
List confessionVos = confessionRepositories.listConfession();
List confessionPage = ListPage.startPage(confessionVos,1,needSize);
通过调整参数即可实现自定义分页。
如果有其他方法实现多表查询并分页这个功能,请大佬告知。主要是对jpa不懂。