mybatis和sql的运用总结(持续...)

以下记录和转载仅是自己在写相似需求时参考过比较有价值的文章或者自己写过后用来保存记录,以便持续复习参考

一.if-else的写法

mybaits 中没有else要用chose when otherwise 代替

范例一

<!--批量插入用户-->
<insert id="insertBusinessUserList" parameterType="java.util.List">
    insert into `business_user` (`id` , `user_type` , `user_login` )
    values
    <foreach collection="list" index="index" item="item" separator=",">
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <choose>
                <when test="item.id != null and item.id !=''">
                    #{item.id,jdbcType=CHAR},
                </when>
                <otherwise>
                    '',
                </otherwise>
            </choose>
            <choose>
                <when test="item.userType != null and item.userType !=''">
                    #{item.userType,jdbcType=VARCHAR},
                </when>
                <otherwise>
                    '',
                </otherwise>
            </choose>
        </trim>
    </foreach>
</insert>

其中choose为一个整体 
when是if 
otherwise是else

范例二:

<select id="selectSelective" resultMap="xxx" parameterType="xxx">
    select
    <include refid="Base_Column_List"/>
    from xxx
    where del_flag=0
    <choose>
        <when test="xxx !=null and xxx != ''">
            and xxx like concat(concat('%', #{xxx}), '%')
        </when>
        <otherwise>
            and xxx like '**%'
        </otherwise>
    </choose>
</select>

下面就是MyBatis中的if....else...表示方法

<choose>
    <when test="">
        //...
    </when>
    <otherwise>
        //...
    </otherwise>
</choose>

以上转自: https://www.cnblogs.com/a8457013/p/8033263.html

二.sql 查询 group by分组 后 获得每组中时间最大 记录

   SELECT SUBSTRING_INDEX(group_concat( id ORDER BY `create_time` DESC ), ',', 1 ) 
                FROM cdes_quota
                GROUP BY card_no

SUBSTRING_INDEX()取出n个数据

以上参考: sql 查询 group by分组 后 获得每组中时间最大 记录_czc9309的博客-CSDN博客_sql分组查询取每组最大

 

三.mysql的instr函数

INSTR(STR,SUBSTR) 在一个字符串(STR)中搜索指定的字符(SUBSTR),返回发现指定的字符的位置(INDEX); 
STR 被搜索的字符串 
SUBSTR 希望搜索的字符串 
结论:在字符串STR里面,字符串SUBSTR出现的第一个位置(INDEX),INDEX是从1开始计算,如果没有找到就直接返回0,没有返回负数的情况。

参考: MySQL 的instr函数 - Mr_伍先生 - 博客园

四.记录mybatis实际中bind的一次运用记录

SELECT
        <include refid="baseSql"></include>
        FROM
        cads_position
        <where>
            <if test="xxxDTO.name != null and xxxDTO.name != ''">
                AND INSTR(name,#{xxxDTO.name})
            </if>
            <if test="xxxDTO.detail!= null and xxxDTO.detail != ''">
                <bind name="likeDetail" value=" '%' + xxxDTO.detail +'%' "></bind>
                AND r.detail like #{likeDetail}
            </if>
        </where>
        order by create_time desc

五.记录mybatis一次递归的写法

最近有个需求要查询树形结构指标分并计算,但是树形的层数是未知的,这个需求考虑过遍历来实现,但是做起来就比较困难,思考后认为递归更好的能解决此类型需求,在mybatis里初步尝试,代码如下:

    <resultMap id="DetailMap" type="com.zjhc.caxxhr.model.dto.xxxxxTreeDto">
        <collection property="xxxList" select="queryxxxList"
                    column="{treeId=id}" ofType="com.zjhc.caxxhr.model.dto.xxxxxTreeDto" javaType="java.util.List">
        </collection>
    </resultMap>

    <select id="queryxxxList" parameterType="map" resultType="com.zjhc.caxxhr.model.dto.xxxxxDto">
        select * from ca_xx
        where 1= 1
         and deleted = '0'
         and tree_id = #{treeId}
    </select>

之后找到一篇感觉不错的文章可以参考 : Mybatis自查询递归查找子菜单_程高伟-CSDN博客_mybatis递归查询 树

六.mysql插入和更新设置自动当前时间

1.timestamp :表示该字段在插入和更新时都不会自动设置为当前时间。
2.timestamp default current_timestamp :字段插入时如果没有时间则自动设置为当前时间,更新没有变化。
3.timestamp on update current_timestamp:字段插入不会自动设置,字段更新时没有指定时间则自动设置为当前时间。
4.default current_timestamp on update current_timestamp:插入和修改字段时如果没有指定时间则会自动设置为当前时间。
5.timestamp default ‘yyyy–mm–dd hh:mm:ss’ on update current_timestamp 
原文链接:https://blog.csdn.net/lxxfirst/article/details/108201157

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值