mysql日常小知识点

1、where 1=1的作用是什么?去掉1=1行不行?

  where 1=1 永真,当需要动态SQL拼接而判断条件不一定为真时(不一定拼接上去)起到占位的作用。可以使用标签代替,不可完全去掉这层意义。

2、对比where 1=1和标签的区别

  where 标签知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。总之,更加智能和人性化。
  而where 1=1 则一定在语句后面出现,且其追加的条件语句前都需要加and 或 or。

3、mybatis xml中常用的标签有哪些?

< sql >标签
该标签主要定义复用的sql语句片段,在执行的sql语句标签直接引用即可。可以提高编码效率、简化代码和提高可读性。
需要配置id熟悉,表示该sql片段的唯一标识。
引用:通过<include refid=" " / >标签引用,refid的值就是< sql>的id属性的值。

<sql id="findAllSql">
select * from user
</sql>
<select id="findByWhere" parameterType="user" resultType="user">
<include refid="findAllSql"/>//进行引用
<where>
<if test="username != null and username != ''">
and username like #{username}
</if>
</where>
</select>

< where >和< if >标签
< where > : 主要用来替换sql语句中的where字段,他的作用主要是用来简化sql语句中where条件判断的书写的

< if >:条件判断标签,配置属性test=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过。

<select id="findByWhere" parameterType="user" resultType="user">
select * from user
<where>
<if test="username != null and username != ''">
and username like #{username}
</if>
</where>

< set >标签
< set > : 主要用来替换sql语句中的set字段,一般在update中使用。

<update>
    update user 
    <set>
      	<if test="name != null and name.length()>0">name = #{name},</if>
      	<if test="age != null and age .length()>0">age = #{age },</if>
    </set>
    where id = #{id}
  </update> 

update user set name=‘xxx’ , age=‘xx’ where id=‘x’
set标记自动帮助我们把最后一个逗号给去掉

< trim>标签

< trim > : 是一个格式化的标记,可以完成set或者是where标记的功能。

示例1:

  select * from user 

  <trim prefix="WHERE" prefixoverride="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
    <if test="age != null and age.length()>0"> AND age=#{age}</if>

  </trim>

假如说name和age的值都不为null的话打印的SQL为:select * from user where name = ‘xx’ and age = ‘xx’

在where的后面是不存在第一个and的,上面两个属性的意思如下:
  prefix:前缀      
  prefixoverride:去掉第一个and或者是or

示例2:

update user
  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
    <if test="age!= null and age.length()>0"> age=#{age} ,  </if>
  </trim>

假如说name和age的值都不为null的话打印的SQL为:update user set name=‘xx’ , age=‘xx’ where id=‘x’

在age='xx’的后面不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
   suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
   suffix:后缀

foreach标签

select * from user where id=1 or id=2 or id=3
<select id="findByIds" parameterType="user" resultType="user">
SELECT * FROM USER
<where>
<foreach collection="ids" open="id=" separator="OR id=" item="i">
#{i}
</foreach>
</where>
</select>
SELECT * FROM USER WHERE id IN (1,2,3)
<select id="findByIds" parameterType="user" resultType="user">
SELECT * FROM USER
<where>
<foreach collection="ids" open="id IN (" separator="," item="i"
close=")">
#{i}
</foreach>
</where>
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值