Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Boot JPA动态查询构建_Spring_Spring Data Jpa - Fatal编程技术网

Spring Boot JPA动态查询构建

Spring Boot JPA动态查询构建,spring,spring-data-jpa,Spring,Spring Data Jpa,我有两个表,下面是表的结构 CREATE TABLE `tbl_directors` ( `id` BIGINT (3) NOT NULL AUTO_INCREMENT, `name` VARCHAR (50) NOT NULL, `status` ENUM ('active', 'inactive', 'pending') NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(), `upda

我有两个表,下面是表的结构

CREATE TABLE `tbl_directors` (
  `id` BIGINT (3) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR (50) NOT NULL,
  `status` ENUM ('active', 'inactive', 'pending') NOT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
  PRIMARY KEY (`id`),
  UNIQUE KEY (`name`)
) ;

CREATE TABLE `tbl_movies` (
  `id` BIGINT (5) NOT NULL AUTO_INCREMENT,
  `director_id` BIGINT (3) NOT NULL,
  `title` VARCHAR (50) NOT NULL,
  `status` ENUM ('active', 'inactive', 'pending') NOT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
  PRIMARY KEY (`id`),
  UNIQUE KEY (`title`),
  FOREIGN KEY (`director_id`) REFERENCES `tbl_directors` (`id`)
) ;
我想在where子句是动态的情况下实现以下类型的查询

  td.`name`,
  tm.`title` 
FROM
  `tbl_directors` td 
  LEFT JOIN `tbl_movies` tm 
    ON td.`id` = tm.`director_id` 
WHERE tm.`title` LIKE '%yo%' ;
我想在spring boot中使用Criteria building选项实现这个功能。
在继续编写代码的同时,我将为这个问题添加代码,同时,我将非常感谢您对此提供的任何帮助。

您可以使用criteria builder来实现这样的查询


//定义查询
CriteriaBuilder cb=entityManager.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(ResultDTO.class);
//从子句定义
Root=cq.from(DirectorEntity.class);
Join ptSku=root.Join(“电影”,JoinType.LEFT);
列表谓词=新的ArrayList();
if(name!=null){
add(cb.like(root.get(“name”)、name));
}
如果(标题!=null){
add(cb.like(root.get(“title”),title));
}
cq.where(cb.and(predicates.toArray(新谓词[predicates.size()]));
cq.distinct(真);
选择(cb.construct(ResultDTO.class、root.get(“name”)、root.get(“title”));
TypedQuery query=entityManager.createQuery(cq).setHint(QueryHints.HINT\u只读,true);
//执行查询
返回query.getResultList();
公共类ResultTo{
私有字符串名称;
私有字符串标题;
public ResultDTO(字符串名称、字符串标题){
.....
}
//吸气剂设定器
}

您可以使用criteria builder实现如下查询


//定义查询
CriteriaBuilder cb=entityManager.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(ResultDTO.class);
//从子句定义
Root=cq.from(DirectorEntity.class);
Join ptSku=root.Join(“电影”,JoinType.LEFT);
列表谓词=新的ArrayList();
if(name!=null){
add(cb.like(root.get(“name”)、name));
}
如果(标题!=null){
add(cb.like(root.get(“title”),title));
}
cq.where(cb.and(predicates.toArray(新谓词[predicates.size()]));
cq.distinct(真);
选择(cb.construct(ResultDTO.class、root.get(“name”)、root.get(“title”));
TypedQuery query=entityManager.createQuery(cq).setHint(QueryHints.HINT\u只读,true);
//执行查询
返回query.getResultList();
公共类ResultTo{
私有字符串名称;
私有字符串标题;
public ResultDTO(字符串名称、字符串标题){
.....
}
//吸气剂设定器
}

这是否回答了您的问题?在为所展示的用例提供的3个选项中,我将通过示例使用查询。这实际上涉及到连接。您给出的建议包含一张表,我已决定这是否回答了您的问题?在为所展示的用例提供的3个选项中,我将通过示例使用查询。这实际上涉及到连接。您给出的建议包含一个表,我已决定是否有任何方法可以不使用ResultTo来完成此操作?通过使用内部类?是的,内部类必须是公共的,并为该类中的名称和标题字段创建构造函数,并在ResultTo类的位置使用该类。有没有办法不使用ResultTo来实现这一点?通过使用内部类?是的,内部类必须是公共的,并为该类中的名称和标题字段创建构造函数,并使用该类代替ResultTo类。
SELECT 
  td.`name`,
  tm.`title` 
FROM
  `tbl_directors` td 
  LEFT JOIN `tbl_movies` tm 
    ON td.`id` = tm.`director_id` 
WHERE td.`name` LIKE '%AB%' ;