一、查询方式

1、导航式查询

  使用“对象.属性”   进行查询;对于多的查询, 默认就是延迟加载,添加注解@Transactional

  在OneToMany 注解中需要添加属性   fetch:值:FetchType.LAZY   延迟加载  FetchType.ENAGER   及时加载

通过联系人 查询客户;一的查询默认是及时加载,可以配置成延时加载

2、Specification查询

  需求   查询客户信息  根据联系人的手机号查
  需要查询  CustomerDao2需要继承JpaSpecificationExecutor接口
二、编写实体类
1、主表实体类关联从表添加属性所需添加的注解  @OneToMany(mappedBy = "customer",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
2、从表实体类关联主表添加属性所需添加的注解  @ManyToOne(fetch = FetchType.LAZY)  @JoinColumn(name="custid",referencedColumnName = "cust_id")
三、编写dao;主表da需要继承JpaRepository和JpaSpecificationExecutor从表dao需要继承JpaRepository
四、测试
package cn.zrf.jpa;

import cn.zrf.jpa.dao.Customer2Dao;
import cn.zrf.jpa.dao.LinkManDao;
import cn.zrf.jpa.entity.Customer2;
import cn.zrf.jpa.entity.LinkMan;
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 org.springframework.transaction.annotation.Transactional; import javax.persistence.criteria.*;
import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class QueryTest {
@Autowired
Customer2Dao customer2Dao;
@Autowired
LinkManDao linkManDao; @Test
@Transactional
public void queryCustomer(){
Customer2 one = customer2Dao.findOne(1l);
// System.out.println(one);
//根据客户查询联系人信息
Set<LinkMan> set = one.getLinkMans();
for(LinkMan linkMan:set){
System.out.println(linkMan);
}
}
//根据联系人查询客户
@Test
@Transactional
public void queryLinkMan(){
LinkMan one = linkManDao.findOne(3l);
System.out.println(one);
Customer2 customer2 = one.getCustomer2();
System.out.println(customer2);
}
//根据手机号查询
@Test
@Transactional
public void questPhone(){
Customer2 customer2 = customer2Dao.findOne(new Specification<Customer2>() {
@Override
public Predicate toPredicate(Root<Customer2> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//root:关联联系人表
//参数1、Customer2实体类中关联的属性的名称 参数2、连接方式
Join<Object, Object> join = root.join("linkMans", JoinType.LEFT);
//创建查询条件
Predicate predicate = criteriaBuilder.equal(join.get("lkmPhone"), "1234");
return predicate;
}
});
// System.out.println(customer2);
//获得客户信息查询联系人
Set<LinkMan> linkMans = customer2.getLinkMans();
for (LinkMan linkMan:linkMans){
System.out.println(linkMan);
} }
}

  

SpringData:关联查询的更多相关文章

  1. JDBC MySQL 多表关联查询查询

    public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...

  2. MYSQL基础操作之数据约束与关联查询

    一.MYSQL约束 1.默认值约束,当字段没有插入值的时候,mysql自动给该字段分配默认值. 默认值的字段允许为空. 对默认值字段也可以插入null. CREATE TABLE STUDENT( I ...

  3. C#代码中实现两个表(DataTable)的关联查询(JOIN)

    之前通常都是使用SQL直接从数据库中取出表1和表2关联查询后的数据,只需要用一个JOIN就可以了,非常方便.近日遇到一种情况,两个表中的数据已经取到代码中,需要在代码中将这两个表关联起来,并得到它们横 ...

  4. Mybatis关联查询和数据库不一致问题分析与解决

    Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...

  5. Mysql多表表关联查询 inner Join left join right join

    Mysql多表表关联查询 inner Join left join right join

  6. YII2-数据库数据查询方法,关联查询with, joinWith区别和分页

    一.ActiveRecord 活动记录 1.with关联查询 例如,查询评论 $post = Post::find()->with('comments'); 等价于以下结果集 SELECT * ...

  7. Mybatis高级查询之关联查询

    learn from:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps 关联查询 准备 关联结果查询(一对一) resul ...

  8. Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询

    在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...

  9. [NHibernate]一对多关系(关联查询)

    目录 写在前面 文档与系列文章 一对多查询 总结 写在前面 上篇文章介绍了nhibernate的一对多关系如何配置,以及级联删除,级联添加数据的内容.这篇文章我们将学习nhibernate中的一对多关 ...

随机推荐

  1. response没有实现跳转,而是提示浏览器下载文件

    问题简述: web项目中,response没能实现重定向跳转网页,而是通知浏览器下载文件. 代码如下: response.getWriter().write("<h1 style='c ...

  2. shiro:集成Springboot(六)

    1:导入相关依赖 <!--thymeleaf 模板引擎依赖包--> <dependency> <groupId>org.springframework.boot&l ...

  3. typeahead自动补全插件的limit参数问题

    遇到的问题很诡异: 后台返回的数据都正确就是显示不正常(有时多有时少),后来发现是typeahead的问题,在1.11版本之后,limit参数从option选项里改到了setdata选项: limit ...

  4. Python获取当前 年 月 日

    import datetime datetime.datetime.now().year datetime.datetime.now().month datetime.datetime.now().d ...

  5. opencv-9-图像噪声以及评估指标 PSNR 与SSIM

    开始之前 我们在将 opencv 的图像显示在了 qt 的label 上, 我们能够将图显示在label 上, 用于显示我们的算法, 我们在 opencv 上一篇文章中介绍了 opencv 的核操作, ...

  6. 从Spring迁移到Spring Boot

    文章目录 添加Spring Boot starters 添加应用程序入口 Import Configuration和Components 迁移应用程序资源 迁移应用程序属性文件 迁移Spring We ...

  7. Django中修改DATABASES后,执行python manage.py ****报错!UnicodeEncodeError

    Django中修改DATABASES后,执行python manage.py ****报错!UnicodeEncodeError: 'latin-1' codec can't encode chara ...

  8. flutter在2019年会有怎样的表现?

    2019独角兽企业重金招聘Python工程师标准>>> Flutter的趋势 在移动端,受成本和效率的驱使,跨平台一站式开发慢慢成为一个趋势.从Hybird,RN,WEEX,Flut ...

  9. 本周ASP.NET英文技术文章推荐[02/03 - 02/16]:MVC、Visual Studio 2008、安全性、性能、LINQ to JavaScript、jQuery...

    摘要 继续坚持,继续推荐.本期共有9篇文章: 最新的ASP.NET MVC框架开发计划 Visual Studio 2008 Web开发相关的Hotfix发布 ASP.NET安全性教程系列 ASP.N ...

  10. 项目Alpha冲刺 Day12

    1)站立式会议: 2)今日安排: 项目演示. 3)项目情况 项目进展:系统已实现预期的所有的功能.问题困难:系统测试不够全面,主要做功能测试,对于非功能测试,如压力测试.效能测试.安全性等并未测试.心 ...