目录
第一步,配置pom.xml文件
第二步:在主启动文件中加入以下代码 ,让Spring来管理JPAQueryFactory
第三步:参考上一篇文章写的案例进行
第四步:在控制器层调用
第五步:运行结果输出 http://127.0.0.1:9011/search
QueryDSL给Spring JPA添上了翅膀,那我再给它加个鸡腿,拿掉VO
吐槽:JAVA SQL查询和PHP SQL查询比,简直一个是木头人。两者灵活性不能比较,当然PHP是脚本语言自然灵活。现在我们要解决的是怎么把一个木头人,变的灵活。 编程也要学会偷懒,有一种“懒” 也是开发效率的提升!
下面我们结合“QueryDSL给Spring JPA添上了翅膀” ,再加个鸡腿,拿掉VO!
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.weijuhe
wjh
0.0.1
jar
demo
Demo project for Spring Boot
1.8
com.alibaba
fastjson
1.2.28
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
1.16.20
provided
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.querydsl
querydsl-apt
provided
com.querydsl
querydsl-jpa
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-maven-plugin
com.mysema.maven
apt-maven-plugin
1.1.3
process
target/generated-sources/java
com.querydsl.apt.jpa.JPAAnnotationProcessor
//让Spring管理JPAQueryFactory
@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
修改 UserService 文件 ,主要看:dynamicQuery
https://blog.csdn.net/qq_15371293/article/details/107025611
package com.example.demo.service;
import com.example.demo.entity.QUser;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.util.DynamicBeanUtil;
import com.example.demo.util.JpaUtil;
import com.google.common.collect.Lists;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Service
public class UserService extends BaseService {
@Autowired
JPAQueryFactory queryFactory;
//分页查询
public Page
package com.example.demo.controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.entity.QUser;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
@RestController
@Controller
@RequestMapping("/")
public class IndexController {
@Autowired
UserService userService;
@GetMapping("index")
public void index() {
User data = new User();
data.setId(1);
data.setNick("只更新,允许的字段");
userService.saveUser(data);
}
@GetMapping(value = "search", name = "查询 分页,总数")
public void search(@RequestParam(value = "id", required = false) Long id,
@RequestParam(value = "page", required = false, defaultValue = "2") int page,
@RequestParam(value = "limit", required = false, defaultValue = "5") int limit
) {
QUser user = QUser.user; //需要 maven compile 自动生成 前缀 Q + 实体类
//初始化组装条件(类似where 1=1)
Predicate predicate = user.isNotNull().or(user.isNull());
predicate = ExpressionUtils.and(predicate,user.mobile.ne("1111"));
predicate = ExpressionUtils.and(predicate,user.id.ne(1));
Pageable pageable = PageRequest.of(page, limit);
Page data = userService.dynamicQuery(predicate, pageable);
Map ret = new HashMap();
ret.put("数据", data.getContent());
ret.put("总共", data.getTotalElements()+"条");
ret.put("第", data.getNumber()+"页");
ret.put("每页显示", data.getNumberOfElements()+"条");
ret.put("总共", data.getTotalPages()+"页");
System.out.println("输出查询数据,拿掉VO ※ \n" + JSON.toJSON(ret));
}
}