MybatisPlus 连表查询 Wrapper的使用

有很多时候我们写业务的时候都会有自己写sql的情况,本文说的就是MybatisPlus 自己写sql怎么用
wrapper 网上也有其他方法 ,大家也可以查考我这个 就不要吐槽有拼音什么的了,也不是我能决定的

Mapper 层

public interface T3GdGrQueryMapper extends BaseMapper<T3GdGrQueryVO> {

    @Select("SELECT t3_gd_gr.MYID,t3_gd_gr.ID,XM,SFZH,CSRQ,CJDW,CJDWLX,JCSJ,GDMC FROM t3_gd_gr inner join t3_gd on t3_gd_gr.SSGD=t3_gd.ID ${ew.customSqlSegment}")
    IPage<T3GdGrQueryVO> getPage(IPage<T3GdGrQueryVO> page,@Param(Constants.WRAPPER) QueryWrapper<T3GdGrQueryVO> wrapper);
}

可以通过加${ew.customSqlSegment}来传wrapper 表示where 后面的条件

VO

@Data
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)
public class T3GdGrQueryVO extends T3GdGr implements Serializable {
    @TableField("GDMC")
    private String GDMC;


}

接口

IPage<T3GdGrQueryVO> getPage(JSONObject json);

service层

@Service
public class GdGrServiceImpl extends ServiceImpl<T3GdGrMapper,T3GdGr> implements IGdGrService { 

   @Autowired
    private T3GdGrQueryMapper t3GdGrQueryMapper;
    
	@Override
    public IPage<T3GdGrQueryVO> getPage(JSONObject json) {
        int pageIndex = Integer.parseInt(json.getString("PageIndex")) ;
        int pageSize  = Integer.parseInt(json.getString("PageSize"));
        String id   = json.getString("ID");
        String xm = json.getString("XM");
        String sfzh = json.getString("SFZH");
        String gdmc = json.getString("GDMC");
        System.out.println("姓名:"+xm);
        System.out.println("身份证号:"+sfzh);
        System.out.println("所属工地:"+gdmc);

        QueryWrapper<T3GdGrQueryVO> wrapper = new QueryWrapper<T3GdGrQueryVO>();
        wrapper.like(!StringUtil.isNullOrEmpty(id),"ID",id);
        wrapper.eq(!StringUtil.isNullOrEmpty(xm),"XM",xm);
        wrapper.eq(!StringUtil.isNullOrEmpty(sfzh),"SFZH",sfzh);
        wrapper.eq(!StringUtil.isNullOrEmpty(gdmc),"GDMC",gdmc);


        IPage<T3GdGrQueryVO> page = new Page<>(pageIndex,pageSize);
        t3GdGrQueryMapper.getPage(page,wrapper);
        return page;
    }
}

Controller

@RestController
@RequestMapping("/api/v2/GdGr")
public class GdGrController{
	@Autowired
    private IGdGrService gdGrService;

	/**
     * 查询分页
     * @param json
     * @return
     */
    @PostMapping("Page")
    public ApiResult getByPage(@RequestBody JSONObject json)
    {
        ApiResult result = new ApiResult();
        try
        {
            IPage<T3GdGrQueryVO> page = this.gdGrService.getPage(json);

            result.setData(page.getRecords());
            result.setRowCount(page.getTotal());
            result.setPageCount(page.getPages());
        }
        catch (Exception ex)
        {
            result.setCode(ApiConstant.QUERY_ERROR_CODE);
            result.setMessage(ex.getMessage());
        }
        return result;
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐