package cn.itcast.test;
import cn.itcast.dao.CustomerDao;
import cn.itcast.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
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.domain.Sort;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpecTest {
@Autowired
private CustomerDao customerDao;
/**
* 根据条件,查询单个对象
*
*/
@Test
public void testSpec() {
//匿名内部类
/**
* 自定义查询条件
* 1.实现Specification接口(提供泛型:查询的对象类型)
* 2.实现toPredicate方法(构造查询条件)
* 3.需要借助方法参数中的两个参数(
* root:获取需要查询的对象属性
* CriteriaBuilder:构造查询条件的,内部封装了很多的查询条件(模糊匹配,精准匹配)
* )
* 案例:根据客户名称查询,查询客户名为传智播客的客户
* 查询条件
* 1.查询方式
* cb对象
* 2.比较的属性名称
* root对象
*
*/
Specification<Customer> spec = new Specification<Customer>() {
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//1.获取比较的属性
Path<Object> custName = root.get("custName");
//2.构造查询条件 : select * from cst_customer where cust_name = '喵喵酱'
/**
* 第一个参数:需要比较的属性(path对象)
* 第二个参数:当前需要比较的取值
*/
Predicate predicate = cb.equal(custName, "喵喵酱");//进行精准的匹配 (比较的属性,比较的属性的取值)
return predicate;
}
};
Customer customer = customerDao.findOne(spec);
System.out.println(customer);
}
/**
* 多条件查询
* 案例:根据客户名(喵喵酱)和客户所属行业查询(it教育)
*
*/
@Test
public void testSpec1() {
/**
* root:获取属性
* 客户名
* 所属行业
* cb:构造查询
* 1.构造客户名的精准匹配查询
* 2.构造所属行业的精准匹配查询
* 3.将以上两个查询联系起来
*/
Specification<Customer> spec = new Specification<Customer>() {
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Path<Object> custName = root.get("custName");//客户名
Path<Object> custIndustry = root.get("custIndustry");//所属行业
//构造查询
//1.构造客户名的精准匹配查询
Predicate p1 = cb.equal(custName, "喵喵酱");//第一个参数,path(属性),第二个参数,属性的取值
//2..构造所属行业的精准匹配查询
Predicate p2 = cb.equal(custIndustry, "it教育");
//3.将多个查询条件组合到一起:组合(满足条件一并且满足条件二:与关系,满足条件一或满足条件二即可:或关系)
Predicate and = cb.and(p1, p2);//以与的形式拼接多个查询条件
// cb.or();//以或的形式拼接多个查询条件
return and;
}
};
Customer customer = customerDao.findOne(spec);
System.out.println(customer);
}
/**
* 案例:完成根据客户名称的模糊匹配,返回客户列表
* 客户名称以 ’喵喵酱‘ 开头
*
* equal :直接的到path对象(属性),然后进行比较即可
* gt,lt,ge,le,like : 得到path对象,根据path指定比较的参数类型,再去进行比较
* 指定参数类型:path.as(类型的字节码对象)
*/
@Test
public void testSpec3() {
//构造查询条件
Specification<Customer> spec = new Specification<Customer>() {
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//查询属性:客户名
Path<Object> custName = root.get("custName");
//查询方式:模糊匹配
Predicate like = cb.like(custName.as(String.class), "喵喵酱%");
return like;
}
};
// List<Customer> list = customerDao.findAll(spec);
// for (Customer customer : list) {
// System.out.println(customer);
// }
//添加排序
//创建排序对象,需要调用构造方法实例化sort对象
//第一个参数:排序的顺序(倒序,正序)
// Sort.Direction.DESC:倒序
// Sort.Direction.ASC : 升序
//第二个参数:排序的属性名称
Sort sort = new Sort(Sort.Direction.DESC,"custId");
List<Customer> list = customerDao.findAll(spec, sort);
for (Customer customer : list) {
System.out.println(customer);
}
}
/**
* 分页查询
* Specification: 查询条件
* Pageable:分页参数
* 分页参数:查询的页码,每页查询的条数
* findAll(Specification,Pageable):带有条件的分页
* findAll(Pageable):没有条件的分页
* 返回:Page(springDataJpa为我们封装好的pageBean对象,数据列表,共条数)
*/
@Test
public void testSpec4() {
Specification spec = null;
//PageRequest对象是Pageable接口的实现类
/**
* 创建PageRequest的过程中,需要调用他的构造方法传入两个参数
* 第一个参数:当前查询的页数(从0开始)
* 第二个参数:每页查询的数量
*/
Pageable pageable = new PageRequest(0,2);
//分页查询
Page<Customer> page = customerDao.findAll(null, pageable);
System.out.println(page.getContent()); //得到数据集合列表
System.out.println(page.getTotalElements());//得到总条数
System.out.println(page.getTotalPages());//得到总页数
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
jpa-day3-spec.zip (56个子文件)
jpa-day3-spec
src
test
java
cn
itcast
test
SpecTest.java 7KB
main
resources
applicationContext.xml 5KB
java
cn
itcast
domain
Customer.java 2KB
dao
CustomerDao.java 4KB
target
generated-test-sources
test-annotations
generated-sources
annotations
classes
cn
itcast
domain
Customer.class 3KB
dao
CustomerDao.class 1KB
applicationContext.xml 5KB
test-classes
cn
itcast
test
SpecTest$2.class 2KB
SpecTest.class 3KB
SpecTest$1.class 2KB
SpecTest$3.class 2KB
jpa-day3-spec.iml 5KB
.idea
libraries
Maven__dom4j_dom4j_1_6_1.xml 492B
Maven__org_javassist_javassist_3_18_1_GA.xml 580B
Maven__mysql_mysql_connector_java_5_1_6.xml 597B
Maven__junit_junit_4_12.xml 485B
Maven__xml_apis_xml_apis_1_0_b2.xml 532B
Maven__org_apache_geronimo_specs_geronimo_jta_1_1_spec_1_1_1.xml 684B
Maven__org_springframework_spring_tx_5_0_2_RELEASE.xml 632B
Maven__org_springframework_spring_expression_5_0_2_RELEASE.xml 688B
Maven__org_springframework_spring_context_5_0_2_RELEASE.xml 667B
Maven__org_jboss_jandex_2_0_0_Final.xml 557B
Maven__org_springframework_spring_beans_5_0_2_RELEASE.xml 653B
Maven__javax_el_javax_el_api_2_2_4.xml 553B
Maven__org_hibernate_hibernate_validator_5_2_1_Final.xml 664B
Maven__javax_validation_validation_api_1_1_0_Final.xml 641B
Maven__org_springframework_spring_test_5_0_2_RELEASE.xml 646B
Maven__org_slf4j_slf4j_api_1_6_6.xml 536B
Maven__org_springframework_data_spring_data_jpa_1_9_0_RELEASE.xml 694B
Maven__c3p0_c3p0_0_9_1_2.xml 495B
Maven__org_hibernate_common_hibernate_commons_annotations_5_0_1_Final.xml 762B
Maven__org_aspectj_aspectjweaver_1_6_8.xml 572B
Maven__org_hamcrest_hamcrest_core_1_3.xml 562B
Maven__org_hibernate_hibernate_core_5_0_7_Final.xml 629B
Maven__org_aspectj_aspectjrt_1_8_6.xml 544B
Maven__antlr_antlr_2_7_7.xml 492B
Maven__org_springframework_spring_context_support_5_0_2_RELEASE.xml 723B
Maven__org_springframework_spring_aop_5_0_2_RELEASE.xml 639B
Maven__org_hibernate_hibernate_entitymanager_5_0_7_Final.xml 692B
Maven__org_hibernate_javax_persistence_hibernate_jpa_2_1_api_1_0_0_Final.xml 750B
Maven__org_springframework_spring_jcl_5_0_2_RELEASE.xml 639B
Maven__org_springframework_spring_jdbc_5_0_2_RELEASE.xml 646B
Maven__org_springframework_spring_orm_5_0_2_RELEASE.xml 639B
Maven__com_fasterxml_classmate_1_1_0.xml 552B
Maven__org_jboss_logging_jboss_logging_3_3_0_Final.xml 638B
Maven__org_springframework_spring_core_5_0_2_RELEASE.xml 646B
Maven__org_glassfish_web_javax_el_2_2_4.xml 561B
Maven__org_springframework_data_spring_data_commons_1_11_0_RELEASE.xml 729B
Maven__org_slf4j_jcl_over_slf4j_1_7_12.xml 578B
Maven__org_slf4j_slf4j_log4j12_1_6_6.xml 564B
Maven__log4j_log4j_1_2_12.xml 499B
misc.xml 443B
compiler.xml 693B
workspace.xml 22KB
modules.xml 266B
pom.xml 5KB
共 56 条
- 1
资源评论
Insist___
- 粉丝: 4
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2%EF%BC%9A%E9%99%95%E8%A5%BF%E
- yyspdz62_944.apk
- SAP公司间采购EDI配置-如何触发自动MIRO.docx
- python197基于图像识别的仪表实时监控系统.rar
- python196基于循环神经网络的情感分类系统.rar
- I2C驱动SHT30温湿度传感器和LCD12864使用例程(RSCG12864B)
- python193中学地理-中国的江河湖泊教学网(django).rar
- python191基于时间序列分析的大气污染预测软件(django).rar
- python190基于人脸识别智能化小区门禁管理系统.rar
- python189某医院体检挂号系统.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功