欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

map入参mybatis,并用List返回

程序员文章站 2022-06-30 19:54:59
...

废话不多说上代码:【注意几点,mapper接口中我们需要使用@Param(“params”)注解,在sql中使用 params.map的key即可】

Mapper接口 和serviceImpl上 别忘记加注解 @Mapper @Service 我得是springboot框架

实体类

 

package com.bfc.boperation.model.po;

import java.util.Date;

public class Advertise {
    private Long advertiseId;

    private String mediaUrl;

    private Date putStartTime;

    private Date putEndTime;

    private String desc;

    private Long createOperationId;

    private Integer status;

    private Date createDate;

    ..........省区get set
}

然后是serviceImpl层

  @Override
    public ResponseData findAll(SearchParam param) {
        ResponseData res = new ResponseData();
        
        //这个map就是sql条件
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageNo", param.getStart());//0
        map.put("pageSize", param.getStart() + param.getLength());//2
        map.put("startTime", param.getParam().get("put_start_time"));
        map.put("endTime", param.getParam().get("put_end_time"));

        //这个是mapper查询的值
        List<Map<String, Object>> list = advertiseMapper.selectBlackAdvertiseList(map);

        //这个是需要返回的list  Res顾名思义就是response的缩写
        List<ResAdvertise> list1 = new ArrayList<>();

        //for循环遍历 查询出来的list get(“key”) 这个key  是我在sql中 as 后的别名  不写便是数据库中的字段名
        for (Map<String, Object> advertise : list) {
            ResAdvertise advertise1 = new ResAdvertise();
            advertise1.setAdvertiseId(Long.valueOf(advertise.get("advertiseId").toString()));
            advertise1.setCreateDate((Date) advertise.get("createDate"));
//            advertise1.setCreateOperationId(Long.valueOf(advertise.get("create_operation_id").toString()));
            advertise1.setNickName(advertise.get("nickName").toString());
            advertise1.setDesc(advertise.get("desc").toString());
            advertise1.setMediaUrl(advertise.get("mediaUrl").toString());
            advertise1.setPutEndTime((Date) advertise.get("putEndTime"));
            advertise1.setPutStartTime((Date) advertise.get("putStartTime"));
            advertise1.setStatus(Integer.parseInt(advertise.get("status").toString()));
            list1.add(advertise1);
        }
        res.setData(list);
        res.setDraw(param.getDraw());
        return res;
    }

然后是mapper

    /**
     * 后台:查询广告
     * @param map
     * @return
     */
    List<Map<String,Object>> selectBlackAdvertiseList(@Param("params") Map<String, Object> map);

然后就是sql语句了  使用@Param中的params   .   在 serviceImpl中   定义好的map条件 中的值(key)    即可

<!--后台:查询广告-->
    <select id="selectBlackAdvertiseList" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
        ad.advertise_id AS advertiseId,
        ad.create_date AS createDate,
        ad.create_operation_id AS createOperationId,
        ad.desc,
        ad.media_url AS mediaUrl,
        ad.put_end_time AS putEndTime,
        ad.put_start_time AS putStartTime,
        ad.status,
        ur.nick_name AS nickName FROM bfc_advertise ad LEFT JOIN bfc_common_user ur ON ur.`user_id` =
        ad.`create_operation_id`
        <where>
            1 = 1
            <if test="params.startTime != null and params.startTime != ''">
                and ad.put_start_time LIKE CONCAT('%',#{params.startTime},'%')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and ad.put_end_time LIKE CONCAT('%',#{params.endTime},'%')
            </if>
            LIMIT #{params.pageNo},#{params.pageSize}
        </where>
    </select>