Mybatis,动态SQL

博客介绍了Mybatis,它是基于ORM的轻量级框架,底层封装JDBC。阐述了ORM中应用对象与数据库表的映射关系及不同情况的处理方式。还介绍了动态SQL,如if、where、choose、foreach等,以及foreach的相关属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis

概述

mybatis是一款基于ORM轻量级框架,底层封装了JDBC。

ORM
o:应用对象,实体类
r:数据库表
m:建立o与r之间的映射关系

  1. 属性名称与数据库字段完全一致(resultMap)
  2. 属性名称与数据库字段符合驼峰命名(resultMap)
  3. 属性名称与数据库字段不符合1和2,则自定义封装(resultMap)

动态SQL

  1. if
<select id="queryByCount" parameterType="Map" resultType="Long">
    select count(1) from rms_personnel
    <where>
        <!--名字-->
        <if test="name!=null and name!='' ">
           name like concat('%',#{name},'%')
        </if> 
    </where>
</select>
  1. where
  2. choose
<select id="getSubsidyPersonnel" resultType="Map">
     select id,name,unit,card,grade from rms_personnel
     where
        <choose>
            <when test="type==1">
                property_subsidies=1
            </when>         
            <otherwise>
                vehicles_subsidies=1
            </otherwise>
        </choose>       
</select>
  1. foreach

collection:迭代元素的类型
item:循环中的对象
index:下标
open:循环开始前添加的元素
close:循环结束后添加的元素
separator:循环分隔符,每循环一次添加一次分隔符

<delete id="deletes" parameterType="int">
   delete from rms_subsidy where id in
   <foreach collection="array" item="id" open="(" close=")" separator=",">
        #{id}
   </foreach>
</delete>
[11,22,33,44]
<foreach collection="array" item="id" index="i">
  #{id} 11,22,33,44
  #{i}  0,1,2,3
</foreach>
{name:admin,age:22}
<foreach collection="map" item="value" index="key">
  #{key}   name,age
  #{value} admin,22 
</foreach>

5.set

<update id="update" parameterType="subsidy">
        update rms_subsidy
        set money=#{money}
        where id = #{id}
</update>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值