SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

  软件152 尹以操html

首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33#java

这篇文章中讲的是spring中使用spring data jpa,使用了xml配置文件。我如今使用的是spring boot ,没有了xml文件配置就方便多了。我一样尝试了两种方式,也都是简单的查询,须要更复杂的查询,还须要我研究研究。往下看,须要先配置springboot的开发环境,须要大体了解springboot,这里能够看下面两篇文章:spring

springboot 项目新建sql

springboot使用小记数据库

建立实体类:安全

package com.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *create by yyc 2017年6月11日上午9:59:59
 */
@Entity
@Table(name="test_user")
public class TestUser {

    @Id
    @GeneratedValue
    private int userId;
    private Integer userAge;
    private String  userName;
    private Integer high;////省略getter、setter
    
}

为了测试,先建立一个简单的实体类。springboot

写元数据模型:dom

Criteria APIide

这套API可用于构建对数据库的查询。测试

类型安全。经过定义元数据模型,在程序编译阶段就能够对类型进行检查,不像SQL须要与Mysql进行交互后才能发现类型问题。

以下即为元数据模型。建立一个元模型类,类名最后一个字符为下划线,内部的成员变量与UserInfo.class这个实体类的属性值相对应。

package com.entity;

import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
import com.TestUser;
@StaticMetamodel(TestUser.class)
public class TestUser_ {

     public static volatile SingularAttribute<TestUser, Integer> userId;// 用户ID,自增量
     public static volatile SingularAttribute<TestUser, Integer> userAge;
     public static volatile SingularAttribute<TestUser, String> userName;
     public static volatile SingularAttribute<TestUser, Integer> high;

    }

可移植。API并不依赖具体的数据库,能够根据数据库类型的不一样生成对应数据库类型的SQL,因此其为可移植的。
面向对象。Criteria API是使用的是各类类和对象如CriteriaQuery、Predicate等构建查询,是面向对象的。而若是直接书写SQL则相对于面向的是字符串。

如今开始SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方式方法

第一种方式:经过JPA的Criteria API实现

  1. EntityManager获取CriteriaBuilder
  2. CriteriaBuilder建立CriteriaQuery
  3. CriteriaQuery指定要查询的表,获得Root<UserInfo>,Root表明要查询的表
  4. CriteriaBuilder建立条件Predicate,Predicate相对于SQL的where条件,多个Predicate能够进行与、或操做。
  5. 经过EntityManager建立TypedQuery
  6. TypedQuery执行查询,返回结果
package com.repository;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.entity.TestUser;
import com.entity.TestUser_;

/**
 *create by yyc 2017年6月11日下午9:08:39
 */
@Repository
public class TestUserExtendDao {

//    @Autowired
    @PersistenceContext//@Autowired和@PersistenceContext注解任取一
    EntityManager em;
    
    @SuppressWarnings("unused")
    public List<TestUser> getTestUserInfo(final Integer age, final String name, final Integer high){
         //1
        CriteriaBuilder cb = em.getCriteriaBuilder();
         //2
        CriteriaQuery<TestUser> query = cb.createQuery(TestUser.class);
        
        //3
        //from 
        Root<TestUser> root = query.from(TestUser.class);
        
         //4
         //where
        Predicate p1 = null;
        if (age != 0) {
            System.out.println("正在操做age!!!");
            Predicate p2 = cb.equal(root.get(TestUser_.userAge), age);
            if (p1 != null) {
                p1=cb.and(p1, p2);
            } else {
                p1 = p2;
            }
        }
        if (false==name.isEmpty()) {
            System.out.println("正在操做name!!!");
            Predicate p2 = cb.equal(root.get(TestUser_.userName), name);
            if (p1 != null) {
                p1=cb.and(p1, p2);
            } else {
                p1 = p2;
            }
        }
        if (high != 0) {
            System.out.println("正在操做high!!!");
            Predicate p2 = cb.equal(root.get(TestUser_.high), high);
            if (p1 != null) {
                p1=cb.and(p1, p2);
            } else {
                p1 = p2;
            }
        }
        //5
        query.where(p1);
        //6
        List<TestUser> testUsers = em.createQuery(query).getResultList();
        return testUsers;
    }
}
                                                                                        

第二种方式:DAO层接口实现JpaSpecificationExecutor<T>接口

  JpaSpecificationExecutor以下,方法参数Specification接口有一个方法toPredicate,返回值正好是Criteria API中的Predicate,而Predicate相对于SQL的where条件。与上一个方法相比,这种写法不须要指定查询的表是哪一张,也不须要本身经过Criteria API实现排序和分页,只须要经过新建Pageable、Sort对象并传参给findAll方法便可,简便一些。

这是JpaSpecificationExecutor接口中的方法:

public interface JpaSpecificationExecutor<T> {
   T findOne(Specification<T> spec);
   List<T> findAll(Specification<T> spec);
   Page<T> findAll(Specification<T> spec, Pageable pageable);
   List<T> findAll(Specification<T> spec, Sort sort);
   long count(Specification<T> spec);
}

TestRepository继承JpaSpecificationExecutor接口:

package com.repository;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.entity.TestUser;

/**
 *create by yyc 2017年6月11日上午9:36:27
 *测试动态sql
 */
public interface TestRepository extends PagingAndSortingRepository<TestUser, Integer>, JpaSpecificationExecutor<TestUser>{

}

实现Specification:

package com.entity;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import com.entity.TestUser;

/**
 * create by yyc 2017年6月11日上午10:17:44
 */
public class TestUserDaoSpec {
    public static Specification<TestUser> getSpec(final Integer age, final String name, final Integer high) {
        return new Specification<TestUser>() {

            @SuppressWarnings("unused")
            @Override
            public Predicate toPredicate(Root<TestUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = null;
                if (age != 0) {
                    System.out.println("正在操做age!!!");
                    Predicate p2 = cb.equal(root.get(TestUser_.userAge), age);
                    if (p1 != null) {
                        p1=cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                if (false==name.isEmpty()) {
                    System.out.println("正在操做name!!!");
                    Predicate p2 = cb.equal(root.get(TestUser_.userName), name);
                    if (p1 != null) {
                        p1=cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                if (high != 0) {
                    System.out.println("正在操做high!!!");
                    Predicate p2 = cb.equal(root.get(TestUser_.high), high);
                    if (p1 != null) {
                        p1=cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                return p1;
            }
        };
    }

}

Service层的调用测试类:

package com.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.entity.TestUser;
import com.entity.TestUserDaoSpec;
import com.repository.TestRepository;
import com.repository.TestUserExtendDao;
import com.result.Result;//一个方法返回的封装,这里直接忽略便可


/**
 *create by yyc 2017年6月11日上午9:40:51
 */
@Service("testService")
public class TestService {

    @Autowired
    private TestRepository testRepository;
    
    @Autowired
    private TestUserExtendDao testUserExtendDao;
    
    //测试第一种方式
    public Result getTestUsersByExtendDao(){
        List<TestUser> list = testUserExtendDao.getTestUserInfo(20, "", 170);//经过两个条件,string设为空
        printTestUserInfo(list);
        return new Result("查询成功!", list);
    }
    
    //测试第二种方式
    public Result getTestUsersByThreeParameter(){
        List<TestUser> list = testRepository.findAll(TestUserDaoSpec.getSpec(20, "yyc", 170));//经过三个条件
        printTestUserInfo(list);
        return new Result("查询成功!",list);
    }
    public Result getTestUsersByTwoParameter1(){
        List<TestUser> list = testRepository.findAll(TestUserDaoSpec.getSpec(20, "yyc", 0));//经过两个条件,Integer设为0
        printTestUserInfo(list);
        return new Result("查询成功!",list);
    }

    public Result getTestUsersByOneParameter(){
        List<TestUser> list = testRepository.findAll(TestUserDaoSpec.getSpec(0, "lrs", 0));//经过一个条件查询
        printTestUserInfo(list);
        return new Result("查询成功!",list);
    }

    private void printTestUserInfo(List<TestUser> list) {
        if (list!=null) {
            for (TestUser testUser : list) {
                System.out.println("userId:"+testUser.getUserId()+
                        " userName:"+testUser.getUserName()+
                        " userAge:"+testUser.getUserAge()+
                        " userHigh:"+testUser.getHigh());
            }
        }
        
    }
}

再次感谢大佬们的文章:

http://www.jianshu.com/p/45ad65690e33#

http://blog.csdn.net/ie8848520/article/details/8161986

http://www.cnblogs.com/jiangxiaoyaoblog/p/5635152.html

相关文章
相关标签/搜索