1、新建资讯实体类
import com.yzyxtech.project.tianfutalents.common.entity.IdentityEntity;
import com.yzyxtech.project.tianfutalents.core.entity.company.Company;
import com.yzyxtech.project.tianfutalents.core.entity.company.EnterpriseMember;
import com.yzyxtech.project.tianfutalents.core.entity.enums.NewsStatus;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
/**
* Created by zhao on 2018/11/2.
* 资讯
*/
@Getter//自动生成getter和setter--》lombok
@Setter
@ToString
@Entity
public class News extends IdentityEntity{
@ManyToOne
private NewsColumn newsColumn;//资讯栏目 类似板块、如农业资讯、招聘资讯、政策法规等
@ManyToOne
private NewsClassify newsClassify;//资讯分类
@ApiModelProperty("标题")
private String title;
@ApiModelProperty("简介")
private String synopsis;
@ApiModelProperty("封面图")
private String headImg;
@ApiModelProperty("内容")
private String content;
@ApiModelProperty("公司")
@ManyToOne
private Company company;
@ApiModelProperty("发布人")
@ManyToOne
private EnterpriseMember enterpriseMember;
@ApiModelProperty("状态")
@Enumerated(EnumType.STRING)
private NewsStatus status;
}
枚举类状态
public interface EnumGetNameInterface {
String getName();
}
import com.yzyxtech.project.tianfutalents.common.serializer.jackson.EnumGetNameInterface;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Created by gyw on 2018/11/9.
*/
@Getter
@AllArgsConstructor//自动生成全字段构造函数
public enum NewsStatus implements EnumGetNameInterface {
APPROVED("审核通过"),
PENDING("待审"),
REJECTED("不通过");
private String name;
}
2、新建资讯表单搜索
import com.yzyxtech.project.tianfutalents.common.utils.DateUtils;
import com.yzyxtech.project.tianfutalents.core.entity.company.Company;
import com.yzyxtech.project.tianfutalents.core.entity.company.CompanyProduct;
import com.yzyxtech.project.tianfutalents.core.entity.company.EnterpriseMember;
import com.yzyxtech.project.tianfutalents.core.entity.enums.NewsStatus;
import com.yzyxtech.project.tianfutalents.core.entity.headhunter.Customer;
import com.yzyxtech.project.tianfutalents.core.entity.headhunter.CustomerContact;
import com.yzyxtech.project.tianfutalents.core.entity.headhunter.Project;
import com.yzyxtech.project.tianfutalents.core.entity.news.News;
import com.yzyxtech.project.tianfutalents.core.entity.news.NewsClassify;
import com.yzyxtech.project.tianfutalents.core.entity.news.NewsColumn;
import com.yzyxtech.project.tianfutalents.core.entity.setting.City;
import com.yzyxtech.project.tianfutalents.core.entity.setting.WorkFunction;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 企业资讯条件查询
* @Author: gyw
* @CreateDate: 2018/11/23 11:18
*/
@Getter
@Setter
@ToString
public class NewsSearch implements Specification {
@ApiModelProperty("资讯ID")
private Long id;
@ApiModelProperty("资讯栏目")
private String newsColumnName;
@ApiModelProperty("资讯分类")
private String newsClassifyName;
@ApiModelProperty("标题")
private String title;
@ApiModelProperty("公司")
private String companyName;
@ApiModelProperty("发布人")
private String enterpriseMemberName;
@ApiModelProperty("状态[APPROVED,PENDING,REJECTED]")
private String status;
@Override
public Predicate toPredicate(Root root, CriteriaQuery> criteriaQuery, CriteriaBuilder cb) {
List list = new ArrayList<>();
if (this.id != null) {
list.add(cb.equal(root.get("id"), this.id));
}
if (StringUtils.isNotBlank(this.newsColumnName)) {
Join newsNewsColumnJoin = root.join("newsColumn");//关联表
list.add(cb.like(newsNewsColumnJoin.get("name"),"%" + this.newsColumnName + "%"));
}
if (StringUtils.isNotBlank(this.newsClassifyName)) {
Join newsNewsClassifyJoin = root.join("newsClassify");//关联表
list.add(cb.like(newsNewsClassifyJoin.get("name"),"%" + this.newsClassifyName + "%"));
}
if (StringUtils.isNotBlank(this.title)) {
list.add(cb.like(root.get("title"), "%" + this.title + "%"));
}
if (StringUtils.isNotBlank(this.companyName)) {
Join companyJoin = root.join("company");关联表
list.add(cb.like(companyJoin.get("name"),"%" + this.companyName + "%"));
}
if (StringUtils.isNotBlank(this.enterpriseMemberName)) {
Join enterpriseMember = root.join("enterpriseMember");
list.add(cb.like(enterpriseMember.get("name"),"%" + this.enterpriseMemberName + "%"));
}
if (this.status != null) {
list.add(cb.equal(root.get("status"), NewsStatus.valueOf(this.status)));
}
/*if (StringUtils.isNotBlank(this.status != null) {
Predicate p1 = cb.like(root.get(Recruitment_.name), "%" + search.getQ() + "%");
Predicate p2 = cb.like(root.get(Recruitment_.tags), "%" + search.getQ() + "%");
Predicate p3 = cb.like(root.get(Recruitment_.description), "%" + search.getQ() + "%");
list.add(cb.or(p1, p2, p3));
}
if (this.startTime != null) {//大于等于
list.add(cb.greaterThanOrEqualTo(root.get(News_.startTime), DateUtils.localDate2OffsetDateTime(this.startTime)));
}
if (this.endTime != null) {//小于等于
list.add(cb.lessThanOrEqualTo(root.get(News_.startTime), DateUtils.localDate2OffsetDateTime(this.endTime)));
}*/
return cb.and(list.toArray(new Predicate[0]));
}
}
3、新建返回数据
import com.yzyxtech.project.tianfutalents.core.entity.company.Company;
import com.yzyxtech.project.tianfutalents.core.entity.company.EnterpriseMember;
import com.yzyxtech.project.tianfutalents.core.entity.enums.NewsStatus;
import com.yzyxtech.project.tianfutalents.core.entity.news.News;
import com.yzyxtech.project.tianfutalents.core.entity.news.NewsClassify;
import com.yzyxtech.project.tianfutalents.core.entity.news.NewsColumn;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.BeanUtils;
/**
* @Description: 资讯返回数据
* @Author: gyw
* @CreateDate: 2018/11/24 13:38
*/
@Getter
@Setter
@ToString
public class NewsJson {
@ApiModelProperty("资讯ID")
private Long id;
@ApiModelProperty("资讯栏目")
private NewsColumn newsColumn;
@ApiModelProperty("资讯分类")
private NewsClassify newsClassify;
@ApiModelProperty("标题")
private String title;
@ApiModelProperty("简介")
private String synopsis;
@ApiModelProperty("封面图")
private String headImg;
@ApiModelProperty("内容")
private String content;
@ApiModelProperty("公司")
private Company company;
@ApiModelProperty("发布人")
private Long enterpriseMemberId;
@ApiModelProperty("发布姓名")
private String enterpriseMemberName;
@ApiModelProperty("状态[APPROVED,PENDING,REJECTED]")
private NewsStatus status;//枚举
public static NewsJson convert(News entity) {
NewsJson json = new NewsJson();
BeanUtils.copyProperties(entity, json);
EnterpriseMember enterpriseMember = entity.getEnterpriseMember();
if(enterpriseMember==null){
enterpriseMember = new EnterpriseMember();
}
json.setEnterpriseMemberId(enterpriseMember.getId());
json.setEnterpriseMemberName(enterpriseMember.getName());
return json;
}
}
3、查询简单封装
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 部分查询封装
* @Author: gyw
* @CreateDate: 2018/11/30 9:28
*/
public class SelectUtil {
/**
* 选择是否要查询删除的
* @param deleted true-查询删除的 false-未删除 null-所以
* @param
*/
public static Specification getIsDeleted(Boolean deleted){
Specification specification = (root, query, cb)->{
List predicates = new ArrayList<>();
predicates.add(SelectUtil.getPredicate(deleted,root, query, cb));
return cb.and(predicates.toArray(new Predicate[0]));
};
return specification;
}
/**
* 添加删除条件查询
* @param deleted true-查询删除的 false-未删除 null-所以
* @param root
* @param criteriaQuery
* @param cb
* @param
* @return
*/
public static Predicate getPredicate(Boolean deleted, Root root, CriteriaQuery> criteriaQuery, CriteriaBuilder cb){
List predicates = new ArrayList<>();
if(deleted==null){
return cb.and(predicates.toArray(new Predicate[0]));
}else if(deleted){
predicates.add(cb.isFalse(root.get("enabled")));
predicates.add(cb.isTrue(root.get("deleted")));
}else{
predicates.add(cb.isTrue(root.get("enabled")));
predicates.add(cb.isFalse(root.get("deleted")));
}
return cb.and(predicates.toArray(new Predicate[0]));
}
public static void main(String[] args){
}
}
4、新建ServiceImpl类
import com.yzyxtech.project.tianfutalents.common.domain.PageResult;
import com.yzyxtech.project.tianfutalents.core.entity.news.News;
import com.yzyxtech.project.tianfutalents.core.repository.news.NewsRepository;
import com.yzyxtech.project.tianfutalents.core.resource.json.news.NewsJson;
import com.yzyxtech.project.tianfutalents.core.resource.news.form.NewsSearch;
import com.yzyxtech.project.tianfutalents.core.service.news.NewsService;
import com.yzyxtech.project.tianfutalents.core.utils.SelectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class NewsServiceImpl implements NewsService {
@Resource
private NewsRepository newsRepository;
@Override
@Transactional//事物--如果有延迟加载可以用这个在session后关闭前在返回json中对延迟数据直接拿到
public PageResult listPageByCondition(NewsSearch newsSearch, Integer page, Integer pageSize) {
Page newsPage = newsRepository.findAll(((root, query, cb) -> {
List list = new ArrayList<>();
list.add(newsSearch.toPredicate(root, query, cb));
list.add(SelectUtil.getPredicate(false,root, query, cb));
return cb.and(list.toArray(new Predicate[0]));
}), PageRequest.of(page - 1, pageSize));
return new PageResult<>//返回数据类型样式
(
newsPage.getTotalElements(),
newsPage.getTotalPages(),
pageSize,
newsPage.getContent().stream()
.map(NewsJson::convert)
.collect(Collectors.toList())
);
}
}
5、dao类接口
import com.yzyxtech.project.tianfutalents.core.entity.news.News;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
//JpaRepository--部分查询--@NoRepositoryBean(自带这个注解继承就自动注入)
//JpaSpecificationExecutor--分页条件查询
public interface NewsRepository extends JpaRepository, JpaSpecificationExecutor {
@Query(value = "from News where newsClassify.id=?1 and deleted=0 and enabled = 1")
List findByNewsClassifyId(Long newsClassifyId);
}
6推荐
https://blog.csdn.net/qq_30054997/article/details/79420141
https://blog.csdn.net/qq_34083066/article/details/80733512