tensquare项目:

  1. 导入相关的依赖
 <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>

  1. 可以用专业的代码生成工具生成代码
  2. 生成以后在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);


    }







}

  1. 实现了这两个接口JpaRepository<Recruit,String>,JpaSpecificationExecutor
  2. 这个是特殊的条件的查询,需要遵循相关的规范。
  3. 可以看到相关的分页查询的封装和特殊条件查询的封装。
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐