mybatis日常笔记

前端传入时间区间查数据范围

Bean中:

 @ApiModelProperty("条件查询开始时间")
    private Date startTime;

    @ApiModelProperty("条件查询结束时间")
    private Date endTime;

Mybatis中:

  <if test="startTime != null and endTime !=null ">
            and  result.report_date between #{startTime} and  #{endTime}
        </if>
ps:between  左闭右开 >=#{startTime} and <#{endTime}
Map<String,Object> XX(@Param("params") Map<String, Object> parMap);

<select id="XX" parameterType="map" resultType="map">
        select *,'${params.aa}' as re1 from tablet
        <trim prefix="WHERE" prefixOverrides="AND|OR">
            <if test="params.bb != null and params.bb != ''">
                字段=#{params.bb}
            </if>
        </trim>
    </select>
<select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
    select * from t_student 
     <trim prefix="where" prefixOverrides="and|or">
         <if test="gradeId!=null">
            gradeId=#{gradeId} //  and  gradeId=#{gradeId}
         </if>
         <if test="name!=null">
            and name like #{name}
         </if>
         <if test="age!=nulll">
            and age=#{age}
         </if>
     </trim>
</select>

mybatis使用or and ()

<if test="pd.flag==1 or ((pd.flag==2 or pd.flag==3) and (pd.sfyj==2 or pd.sfyj==3)) or pd.flag==3">
    union all
  </if>

Mybatis中 if标签判断字符串

mybatis中判断字符串为某一个值,必须+toString() 或者 flage==“xxx” (双引号),否则无法进入该判断
Mybatis的OGNL语法, 会把 ‘Y’ 或 ‘N’ 认为成char类型

引发异常:Cause: java.lang.NumberFormatException: For input string: “N”\r\n### Cause

1:将代码改为test="param eq 'Y'.toString()"

2:将代码改为test="name == &quot;Y&quot;"

3:将代码改为test='index == "Y" '

第三种是最简单的方法,被双引号修饰的都会被看成String类型



Mybatis中 if标签判断bool

 <choose>
            <when test="is2thProcess">
                AND detail.SENCOND_TITLE = 'Y'
            </when>
            <otherwise>
                AND detail.SENCOND_TITLE IS NULL
            </otherwise>
        </choose>
1、
<if test="boolvalue">
//boolvalue=true 时
</if>
2、
<if test="boolvalue==true">
//boolvalue=true 时
</if>

Weekend写法

//Weekend写法,其实这个是类似上面的Example的
        Weekend<ResultDO> weekend = new Weekend<>(ResultDO.class);
        //这个类似上面的Example.Criteria
        WeekendCriteria<ResultDO, Object> weekendCriteria = weekend.weekendCriteria();
        //这样写比上面更简洁,最重要的是,上面是字符串形式,
        //当实体类改名,上面的写法不易查找错误,下面的就会立即报错,很快就可以定位要修改的点
        weekendCriteria.andEqualTo(ResultDO::getArticleId, companyNewsVO.getArticleId())
                .andEqualTo(ResultDO::getValid, 0);
        int count = resultMapper.selectCountByExample(weekend);

标题通用mapper插入基本类型

要用封装类

https://blog.csdn.net/qq_25933841/article/details/105861569

mybatis 没有引号_mybatis 中生成的字段不带引号 #{}和${}

1) 使用#{参数}传入会加上单引号,sql语句解析是会加上""

比如 select * from table where name = #{name} ,传入的name为小李,那么最后打印出来的就是

select * from table where name = ‘小李’,就是会当成字符串来解析

#{}传参能防止sql注入,如果你传入的参数为 单引号’,那么如果使用${},这种方式 那么是会报错的

2)${}

另外一种场景是,如果你要做动态的排序,比如 order by column,这个时候务必要用${},

mysql中大于号小于号的写法

<	<	<![CDATA[ < ]]>
<=	<=	<![CDATA[ <= ]]>
>	>	<![CDATA[ > ]]>
>=	>=	<![CDATA[ >= ]]>
&	&	
’	'	
"	"	

mybatis select X from Table where id in (xx,ss) 参数超过1000个处理

    <select id="selectToConvertWord" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM converted_pdf t
        WHERE convert_status ='0'
        and t.rela_id in
        <foreach collection="relaIdSet" item="relaId" index="index" open="(" close=")">
            <if test="index != 0">
                <choose>
                    <when test="index % 1000 == 999">) OR t.rela_id in (</when>
                    <otherwise>,</otherwise>
                </choose>
            </if>
            #{relaId,jdbcType=VARCHAR}
        </foreach>
    </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值