SpringData JPA

SpringBoot整合JPA

1. 实体类

package com.tensquare.base.pojo;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * @author Oxygen
 * @create 2018-11-30 - 19:09
 */
@Entity //jpa需要的注解,指明实体类
@Table(name = "tb_label")
public class Label implements Serializable {
    @Id //主键注解
    private String id;
    private String labelname;//标签名称
    private String state;//状态
    private long count;//使用数量
    private long fans;//关注数
    private String recommend;//是否推荐

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabelname() {
        return labelname;
    }

    public void setLabelname(String labelname) {
        this.labelname = labelname;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public long getFans() {
        return fans;
    }

    public void setFans(long fans) {
        this.fans = fans;
    }

    public String getRecommend() {
        return recommend;
    }

    public void setRecommend(String recommend) {
        this.recommend = recommend;
    }
}
View Code

2. Dao

package com.tensquare.base.dao;

import com.tensquare.base.pojo.Label;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * 参数一:当前需要映射的实体
 * 参数二:当前映射的实体中的ID类型
 *
 * @author Oxygen
 * @create 2018-11-30 - 20:53
 */
public interface LabelDao extends JpaRepository<Label, String>,
        JpaSpecificationExecutor<Label> {

}
View Code

JPA核心接口

Repository

该接口提供了2种查询方式,基于方法名称命名查询方式基于@Query注解的查询与更新,基于方法名称命名查询方式:如果一个Dao接口实现了Repository接

口,则该接口中方法的命名需要遵循一定的规范,两种方式的代码如下:

package com.tensquare.base.test;

import com.tensquare.base.pojo.Label;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

import java.util.List;

/**
 * Repository接口的方法命名查询
 *
 * @author Oxygen
 * @create 2018-12-02 - 22:40
 */
public interface LabelDaoRopository extends Repository<Label, String> {
    //方法的名称必须要遵循:findBy+属性名称+查询条件
    List<Label> findByLabelname(String name);

    @Query(value = "select * from tb_label where labelname = ?", nativeQuery = true)
    List<Label> queryByLabelnameUseHQL(String name);
}
View Code

CrudRepository

该接口继承Repository接口,直接调用相关方法即可

PagingAndSortingRepository

该接口继承CrudRepository接口,提供了分页与排序的操作

View Code

JpaRepository

该接口继承了PagingAndSortingRepository接口,对继承的父接口的返回值进行了适配

JpaSpecificationExecutor

该接口单独存在,提供了多条件查询的支持,并且可以在查询中添加分页与排序,要配合其他接口一起使用

//单条件查询
    @Test
    public void testLabelDaoJpaSpecificationExecutor() {
        Specification<Label> specification = new Specification<Label>() { // specification 用于封装查询条件
            /**
             * Predicate 封装了单个的查询条件
             * @param root 根对象,就是把条件封装到那个对象中,即where 类名=label.getId
             * @param query 封装查询的关键字,例如 select, from, group by, order by 等(一般用不到)
             * @param cb 查询条件的构造器,定义不同的查询条件
             * @return 如果return null,表示不需要任何条件
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //where id = 1
                Predicate pre = cb.equal(root.get("id"), "1");
                return pre;
            }
        };

        List<Label> list = jpaSpecificationExecutor.findAll();
        for (Label label : list) {
            System.out.println(label);
        }
    }

    //多条件查询
    @Test
    public void testLabelDaoJpaSpecificationExecutor2() {
        Specification<Label> specification = new Specification<Label>() { // specification 用于封装查询条件
            /**
             * Predicate 封装了单个的查询条件
             * @param root 根对象,就是把条件封装到那个对象中,即where 类名=label.getId
             * @param query 封装查询的关键字,例如 select, from, group by, order by 等(一般用不到)
             * @param cb 查询条件的构造器,定义不同的查询条件
             * @return 如果return null,表示不需要任何条件
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // where id = 1 and name ="zhangsan"
                List<Predicate> list = new ArrayList<>();
                list.add(cb.equal(root.get("id"), "1"));
                list.add(cb.equal(root.get("name"), "zhangsan"));
                Predicate[] arr = new Predicate[list.size()];
                return cb.and(list.toArray(arr));
            }
        };

        List<Label> list = jpaSpecificationExecutor.findAll();
        for (Label label : list) {
            System.out.println(label);
        }
    }
View Code
原文地址:https://www.cnblogs.com/oxygenG/p/10060786.html