美文网首页
QueryWrapper常用案例

QueryWrapper常用案例

作者: Yluozi | 来源:发表于2023-09-17 17:52 被阅读0次

    1.多条数据查询

    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    @Service("xxService")
    public class xxServiceImpl extends ServiceImpl<xxDao, xxEntity> implements xxService {
        @Override
        public List<IndexEntity> getList(Map<String, Object> params) {
            List<IndexEntity> list = this.list(
                    new QueryWrapper<IndexEntity>()
                            .eq("to_date(STATIS_DATE, 'yyyy-mm-dd')",params.get("statisDate"))
                            .lambda()
                            .eq(StringUtils.isNotBlank(params.get("Type").toString()),IndexEntity::getType,params.get("Type"))
                            .apply(  params.containsKey("insertTimeStart")," INSERT_TIME  BETWEEN  to_date('"+params.get("insertTimeStart")+"', 'yyyy-mm-dd') AND to_date('"+params.get("insertTimeEnd")+"', 'yyyy-mm-dd')")
                            .like(  params.containsKey("province"),"PROVINCE",params.get("province"))
                            .isNotNull("INDEX_ID")
                            .orderByAsc("create_time")
            );
            return list;
        }
    
    

    2.select指定字段

        @Override
        public PageUtils queryPage(Map<String, Object> params) throws ParseException {
            IPage<CUserEntity> page = this.page(
                    new Query<CUserEntity>().getPage(params),
                    new QueryWrapper<CUserEntity>()
                            .select("to_char(STATIS_DATE, 'yyyy-MM-dd HH24:mi:ss') as statisDate"," id as ","xxx as ss","xx as ss")
                            .apply(null!=params.get("statisDateStart")," STATIS_DATE  BETWEEN  to_date('"+params.get("statisDateStart")+"', 'yyyy-MM-dd HH24:mi:ss') AND to_date('"+params.get("statisDateEnd")+"', 'yyyy-MM-dd HH24:mi:ss')")
                            .orderByAsc("STATIS_DATE")
            );
            return new PageUtils(page);
        }
    

    3.为读xml文件的sql添加ipage页面

       IPage<Map<String,Object>> relationInfoPage(Map<String, Object> params);
    
    @Mapper
    public interface xxDao extends BaseMapper<xxEntity> {
        IPage<Map<String,Object>> relationInfoPage(IPage<Map<String,Object>> page , String Id);
    }
    
    
        @Override
        public IPage<Map<String, Object>> relationInfoPage(Map<String, Object> params) {
            IPage<Map<String, Object>> thIPage = new Query<Map<String, Object>>().getPage(params);
            return cxDao.relationInfoPage(thIPage,params.get("xId").toString());
        }
    

    若数据库为oralce获取的参数对象为map时回参全大写,避免大写回参可在mapper xml文件中添加默认resultMap配置,如下:

        <!-- 可根据自己的需求,是否要使用 -->
        <resultMap type="com.z.xxEntity" id="xxMap">
            <result property="xId" column="DIX_ID"/>
            <result property="sceneName" column="SCENE_NAME"/>
            <result property="sceneState" column="SCENE_STATE"/>
        </resultMap>
    
        <select id="relationInfoPage"   resultMap="xxMap" >
            <![CDATA[
                select *
                from  xxx
                where  a.DIX_ID=  #{Id}
                order by  b.create_time asc
            ]]>
        </select>
    

    4.直接拼接lambdaQueryWrapper的方法

    带/不带分页

            LambdaQueryWrapper<ResouseEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ResouseEntity::getTaskId, taskId);
            IPage<ResouseEntity> iPage = ponCutoverOnuInfoService.page(new Query<ResouseEntity>().getPage(params), lambdaQueryWrapper);
    //        List<ResouseEntity> list = ponCutoverOnuInfoService.list(lambdaQueryWrapper);
            log.info("查询业务设计结束:" + JSON.toJSONString(iPage));
            return R.ok().put("data", iPage);
    

    相关文章

      网友评论

          本文标题:QueryWrapper常用案例

          本文链接:https://www.haomeiwen.com/subject/iemrortx.html