jpa原生拼接示例

 private final static Logger LOG = LoggerFactory.getLogger(ContractorNonMotorApplyDao.class);
    private static final String SQL = "SELECT t.ID," +
            "       t.CREATE_TIME," +
            "       t.ORDER_NUMBER," +
            "       t.REMARK," +
            "       t.STAFF_ID," +
            "       b.CNAME STAFFNAME," +
            "       t.STATE_ID," +
            "       t.SUBMIT_TIME," +
            "       b.PHONE STAFFPHONE" +
            "       FROM xxx t  " +
            "       LEFT JOIN xxxx b ON t.STAFF_ID = b.ID";

    public ContractorNonMotorApplyDao(DataSource dataSource) {
        super(dataSource);
    }

    public PageImpl<ContractorNonMotorApplyDto> findPage(ContractorNonMotorApplyFindDto findDto) {
        return new PageImplBuilder<>(ContractorNonMotorApplyDto::ofSqlResult, findDto, SQL, this::fullWhere)
                .baseWhere(" WHERE (t.DELETE_MARK = 0) ")
                .order(" ORDER BY t.CREATE_TIME DESC ")
                .buildPage();

    }

    private void fullWhere(ContractorNonMotorApplyFindDto findDto, StringBuilder stringBuilder, Map<String, Object> stringObjectMap) {
        new ContractorNonMotorApplyFindSqlCommandPackage(findDto, stringBuilder, stringObjectMap).commandPackage();
    }

    public ContractorNonMotorApplyDto findById(String id, Boolean deleteMark) {
        String sql = SQL + " WHERE(t.ID = :id) ";
        if (!deleteMark) {
            sql = sql + " AND (t.DELETE_MARK = '0') ";
        }
        Map<String, Object> parameterMap = new HashMap<>(1);
        parameterMap.put("id", id);
        return super.getFirstResult(ContractorNonMotorApplyDto::ofSqlResult, sql, parameterMap).orElseThrow(() -> new LhyException("未找到该数据"));
    }

    static class ContractorNonMotorApplyFindSqlCommandPackage extends SqlCommandPackage<ContractorNonMotorApplyFindDto> {

        /**
         * SQL原生封装 构建函数
         *
         * @param findDto      查询条件
         * @param sqlBuilder   sqlBuilder
         * @param parameterMap 参数
         * @author LiuHuiYu
         * Created DateTime 2022-11-20 8:27
         */
        public ContractorNonMotorApplyFindSqlCommandPackage(ContractorNonMotorApplyFindDto findDto, StringBuilder sqlBuilder, Map<String, Object> parameterMap) {
            super(findDto, sqlBuilder, parameterMap);
        }

        @Override
        public void commandPackage() {
            IfRun.create()
                    .elseIf(this.findDto.getOperating().isModel(ContractorNonMotorApplyFindDto.FIND_MODEL_01), this::none)
                    .elseIf(this.findDto.getOperating().isModel(ContractorNonMotorApplyFindDto.FIND_MODEL_02), this::all)
                    .elseIf(this.findDto.getOperating().isModel(ContractorNonMotorApplyFindDto.FIND_MODEL_03), this::model3)
                    .orElseThrow(() -> new LhyException("模式未设定。"));
        }

        private void model3() {
            if (StringUtils.hasText(this.findDto.getStaffName())) {
                super.likeValue("b.CNAME", "staffName", this.findDto.getStaffName());
            }
            if (StringUtils.hasText(this.findDto.getApplicationNumber())) {
                super.likeValue("t.ORDER_NUMBER", "orderNumber", this.findDto.getApplicationNumber());
            }
            if (StringUtils.hasText(this.findDto.getContractorName())) {
                this.sqlBuilder.append("and(t.ID in(select t_list.APPLY_ORDER_NUMBER from ZNAF_CONTR_NONMOTOR_LIST t_list where (DELETE_MARK=0)and(t_list.CONTRACTOR_PERSONNEL_NAME like :personnelName)))");
                this.parameterMap.put("personnelName", this.findDto.getContractorName());
            }
            super.inPackage( "STATE", "t.STATE_ID",this.findDto.getStateIdList().toArray(new Integer[0]));
        }

        private void none() {
            if (StringUtils.hasText(findDto.getStaffName())) {
                this.sqlBuilder.append(" AND b.CNAME LIKE :staffName ");
                this.parameterMap.put("staffName", "%" + findDto.getStaffName() + "%");
            }
            if (StringUtils.hasText(findDto.getStaffId())) {
                this.sqlBuilder.append(" AND t.CREATOR_ID = :operatorId");
                this.parameterMap.put("operatorId", findDto.getStaffId());
            }
        }

        public void all() {

        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值