SprinBoot+Jpa实现1024社区系统的最新职位推荐和最热职位推荐
Posted SmallCuteMonkey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SprinBoot+Jpa实现1024社区系统的最新职位推荐和最热职位推荐相关的知识,希望对你有一定的参考价值。
tensquare项目:
- 导入相关的依赖
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!--这个不会被引入依赖进行下载的,相当于一个接口-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
- 可以用专业的代码生成工具生成代码
- 生成以后在dao层进行相关的查询操作
package com.tensquare.recruit.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.tensquare.recruit.pojo.Recruit;
import java.util.List;
/**
* 数据访问接口
* @author Administrator
*
*/
public interface RecruitDao extends JpaRepository<Recruit,String>,JpaSpecificationExecutor<Recruit>{
/*查询推荐职位表,按照时间的倒序排列
state 状态: 0:关闭 1:开启 2:推荐 查询前四条记录,并且按照创建的时间降序排序
*
*/
public List<Recruit> findTop4ByStateOrderByCreatetime(String state);
/*
* 查询最新职位信息
* state!=0 和按照时间的倒序排序
* findTopBy12StateNotOOrderByCreatetimeDesc 这个约定的语句可以表示这个意思
* */
public List<Recruit> findTopBy12StateNotOrderByCreatetimeDesc(String state);
}
package com.tensquare.service;
import com.tensquare.dao.LabelDao;
import com.tensquare.pojo.Label;
import org.hibernate.loader.custom.Return;
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.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
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;
import java.util.Map;
@Service
public class LabelService {
@Autowired
private LabelDao labelDao;
@Autowired
private IdWorker idWorker;
// 查询所有
public List<Label> findAll(){
System.out.println(labelDao.findAll());
return labelDao.findAll();
}
// 查询单个
public Label findById(String id){
return labelDao.findById(id).get();
}
// 增加
public void addLabel(Label label){
label.setId(idWorker.nextId()+"");
labelDao.save(label);
}
/**
* 修改标签
* @param label
*/
public void update(Label label){
labelDao.save(label);
}
/**
* 删除标签
* @param id
*/
public void deleteById(String id){
labelDao.deleteById(id);
}
// 构建条件查询
public Specification<Label> createSpecification(Map searchMap){
return new Specification<Label>(){
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicateList=new ArrayList<>();
if(searchMap.get("labelname")!=null&&!"".equals(searchMap.get("labelname"))){
predicateList.add(criteriaBuilder.like(root.get("labelname").as(String.class),"%"+searchMap.get("labelname")+"%"));
}
if(searchMap.get("state")!=null&&!"".equals(searchMap.get("state"))){
predicateList.add(criteriaBuilder.equal(root.get("state").as(String.class),searchMap.get("state")));
}
if(searchMap.get("recommend")!=null&&!"".equals(searchMap.get("recommend"))){
predicateList.add(criteriaBuilder.equal(root.get("recommend").as(String.class),searchMap.get("recommend")));
}
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
/*
* 条件查询
*
* */
public List<Label> findSearch(Map searchMap){
return labelDao.findAll(createSpecification(searchMap));
}
/*分页查询
* @param 当前页
* @param size 页面大小
*
*
* */
// 方法重载
public Page<Label> findSearch(Map searchMap, int page, int size){
Specification<Label> specification = createSpecification(searchMap);
//官方文档说需要-1
PageRequest pageRequest= PageRequest.of(page-1, size);
return labelDao.findAll(specification,pageRequest);
}
}
- 实现了这两个接口JpaRepository<Recruit,String>,JpaSpecificationExecutor
- 这个是特殊的条件的查询,需要遵循相关的规范。
- 可以看到相关的分页查询的封装和特殊条件查询的封装。
以上是关于SprinBoot+Jpa实现1024社区系统的最新职位推荐和最热职位推荐的主要内容,如果未能解决你的问题,请参考以下文章
基于JAVA的sprinboot+vue车辆充电桩,附源码+数据库+论文+PPT
性能优化+架构迭代升级 Go读书社区web开发与架构优化(完整)微心:NoBug1024