JPA自定义VO接受返回结果集(unwrap)

JPA跟mybitis比较,简单的业务搜索是方便的,但是设计到复杂的SQL搜索时,我们需要自定义SQL。

1.@Query直接写SQL,缺点是无法动态的组装条件

2.JPA的Specification对象动态组装where搜索条件

3.entityManager执行CriteriaBuilder

4.entityManger直接使用createNativeQuery,执行原生SQL。这里设计到返回结果集的承载体必须是数据库对应的实体。

   这里说一个自定义的VO承接返回结果集的方法。

ProjectAttendanceEntity
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "f_id")
    private Long fId;

    @Column(name = "user_id")
    private Integer userId;

    @Column(name = "zh_name")
    private String zhName;

    @Column(name = "po_code")
    private String poCode;

    @Column(name = "po_name")
    private String poName;

    @Column(name = "punch_date")
    private String punchDate;

    @Column(name = "is_original")
    private String isOriginal;

    @Column(name = "attendance_hours")
    private String attendanceHours;

    @Column(name = "work_hours")
    private String workHours;

    @Column(name = "punch_area")
    private String punchArea;

结果集承接VO   (AttendancePoSzVO)

    private String poId;

    private String poName;

    private String zhName;

执行接口:

/**
     * 批量修改项目名称
     * @return
     */
    @PostMapping("/po/sz/batch/{project}/{pn}/{ps}")
    public PageResultVO findBatchPoInfoByUserIdAndDate(@RequestBody List ids,@PathVariable String project,@PathVariable Integer pn,@PathVariable Integer ps){
        log.info("url:/po/sz/batch/"+"|param:"+ids);
        //通过id查询数据
        List projects = projectAttendanceEntityRepository.findByIdIn(ids);
        //获取SQL
        String sql = getSQL(projects,pn,ps);
        Query query = entityManager.createNativeQuery(sql);
        List list = query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(AttendancePoSzVO.class)).getResultList();
        //初始化结果集
        List result = new ArrayList<>();
        for(AttendancePoSzVO poSz : list){
            result.add(new DropDownVO(poSz.getPoName(),poSz.getPoId()));
        }
        return new PageResultVO(GlobalReturnCode.SUCCESS_CODE,"SUCCESS",ps,pn,result);
    }


    /**
     * 组装查询SQL
     * @return
     */
    public String getSQL(List poStatus, Integer pn, Integer ps){
        StringBuilder sql = new StringBuilder("SELECT DISTINCT res.po_id as poId,res.po_name as poName, GROUP_CONCAT(DISTINCT res.user_id) AS zhName ");
            sql.append(" FROM (");
            sql.append(" SELECT tt.po_name,tt.po_id,tt.user_id");
            sql.append(" FROM sie_sz_po_attendance_v tt ");
            sql.append(" WHERE");
            for(ProjectAttendanceEntity po : poStatus){
                sql.append("(tt.user_id = ").append(po.getUserId()).append(" and tt.rt_begin_date <= '").append(po.getPunchDate())
                    .append("' and tt.rt_end_date >= '").append(po.getPunchDate()).append("') OR ");
            }
            //截掉最后一个OR
            sql = new StringBuilder(sql.substring(0,sql.length()-3));
            sql.append(" ) res");
            sql.append(" GROUP BY res.po_name,res.po_id");
            sql.append(" HAVING ");
            for(ProjectAttendanceEntity po : poStatus){
                sql.append(" INSTR(zhName,").append(po.getUserId()).append(") >0").append(" AND ");
            }
            //截取最后一个AND
            sql = new StringBuilder(sql.substring(0,sql.length()-4));
            sql.append(" LIMIT ").append(pn).append(",").append((pn+1)*ps);
        return sql.toString();
    }

核心代码:

List list = query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(AttendancePoSzVO.class)).getResultList();

但是这里的 setResultTransformer 已经过时了。

所以接下来寻找 setResultTransformer的替代API。

你可能感兴趣的:(Java,mysql,JPA)