spring data jpa 多对多查询
package com.ideal.entity;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = “e_infotitle”)
@JsonIgnoreProperties(value = { “infomationSet”,“infoTitle” })
public class InfoTitle {
@Id
@GeneratedValue(generator = “idGenerator”)
@GenericGenerator(name = “idGenerator”, strategy = “guid”)
private String title_id;
private String infotype;//信息类型
private String is_login;//是否是登入
private String ispublish;//是否发布
private String link;
private String menhu;
private String order_no;
private String parent_id;
private String pictype;
private String title_name;
private String title_src;
private String title_status;
private String title_style;
private String titledepart;
private String titletype;
private String tree_code;
private String trs_titleid;
private String trs_titlename;
private String order_num;
@ManyToMany(mappedBy="titleSet")
private Set infomationSet ;
public Set getInfomationSet() {
return infomationSet;
}
public void setInfomationSet(Set infomationSet) {
this.infomationSet = infomationSet;
}
public String getTitle_id() {
return title_id;
}
public void setTitle_id(String title_id) {
this.title_id = title_id;
}
public String getInfotype() {
return infotype;
}
public void setInfotype(String infotype) {
this.infotype = infotype;
}
public String getIs_login() {
return is_login;
}
public void setIs_login(String is_login) {
this.is_login = is_login;
}
public String getIspublish() {
return ispublish;
}
public void setIspublish(String ispublish) {
this.ispublish = ispublish;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getMenhu() {
return menhu;
}
public void setMenhu(String menhu) {
this.menhu = menhu;
}
public String getOrder_no() {
return order_no;
}
public void setOrder_no(String order_no) {
this.order_no = order_no;
}
public String getParent_id() {
return parent_id;
}
public void setParent_id(String parent_id) {
this.parent_id = parent_id;
}
public String getPictype() {
return pictype;
}
public void setPictype(String pictype) {
this.pictype = pictype;
}
public String getTitle_name() {
return title_name;
}
public void setTitle_name(String title_name) {
this.title_name = title_name;
}
public String getTitle_src() {
return title_src;
}
public void setTitle_src(String title_src) {
this.title_src = title_src;
}
public String getTitle_status() {
return title_status;
}
public void setTitle_status(String title_status) {
this.title_status = title_status;
}
public String getTitle_style() {
return title_style;
}
public void setTitle_style(String title_style) {
this.title_style = title_style;
}
public String getTitledepart() {
return titledepart;
}
public void setTitledepart(String titledepart) {
this.titledepart = titledepart;
}
public String getTitletype() {
return titletype;
}
public void setTitletype(String titletype) {
this.titletype = titletype;
}
public String getTree_code() {
return tree_code;
}
public void setTree_code(String tree_code) {
this.tree_code = tree_code;
}
public String getTrs_titleid() {
return trs_titleid;
}
public void setTrs_titleid(String trs_titleid) {
this.trs_titleid = trs_titleid;
}
public String getTrs_titlename() {
return trs_titlename;
}
public void setTrs_titlename(String trs_titlename) {
this.trs_titlename = trs_titlename;
}
public String getOrder_num() {
return order_num;
}
public void setOrder_num(String order_num) {
this.order_num = order_num;
}
public InfoTitle() {
super();
}
public InfoTitle(String title_id) {
super();
this.title_id = title_id;
}
}
package com.ideal.entity;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = “e_infomation”)
@JsonIgnoreProperties(value = { “infomation” })
public class Infomation {
@Id
private String content_id;
private String attachs_path;
private String audituserid;
private String auditusername;
private String contact;
private String contactaddress;
@Lob
@Basic(fetch=FetchType.LAZY)
@Column(columnDefinition=“TEXT”)
private byte[] content;
private Date crtdate;
private Date date_time;
private String displayuser;
private String email;
private Date importdate;
private String include;
private Date info_endtime;
private Date info_starttime;
private String info_status;
private String info_type;
private String info_url;
private String info_user;
private Integer insertedoldsystem;
private String introduction;
private Integer is_main;
private Integer is_top;
private Integer is_view;
private Integer is_vip;
private String isimport;
private String isout;
private String ispic;
private String istohypl;
private String istojituan;
private String job;
private String keyword;
private String num;
private Integer order_no;
private String orgid;
private String senduserid;
private String sendusername;
private String sex;
private String simple_content;
private String subhead;
private Integer timenew;
private String title;
private String unit;
private String work_user;
private Date work_time;
private String workaddress;
@ManyToMany(cascade = CascadeType.DETACH,fetch=FetchType.EAGER)
@JoinTable(name = "e_title_info",
joinColumns = {@JoinColumn(name="content_id",referencedColumnName="content_id")},
inverseJoinColumns = {@JoinColumn(name = "title_id",referencedColumnName="title_id")})
private Set titleSet;
public Set getTitleSet() {
return titleSet;
}
public void setTitleSet(Set titleSet) {
this.titleSet = titleSet;
}
public String getContent_id() {
return content_id;
}
public void setContent_id(String content_id) {
this.content_id = content_id;
}
public String getAttachs_path() {
return attachs_path;
}
public void setAttachs_path(String attachs_path) {
this.attachs_path = attachs_path;
}
public String getAudituserid() {
return audituserid;
}
public void setAudituserid(String audituserid) {
this.audituserid = audituserid;
}
public String getAuditusername() {
return auditusername;
}
public void setAuditusername(String auditusername) {
this.auditusername = auditusername;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getContactaddress() {
return contactaddress;
}
public void setContactaddress(String contactaddress) {
this.contactaddress = contactaddress;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public Date getCrtdate() {
return crtdate;
}
public void setCrtdate(Date crtdate) {
this.crtdate = crtdate;
}
public Date getDate_time() {
return date_time;
}
public void setDate_time(Date date_time) {
this.date_time = date_time;
}
public String getDisplayuser() {
return displayuser;
}
public void setDisplayuser(String displayuser) {
this.displayuser = displayuser;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getImportdate() {
return importdate;
}
public void setImportdate(Date importdate) {
this.importdate = importdate;
}
public String getInclude() {
return include;
}
public void setInclude(String include) {
this.include = include;
}
public Date getInfo_endtime() {
return info_endtime;
}
public void setInfo_endtime(Date info_endtime) {
this.info_endtime = info_endtime;
}
public Date getInfo_starttime() {
return info_starttime;
}
public void setInfo_starttime(Date info_starttime) {
this.info_starttime = info_starttime;
}
public String getInfo_status() {
return info_status;
}
public void setInfo_status(String info_status) {
this.info_status = info_status;
}
public String getInfo_type() {
return info_type;
}
public void setInfo_type(String info_type) {
this.info_type = info_type;
}
public String getInfo_url() {
return info_url;
}
public void setInfo_url(String info_url) {
this.info_url = info_url;
}
public String getInfo_user() {
return info_user;
}
public void setInfo_user(String info_user) {
this.info_user = info_user;
}
public Integer getInsertedoldsystem() {
return insertedoldsystem;
}
public void setInsertedoldsystem(Integer insertedoldsystem) {
this.insertedoldsystem = insertedoldsystem;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public Integer getIs_main() {
return is_main;
}
public void setIs_main(Integer is_main) {
this.is_main = is_main;
}
public Integer getIs_top() {
return is_top;
}
public void setIs_top(Integer is_top) {
this.is_top = is_top;
}
public Integer getIs_view() {
return is_view;
}
public void setIs_view(Integer is_view) {
this.is_view = is_view;
}
public Integer getIs_vip() {
return is_vip;
}
public void setIs_vip(Integer is_vip) {
this.is_vip = is_vip;
}
public String getIsimport() {
return isimport;
}
public void setIsimport(String isimport) {
this.isimport = isimport;
}
public String getIsout() {
return isout;
}
public void setIsout(String isout) {
this.isout = isout;
}
public String getIspic() {
return ispic;
}
public void setIspic(String ispic) {
this.ispic = ispic;
}
public String getIstohypl() {
return istohypl;
}
public void setIstohypl(String istohypl) {
this.istohypl = istohypl;
}
public String getIstojituan() {
return istojituan;
}
public void setIstojituan(String istojituan) {
this.istojituan = istojituan;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Integer getOrder_no() {
return order_no;
}
public void setOrder_no(Integer order_no) {
this.order_no = order_no;
}
public String getOrgid() {
return orgid;
}
public void setOrgid(String orgid) {
this.orgid = orgid;
}
public String getSenduserid() {
return senduserid;
}
public void setSenduserid(String senduserid) {
this.senduserid = senduserid;
}
public String getSendusername() {
return sendusername;
}
public void setSendusername(String sendusername) {
this.sendusername = sendusername;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSimple_content() {
return simple_content;
}
public void setSimple_content(String simple_content) {
this.simple_content = simple_content;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public Integer getTimenew() {
return timenew;
}
public void setTimenew(Integer timenew) {
this.timenew = timenew;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getWork_user() {
return work_user;
}
public void setWork_user(String work_user) {
this.work_user = work_user;
}
public Date getWork_time() {
return work_time;
}
public void setWork_time(Date work_time) {
this.work_time = work_time;
}
public String getWorkaddress() {
return workaddress;
}
public void setWorkaddress(String workaddress) {
this.workaddress = workaddress;
}
}
*/
@RequestMapping("/infomation/query")
public String getInfoQueryList(String title_id,String keyword,String title,String subhead,Date crtdate,String auditusername, @RequestParam(defaultValue ="0") int pageNum, Model model) {
try {
System.out.println("keyword值》》》》》》》》》》》》》》》》》"+keyword);
System.out.println("title_id值》》》》》》》》》》》》》》》》》"+title_id);
System.out.println("title值》》》》》》》》》》》》》》》》》"+title);
System.out.println("pageNum值》》》》》》》》》》》》》》》》》"+pageNum);
System.out.println("subhead值》》》》》》》》》》》》》》》》》"+subhead);
System.out.println("mobile值》》》》》》》》》》》》》》》》》"+auditusername);
System.out.println("date值》》》》》》》》》》》》》》》》》"+crtdate);
List InfomationList = null;
Specification querySpecifi = new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder criteriaBuilder) {
List predicates = new ArrayList<>();
if (title != null && !title.equals("")) {
predicates.add(criteriaBuilder.like(root.get("title"), "%" + title + "%"));
}
if (keyword != null && !keyword.equals("")) {
predicates.add(criteriaBuilder.like(root.get("keyword"), "%" + keyword + "%"));
}
if (title_id != null && !title_id.equals("")) {
SetJoin title = root.join(root.getModel().getSet("titleSet", InfoTitle.class),JoinType.LEFT);
predicates.add(criteriaBuilder.equal(title.get("title_id"),title_id)) ;
}
if (crtdate != null && !crtdate.equals("")) {
predicates.add(criteriaBuilder.equal(root.get("crtdate"), crtdate));
}
if (auditusername != null && !auditusername.equals("")) {
predicates.add(criteriaBuilder.like(root.get("auditusername"), "%" + auditusername + "%"));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
// InfomationList = this.InfomationService.findAll(querySpecifi);
//分页
pageNum=pageNum<=0?0:pageNum-1;
Pageable pageable= PageRequest.of(pageNum,myFile.getPagesize());
Page InfomationPage = this.infomationService.findAll(querySpecifi, pageable);
log.info(InfomationPage.getTotalElements()+"");
model.addAttribute("InfomationList", InfomationPage);
model.addAttribute("pageable", pageable);
if(title_id == null){
return "/commons/infoPublish::info_list";
}else{
return "/commons/infoPublish::main_list";
}
} catch (Exception e) {
// TODO Auto-generated catch block
return e.getMessage();
}
}
@GetMapping("/infomation/download/{objid}")
public void downloadFileByOutputStream(HttpServletRequest request, HttpServletResponse response,@PathVariable String objid) {
Optional accessory= accessoryService.findById(objid);
Accessory acc=accessory.get();
String ledgerAttach_url=acc.getAccessory_url();
String fileName=acc.getAccessory();
// 1.获取要下载的文件的绝对路径
File file = new File(ledgerAttach_url);
// 2.获取要下载的文件名
// String fileName = “ddd.txt”;
// 3.设置content-disposition响应头控制浏览器以下载的形式打开文件
String userAgent = request.getHeader(“User-Agent”);
if(file.exists()){ //判断文件父目录是否存在
//response.setContentType(“application/force-download”);
//response.setHeader(“Content-Disposition”, “attachment;fileName=” + fileName);
try {
// 针对IE或者以IE为内核的浏览器:
if (userAgent.contains(“MSIE”) || userAgent.contains(“Trident”)) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else {
// 非IE浏览器的处理:
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setHeader("Content-disposition",String.format("attachment; filename=\"%s\"", fileName));
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;
OutputStream os = null; //输出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download" + fileName);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}