illustrate:
Jeecgboot uses mybatis plus, and uses the QueryWrapper object in the condition constructor to complete the query. The getCustomSqlSegment() method in this object is to obtain custom SQL: so, we use QueryWrapper to construct and obtain the custom SQL in the
mapper Define SQL to be spliced after our join table query statement.
Mybatis plus document address: mybatis plus querywrapper
Code process:
My requirement: According to the openid query to the result data that has been done, the result data needs to be obtained by joint table query:
front end [small program]:
wx.request({
url: getPayResultListByOpenId,
data: {
pageNo: this.data.page,
pageSize: this.data.limit,
openId: wx.getStorageSync('openid')
},
success: (res) => {
},
fail: (res) => {
console.log(res);
}
})
controller:
@RequestMapping(value = "getPayResultListByOpenId")
@ResponseBody
public Result<?> queryPageList(PayTestresult payTestresult,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest req) {
QueryWrapper<PayTestresult> queryWrapper = QueryGenerator.initQueryWrapper(payTestresult, req.getParameterMap());
Page<PayTestresult> page = new Page<PayTestresult>(pageNo, pageSize);
IPage<PayTestresult> pageList = payTestresultService.pageList(page, queryWrapper);
return Result.OK(pageList);
}
service,serviceimpl:
IPage<PayTestresult> pageList(Page<PayTestresult> page, QueryWrapper<PayTestresult> queryWrapper);
@Overridepublic IPage<PayTestresult> pageList(Page<PayTestresult> page, QueryWrapper<PayTestresult> queryWrapper) {
return testresultMapper.getPageList(page, queryWrapper);
}
mapper layer:
IPage<PayTestresult> getPageList(Page page, @Param(MybatisPlusConst.QUERYWRAPPER) Wrapper<PayTestresult> queryWrapper);
MybatisPlusConst is a custom constant class, and QUERYWRAPPER is a custom field
public class MybatisPlusConst {
public static final String QUERYWRAPPER = "ew";
public static final String SQL = "sql";
}
SQL statement in the mapper layer: use ${ew.customSqlSegment} to get the custom SQL statement, splicing
<select id="getPageList" resultType="org.jeecg.modules.paytest.entity.PayTestresult">
SELECT b.descp, b.que_count, b.read_num, b.pic AS picPath, a.*
FROM pay_testresult a
LEFT JOIN pay_test b ON a.test_id = b.id
${ew.customSqlSegment}
</select>
result:
The printed SQL statement is as follows: