查询
package com.kuang.repositories;
import com.kuang.pojo.Customer;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
//extends CrudRepository
public interface CustomerRepository extends PagingAndSortingRepository {
//增删查改
//查询
@Query("from Customer where custName=?1")
List findCustomerByCustName(String custName);
//查询
@Query("from Customer where custName=:custName")
List findCustomerByCustName1(@Param("custName") String custName);
}
CustomerQueryByExampleRepository.java
package com.kuang.repositories;
import com.kuang.pojo.Customer;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import java.util.List;
public interface CustomerQueryByExampleRepository extends PagingAndSortingRepository , QueryByExampleExecutor {
}
QueryByExampleTest
package com.kuang.test;
import com.kuang.config.ApplicationConfig;
import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerQueryByExampleRepository;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@ContextConfiguration(classes = ApplicationConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class QueryByExampleTest {
@Autowired
private CustomerQueryByExampleRepository repository;
@Test
public void test01() {
Customer customer = new Customer();
customer.setCustName("lzl");
customer.setCustAddress("为鲁斯");
//通过Example构建查询条件 动态查询
Example of = Example.of(customer);
List list = (List) repository.findAll(of);
System.out.println(list);
}
/**
* 通过匹配器 进行条件的限制
* 简单实例 客户名称 客户地址动态查询
*
*/
@Test
public void test02() {
Customer customer = new Customer();
customer.setCustName("徐庶");
customer.setCustAddress("斯");
//通过Example构建查询条件 动态查询
ExampleMatcher matching = ExampleMatcher.matching().withIgnorePaths("custName")
.withMatcher("custAddress", ExampleMatcher.GenericPropertyMatchers.endsWith());//针对单个条件进行设置
// .withMatcher("custAddress", new ExampleMatcher.MatcherConfigurer() {
// @Override
// public void configureMatcher(ExampleMatcher.GenericPropertyMatcher matcher) {
// matcher.endsWith();
// }
// });
// .withStringMatcher(ExampleMatcher.StringMatcher.ENDING);//对所有条件字符串进行结尾匹配
Example of = Example.of(customer,matching);
List list = (List) repository.findAll(of);
System.out.println(list);
}
}
需要导入依赖整合前面的springdata-jpa
com.querydsl
querydsl-jpa
4.4.0
完整maven依赖还需要配置插件
springdata
com.kuang
1.0-SNAPSHOT
4.0.0
02-springdata-jpa
8
8
4.4.0
1.1.3
junit
junit
4.12
test
org.hibernate
hibernate-entitymanager
5.4.32.Final
mysql
mysql-connector-java
5.1.47
org.springframework.data
spring-data-jpa
com.alibaba
druid
1.2.8
org.springframework
spring-test
5.3.10
test
com.querydsl
querydsl-jpa
${querydsl.version}
com.mysema.maven
apt-maven-plugin
${apt.version}
com.querydsl
querydsl-apt
${querydsl.version}
generate-sources
process
target/generated-sources/queries
com.querydsl.apt.jpa.JPAAnnotationProcessor
true
定义接口
package com.kuang.repositories;
import com.kuang.pojo.Customer;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
public interface CustomerQueryDSLRepository extends PagingAndSortingRepository , QuerydslPredicateExecutor {
}
测试类
package com.kuang.test;
import com.kuang.config.ApplicationConfig;
import com.kuang.pojo.Customer;
import com.kuang.pojo.QCustomer;
import com.kuang.repositories.CustomerQueryDSLRepository;
import com.kuang.repositories.CustomerRepository;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@ContextConfiguration(classes = ApplicationConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class QueryDslTest {
@Autowired
private CustomerQueryDSLRepository customerQueryDSLRepository;
@Test
public void name() {
QCustomer customer = QCustomer.customer;
//通过ID查找
BooleanExpression eq = customer.custId.eq(5L);
System.out.println(customerQueryDSLRepository.findOne(eq));
}
/**
* 查询客户名称范围
* id > 大于
* 地址精确
*/
@Test
public void test02() {
QCustomer customer = QCustomer.customer;
BooleanExpression be = customer.custName.in("忽忽", "刘备")
.and(customer.custId.gt(0L))
.and(customer.custAddress.eq("杭州"));
System.out.println(customerQueryDSLRepository.findOne(be));
}
}
package com.kuang.repositories;
import com.kuang.pojo.Customer;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface CustomerSpecificationsRepository extends PagingAndSortingRepository, JpaSpecificationExecutor {
}
package com.kuang.test;
import com.kuang.config.ApplicationConfig;
import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import com.kuang.repositories.CustomerSpecificationsRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.persistence.criteria.*;
import java.util.List;
@ContextConfiguration(classes = ApplicationConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpecificaTest {
@Autowired
private CustomerSpecificationsRepository repository;
@Test
public void name() {
List all = repository.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder criteriaBuilder) {
//root from Customer //获取列
// CriteriaBuilder where 设置各种条件(> < in ..)
//query 组合 (order by , where )
return null;
}
});
}
/**
* 查询客户范围(in)
* id > 大于
* 地址 精确
*/
@Test
public void select() {
List all = repository.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
//root from Customer //获取列
// CriteriaBuilder where 设置各种条件(> < in ..)
//query 组合 (order by , where )
Path custID = root.get("custId");
Path custName = root.get("custName");
Path custAddress = root.get("custAddress");
//参数1:为那个字段设置条件 参数2 :值
Predicate custNameP = cb.equal(custName, "刘备");
Predicate custIDP = cb.greaterThan(custID,0L);
CriteriaBuilder.In in = cb.in(custAddress);
in.value("叙述").value("wangwu");
Predicate and = cb.and(custIDP,custNameP,in);
return and;
}
});
System.out.println(all);
}
@Test
public void dongtaiSQL() {
Customer customer = new Customer();
customer.setCustName("老六");
customer.setCustId(0L);
customer.setCustAddress("徐庶,王五");
}
}