java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot QueryDsl数据查询

Springboot+QueryDsl实现融合数据查询

作者:天道酬勤的博客

这篇文章主要将介绍的是 Springboot 使用 QueryDsl 实现融合数据查询,文中有详细的代码讲解,对 SpringBoot Querydsl 查询操作感兴趣的朋友一起看看吧

Springboot+QueryDsl技术

1、添加依赖

<!--基于JPA-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--QueryDSL支持-->
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <scope>provided</scope>
</dependency>
<!--QueryDSL支持-->
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
</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());
  }
}

到此这篇关于Springboot+QueryDsl实现融合数据查询的文章就介绍到这了,更多相关Springboot QueryDsl数据查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文