Mybatis中常见标签名

<trim prefix=““ suffix=““ suffixOverrides=““ prefixOverrides=““>
</trim>

prefix:在trim标签内sql语句加上前缀

suffix:在trim标签内sql语句加上后缀

suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","

prefixOverrides:指定去除多余的前缀内容

<insert id="方法名" parameterType="" useGeneratedKeys="" keyProperty="" >
    insert into 表名
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="a != null" >
        a ,
      </if>
      ...........
      
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >

      <if test="a != null" >
        #{a},
      </if>
     ............

    </trim>
    <selectKey resultType="" order="" keyProperty="">
      ......
    </selectKey>
</insert>

假设没有指定
suffixOverrides=","  
执行的sql语句也许是这样的:
insert into user (id,name,location,) values(1,"tom","beijing",);
显然是错误的
指定之后语句就会变成
insert into user (id,name,location) values(1,"tom","beijing");
这样就将“,”去掉了

<include refid="Base_Column_List" />

<sql id="Base_Column_List" >
        name,location
</sql> 
 
<select id="方法名" resultMap="" parameterType="" >
       select 
        <include refid="Base_Column_List" />
        from 表名
       where id = #{id,jdbcType=BIGINT}
</select>

Base_Column_List就是提前定义好的字段

resultMap="BaseResultMap"

<sql>标签

用来定义表中出现的所有字段,方便其他地方调用

<sql id="Base_Column_List">
    id,name,location,age.......
<sql>

<include>标签

引用公用的代码,例如上面的sql标签

<select id="" resultMap="" parameterType="" >
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=BIGINT}
</select>

<where>标签

如果某一个条件符合,会动态的拼接sql where条件
如果第二个条件为空,会智能的去除and连接符

<select id="" parameterType="" resultType="">
    select count(*) from user 
    <where>
        <if test="name != null">
            name=#{name}
        </if>
        <if test="age != null">
            and age=#{age}
        </if>
    </where>
</select>

<if>标签

用来动态判断属性

<if test a != null>
a = #{a}
<if>

<foreach>标签
常用来批量操作时拼接条件,其中item表示循环体的别名,collection可以为list、Map、array

<insert id="" parameterType="">
        insert into user (
        name,location,age)
        values
        <foreach collection="list" item="conf" index="index" separator=",">
            (
                #{conf.name,jdbcType=VARCHAR},
                #{conf.location,jdbcType=VARCHAR},
                #{conf.age,jdbcType=INTEGER}
                
            )
        </foreach>
</insert>

useGeneratedKeys=""

keyProperty=""

这两个不是很理解,还在整理中

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值