mybatis sql mapper 用map的写法

CHAR_LENGTH(str)
返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。对于一个包含五个二字节字符集, LENGTH()返回值为 10,而CHAR_LENGTH()的返回值为5。
CHARACTER_LENGTH(str)
CHARACTER_LENGTH()是CHAR_LENGTH()的同义词。
BIT_LENGTH(str)
返回2进制长度.
简单的总结来说,mysql中获取字符串长度的有两个函数:
length:返回字符串所占的字节数,是计算字段的长度一个汉字是算三个字符,一个数字或字母算一个字符
char_length:返回字符串所占的字符数,不管汉字还是数字或者是字母都算是一个字符

有时候写mapper的时候用到map类型的传参,写法如下

// 获取方法查询入参
        public Map<String, Object> getCommonParams(CaseRecordsInfoModel caseRecordsInfoModel, Pageable pageable) {
            Map<String, Object> params = new HashMap<String, Object>();
            if (caseRecordsInfoModel.getRecord() != null) {
                if (StringUtils.isNotBlank(caseRecordsInfoModel.getRecord().getUserId())) {
                    params.put("userId", caseRecordsInfoModel.getRecord().getUserId());
                }
            if (pageable != null) {
                params.put("offset", pageable.getOffset());
                params.put("pageSize", pageable.getPageSize());
            }
            return params;
        }

service 层

    @Override
    public int selectPageByParamsCount(Map<String, Object> params, Pageable pageable) {
        return caseRecordsInfoRepo.selectPageByParamsCount(params);
    }

mapper层

    <select id="selectPageByParams" resultMap="BaseResultMap" parameterType="Map">
        select
        <include refid="Base_Column_Lists" />
        from case_records_info a , case_records record  where record.if_out=0
        <if test="_parameter.containsKey('userId')" >
            and record.user_id not in
            <foreach item="item" index="index" collection="items"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="_parameter.containsKey('ifAboveM7') and ifAboveM7 =='false'.toString()" >
            and record.collection_type in 
            <foreach item="item" index="index" collection="types"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="_parameter.containsKey('ifAboveM7') and ifAboveM7=='true'.toString()" >
            and (record.collection_type > 'M7' or LENGTH(record.collection_type)>2 or record.collection_type in 
            <foreach item="item" index="index" collection="types"
                     open="(" separator="," close=")">
                #{item}
            </foreach>)
        </if>
        <if test="_parameter.containsKey('sendAreaName') and ifNoAdr=='true'.toString()">
           and a.send_area_name = ''
        </if>
         <if test="_parameter.containsKey('sendAreaName') and ifNoAdr=='false'.toString()">
           and a.send_area_name like CONCAT(#{sendAreaName},'%' ) 
        </if>
        and a.case_id=record.id and a.is_deleted=0 and
        record.is_deleted=0  ORDER  BY record.user_id ASC
        <if  test="_parameter.containsKey('offset')">
            limit #{offset},#{pageSize}
        </if>
    </select>

mapper 还有多条件判断
比如<if test="_parameter.containsKey('sendAreaName') and ifNoAdr=='false'.toString()">
这里就是判断某个变量字符串是不是“true”再进行sql语句的编写。

总体mapper 使用map参数的用法就讲到这。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值