springboot中使用JOIN实现关联表查询

* 首先要确保你的表和想要关联的表有外键连接

  • repository中添加接口JpaSpecificationExecutor<?>,就可以使用springboot jpa 提供的API了。
  • @Repository
    public interface MyEntityRepository extends JpaRepository<MyEntity, Integer>, JpaSpecificationExecutor<MyEntity> {
       //...
    }

    在查询方法中调用 JpaSpecificationExecutor 提供的 findAll() 方法,查询到我们需要的结果集,先上代码,后续说明

      • public Page<MyEntity> getMerchants(List<Integer> fKIds, String sortField, String entityName,
                    Integer pageNum) {
                Pageable pageable = PageRequest.of(pageNum, 20, Direction.DESC, sortField);
                @SuppressWarnings("serial")
                Page<MyEntity> page = myEntityRepository.findAll(new Specification<MyEntity>() {
                    
                    @Override
                    public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                       
                        if (fKIds!= null) {
                            Join<MyEntity, EntityDetails> join = root.join("entityDetails");
                            list.add(join .get("id").in(fKIds));
                        }
                        Predicate[] array = new Predicate[list.size()];
                        return cb.and(list.toArray(array));
                    }
                }, pageable);
                return page;
            }

        代码解释:

      • fKIds,这里为了顺便记录in查询,我查询的条件是 MyEntity.entityDetails.id in fKIds,就是关联表字段的in查询
      • sortField, pageNum为分页参数
      • toPredicate方法构建了查询条件的集合
      •    这里join的精髓就是,获取join对象,通过join对象进行查询条件的构建。
      • javax.persistence.criteria.Join<MyEntity, EntityDetails>
        
        
        A join to an entity, embeddable, or basic type.
        Type Parameters:<Z> the source type of the join
        <X> the target type of the join
        Since:Java Persistence 2.0

 

低调总结:真的不太用的来博客园这个编辑器(^_^),JPA其实蛮好用的,多用就知道了。

转载于:https://www.cnblogs.com/zhoujl-5071/p/10382976.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现联查询需要使用MyBatis的多表查询功能,并在Spring Boot进行整合。以下是一些基本步骤: 1. 在pom.xml添加MyBatis和MySQL依赖。 2. 创建实体类和Mapper接口。 3. 在MyBatis的Mapper配置文件编写联查询语句。 4. 在Spring Boot配置MyBatis。 5. 在Service调用Mapper接口方法实现联查询。 下面是一个示例: 1. 添加依赖 ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. 创建实体类和Mapper接口 ``` public class User { private Long id; private String name; private Integer age; private List<Article> articles; // getter and setter } public class Article { private Long id; private String title; private String content; private Long userId; // getter and setter } public interface UserMapper { User getUserById(Long id); } ``` 3. 编写联查询语句 在MyBatis的Mapper配置文件编写联查询语句,例如: ``` <select id="getUserById" resultType="User"> SELECT u.*, a.id as article_id, a.title as article_title, a.content as article_content FROM user u LEFT JOIN article a ON u.id = a.user_id WHERE u.id = #{id} </select> ``` 4. 配置MyBatis 在application.properties文件配置MyBatis的数据源、Mapper扫描路径等: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath*:mapper/*.xml ``` 5. 在Service调用Mapper接口方法实现联查询 ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.getUserById(id); } } ``` 以上就是使用Spring Boot整合MyBatis实现联查询的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值