Springboot使用QueryDsl实现融合数据查询

Springboot+QueryDsl技术

1、添加依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-jpaartifactId>
dependency>

<dependency>
    <groupId>com.querydslgroupId>
    <artifactId>querydsl-aptartifactId>
    <scope>providedscope>
dependency>

<dependency>
    <groupId>com.querydslgroupId>
    <artifactId>querydsl-jpaartifactId>
dependency>
2、添加配置
@Configuration
public class PeopleQueryConfig {
  @Bean
  public JPAQueryFactory jpaQuery(EntityManager entityManager) {
    return new JPAQueryFactory(entityManager);
  }
}
3、添加实体类
@Data
@Entity
@Table(name = "t_msg_log")
public class MsgLog extends PkUUID implements Serializable {
 
  private static final long serialVersionUID = -1479947068453421077L;
 
  @Column(name = "mobile", length = 16)
  private String mobile;
 
  @Column(name = "openid", length = 64)
  private String openid;
 
  @Column(name = "tsrname", length = 64)
  private String tsrname;
 
  @Column(name = "msg_direction", length = 2)
  @Convert(converter = MsgDirectionConverter.class)
  private MsgDirection msgDirection;
 
  @Column(name = "msg_type", length = 16)
  @Enumerated(EnumType.STRING)
  private MsgType msgType;
 
  @Column(name = "msg_channel", length = 2)
  @Convert(converter = MsgChannelConverter.class)
  private MsgChannel msgChannel;
 
  @Lob
  @Column(name = "msg_content")
  private String msgContent;
 
  @Column(name = "is_passive")
  private Boolean isPassive;
 
  @Column(name = "status", length = 2)
  @Convert(converter = MsgStatusConverter.class)
  private MsgStatus status;
 
  @Column(name = "send_time")
  private Date sendTime;
 
  @Column(name = "schedule_flag", length = 2)
  @Convert(converter = ScheduleFlagConverter.class)
  private ScheduleFlag scheduleFlag;
 
  @Column(name = "src_msg_id")
  private String srcMsgId;
 
  /**
   * enum
   */
  public enum MsgType {
    text, image, video, voice, news, link, template, location
  }
 
  @RequiredArgsConstructor
  public enum MsgDirection implements ValuedEnum<String> {
    User2Agent("0"), Agent2User("1");
    @Getter
    private final String value;
  }
 
  @RequiredArgsConstructor
  public enum MsgChannel implements ValuedEnum<String> {
    SMS("0"), WECHAT("1");
    @Getter
    private final String value;
 
  }
 
  @RequiredArgsConstructor
  public enum MsgStatus implements ValuedEnum<String> {
    Success("1"), Failure("-1");
    @Getter
    private final String value;
  }
 
  @RequiredArgsConstructor
  public enum ScheduleFlag implements ValuedEnum<String> {
    ScheduledMsg("0"), InstantMsg("1");
    @Getter
    private final String value;
  }
 
 
}

4、JPA-DAO类

DAO 类继承QueryDslPredicateExecutor

@Repository
public interface MsgLogRepository extends JpaRepository<MsgLog,String>,QueryDslPredicateExecutor<MsgLog>{
 
}
5、Controller
@RestController
@RequestMapping("/msgLog/")
public class MsgLogController {
  private static final Logger LOGGER = LoggerFactory.getLogger(MsgLogController.class);
 
  @Autowired
  private MsgLogRepository msgLogRepository;
 
  /**
   * 根据条件查询回话内容
   * @param predicate
   * @return
   */
  @RequestMapping(value = "list")
  public List<MsgLogResp> list(@QuerydslPredicate(root = MsgLog.class) Predicate predicate){
    Iterable<MsgLog> msgLogs = msgLogRepository.findAll(predicate);
    LOGGER.info("msgLogs = "+ JsonUtil.toJson(msgLogs));
 
    List<MsgLog> msgLogList = new ArrayList<>();
    msgLogs.forEach((MsgLog msgLog)-> msgLogList.add(msgLog));
    return msgLogList.stream().map(new Function<MsgLog, MsgLogResp>() {
      @Nullable
      @Override
      public MsgLogResp apply(@Nullable MsgLog msgLog) {
        return new MsgLogResp(msgLog);
      }
    }).collect(Collectors.toList());
  }
 
}

你可能感兴趣的:(spring,boot,java,后端)