使用Mybatis-Plus多表查询,还带有queryWapper条件的查找方法
需要多表查询,还要带有Mybatis-Plus的queryWapper条件查询功能(很香)
~以写过的业务为例
Controller代码如下:
/**
* 分页列表查询
*
* @param cesToolException
* @param pageNo
* @param pageSize
* @param req
* @return
*/
@AutoLog(value = "工装异常提报表-分页列表查询")
@ApiOperation(value="工装异常提报表-分页列表查询", notes="工装异常提报表-分页列表查询")
@GetMapping(value = "/list")
public Result<?> queryPageList(CesToolException cesToolException,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
Map<String, String[]> map = req.getParameterMap();
String[] exceptionBecauses = map.get("exceptionBecause");
Map<String, String[]> map1 = new HashMap<>();
map1.putAll(map);
if (exceptionBecauses != null) {
if ("0".equals(exceptionBecauses[0])) {
cesToolException.setExceptionBecause(null);
map1.remove("exceptionBecause");
}
}
QueryWrapper<CesToolException> queryWrapper = QueryGenerator.initQueryWrapper(cesToolException, map1);
Page<CesToolException> page = new Page<CesToolException>(pageNo, pageSize);
if (exceptionBecauses != null) {
if ("0".equals(exceptionBecauses[0])) {
queryWrapper.notIn("exception_because","1","2","3");
}
}
IPage<CesToolException> pageList = cesToolExceptionService.getCesToolExceptionList(page, queryWrapper);
return Result.ok(pageList);
}
Service类
/**
* @Description: 工装异常提报表
* @Author: jeecg-boot
* @Date: 2022-07-18
* @Version: V1.0
*/
public interface ICesToolExceptionService extends IService<CesToolException> {
IPage<CesToolException> getCesToolExceptionList(Page<CesToolException> page, QueryWrapper<CesToolException> queryWrapper);
}
ServiceImpl类
/**
* @Description: 工装异常提报表
* @Author: jeecg-boot
* @Date: 2022-07-18
* @Version: V1.0
*/
@Service
public class CesToolExceptionServiceImpl extends ServiceImpl<CesToolExceptionMapper, CesToolException> implements ICesToolExceptionService {
@Override
public IPage<CesToolException> getCesToolExceptionList(Page<CesToolException> page, QueryWrapper<CesToolException> queryWrapper) {
return baseMapper.findByPage(page, queryWrapper);
}
}
此处的baseMapper是不需要注入的,直接在ServiceImpl类调用就可以了
baseMapper代表的是Mybatis-plus对应的Mapper接口类,此处对应的是CesToolExceptionMapper.java
本来应该是没有这个getCesToolExceptionList方法的,是为了自定义多表查询才写的
CesToolExceptionMapper类
/**
* @Description: 工装异常提报表
* @Author: jeecg-boot
* @Date: 2022-07-18
* @Version: V1.0
*/
public interface CesToolExceptionMapper extends BaseMapper<CesToolException> {
//参数加上@Param(Constants.WRAPPER),xml里加上${ew.customSqlSegment}可以实现复杂条件检索查询
IPage<CesToolException> findByPage(Page<CesToolException> page,@Param(Constants.WRAPPER) QueryWrapper<CesToolException> queryWrapper);
}
CesToolExceptionMapper.xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jeecg.modules.quality.mapper.CesToolExceptionMapper">
<select id="findByPage" resultType="org.jeecg.modules.quality.entity.CesToolException">
select *
from (SELECT ces_tool_exception.*,
sys_user.realname,
qa_tooling_system.system_name as toolName
FROM ces_tool_exception
LEFT JOIN sys_user ON sys_user.username COLLATE Chinese_PRC_CI_AS = ces_tool_exception.create_by
LEFT JOIN qa_tooling_system ON qa_tooling_system.id COLLATE Chinese_PRC_CI_AS = ces_tool_exception.system_id)
AS t ${ew.customSqlSegment}
select>
mapper>
大佬写的比较多,比较恶心,可以看一下另外一篇文章Mybatis-Plus多表联合查询随手记2
通过上面的写法,可以实现queryWrapper条件查询+slq多表查询结合,超香
大致思路:
就是mybatis-plus正常的queryWrapper条件查询,然后调用自己定义的Mapper接口,映射对应的XML文件,在XML文件就可以写对应查找的sql语句,然后有个框架
<!--mybatis-plus多表联合查询,带上${ew.customSqlSegment就可以实现查询-->
<select id="findByPage" resultType="org.jeecg.modules.quality.vo.QaProcessInstanceVO">
select * from (
SELECT
qa_process_instance.*, qa_process.process_name,
qa_process.process_code,
qa_process.process_type,
c.process_node_name AS cur_node_name,
n.process_node_name AS next_node_name,
h.cpk_id AS cpk_data_id,
h.cpk_name AS cpk_name,
h.reason,
h.wu_code AS wu_code,
h.group_line
FROM
qa_process_instance
LEFT JOIN qa_process ON qa_process_instance.process_id = qa_process.id
LEFT JOIN qa_process_node c ON qa_process_instance.cur_node_id = c.id
LEFT JOIN qa_process_node n ON qa_process_instance.next_node_id = n.id
INNER JOIN qa_process_instance_handle h ON h.process_instance_id = qa_process_instance.id)
AS t ${ew.customSqlSegment}
</select>
<!--mybatis-plus多表联合查询,带上${ew.customSqlSegment就可以实现查询-->
<select id="findByPage" resultType="org.jeecg.modules.quality.vo.QaProcessInstanceVO">
select * from (
SELECT 想要保留的字段 可以使用table.*,t1.name 这种形式
FROM 自己想要连接的查询语句
随便左右连接自己的表,查就对了
)
AS t ${ew.customSqlSegment}
</select>
<!--mybatis-plus多表联合查询,带上${ew.customSqlSegment就可以实现查询-->
<select id="findByPage" resultType="org.jeecg.modules.quality.vo.QaProcessInstanceVO">
select * from (
)
AS t ${ew.customSqlSegment}
这个框架就是有需要多写的框架,里面对应的**AS t ${ew.customSqlSegment}**对应接口类里面的@Param(Constants.WRAPPER)对应才能使用
这个查询可能会报错误
Cannot resolve the collation conflict between “Chinese_PRC_CI_AS” and "SQL_L及由于排序规则不同导致查询结果为空的问题
原理就是自己指定一个中文排序方式 直接加在连接字段后面就好了,就加这段COLLATE Chinese_PRC_CI_AS,根据自己的报错自己选一个中文排序方式。这是中文排序错误。
LEFT JOIN qa_tooling_system ON qa_tooling_system.id COLLATE Chinese_PRC_CI_AS = ces_tool_exception.system_id)
就是查询条件都不管,把连接的部分先查找出来,然后在最外层套上自己queryWapper对应的条件实现条件查找。
这段代码还带有分页查找功能,自己创建Page对象的方法
自己new一个Page对象,代码如下:
Page<CesToolException> page = new Page<CesToolException>(pageNo, pageSize);