Springboot|QueryDSL给Spring JPA添上了翅膀,那我再给它加个鸡腿,拿掉VO

目录
第一步,配置pom.xml文件
第二步:在主启动文件中加入以下代码 ,让Spring来管理JPAQueryFactory
第三步:参考上一篇文章写的案例进行
第四步:在控制器层调用
第五步:运行结果输出http://127.0.0.1:9011/search
QueryDSL给Spring JPA添上了翅膀,那我再给它加个鸡腿,拿掉VO
吐槽:JAVA SQL查询和PHP SQL查询比,简直一个是木头人。两者灵活性不能比较,当然PHP是脚本语言自然灵活。现在我们要解决的是怎么把一个木头人,变的灵活。 编程也要学会偷懒,有一种“懒” 也是开发效率的提升!
下面我们结合“QueryDSL给Spring JPA添上了翅膀” ,再加个鸡腿,拿掉VO!
第一步,配置pom.xml文件

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【Springboot|QueryDSL给Spring JPA添上了翅膀,那我再给它加个鸡腿,拿掉VO】com.querydsl.apt.jpa.JPAAnnotationProcessor

第二步:在主启动文件中加入以下代码 ,让Spring来管理JPAQueryFactory
//让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 dynamicQuery(Predicate predicate, Pageable pageable) { QUser u = QUser.user; //需要 maven compile 自动生成 前缀 Q + 实体类 QueryResults queryResults = queryFactory.select(u.id, u.username,u.status).from(u) .where(predicate).offset(pageable.getOffset()).limit(pageable.getPageSize()) .orderBy(u.id.desc()).fetchResults(); if (queryResults == null || queryResults.isEmpty() ){ System.out.println("查询空!!"); returnnull; } List tuples = queryResults.getResults(); List data = https://www.it610.com/article/Lists.newArrayList(); //根据字段生成动态vo类 Map field = new HashMap() {{ put(u.id.getMetadata().getName(), u.id); put(u.username.getMetadata().getName(), u.username); put(u.status.getMetadata().getName(), u.status); }}; for (Tuple tu : tuples) { DynamicBeanUtil vo = new DynamicBeanUtil(DynamicBeanUtil.referObjectType(field)); for (String fieldName : field.keySet()) { vo.setValue(fieldName, tu.get(field.get(fieldName))); } data.add(vo.getObject()); } long total = queryResults.getTotal(); return new PageImpl<>(data, pageable, total); }@Autowired UserRepository userRepository; public User saveUser(User data) { User save = null; Optional selectData = https://www.it610.com/article/userRepository.findById(data.getId()); if (selectData.isPresent()) { String[] Field = {"nick", "theme"}; //更新 Field指定允许字段 JpaUtil.copyNotNullPropertiesAllow(data, selectData.get(), Field); save = userRepository.save(selectData.get()); } else { userRepository.save(data); //新增 } return save; }}
第四步:在控制器层调用
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 = https://www.it610.com/article/new User(); data.setId(1); data.setNick("只更新,允许的字段"); userService.saveUser(data); }@GetMapping(value = "https://www.it610.com/article/search", name = "查询 分页,总数") public void search(@RequestParam(value = "https://www.it610.com/article/id", required = false) Long id, @RequestParam(value = "https://www.it610.com/article/page", required = false, defaultValue = "https://www.it610.com/article/2") int page, @RequestParam(value = "https://www.it610.com/article/limit", required = false, defaultValue = "https://www.it610.com/article/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 = https://www.it610.com/article/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)); } }
第五步:运行结果输出http://127.0.0.1:9011/search
Springboot|QueryDSL给Spring JPA添上了翅膀,那我再给它加个鸡腿,拿掉VO
文章图片

    推荐阅读