动态SQL 模糊查询 联表查询

1. 正文

1. set标签 和 foreach标签 trim标签 sql片段

2. mybatis映射文件处理特殊字符.

3. mybatis完成模糊查询。

4. 联表查询

2. 动态sql

2.1 set标签

这个配合if标签一起用,一般用在修改语句。如果传递的参数值为null,那么应该不修改该列的值。

<!--set:可以帮我们生成关键字 set 并且可以去除最后一个逗号-->
    <update id="update">
          update account
          <set>
             <if test="name!=null and name!=''">
                name=#{name},
             </if>
             <if test="money!=null">
                money=#{money},
             </if>
             <if test="isdeleted!=null">
                 isdeleted=#{isdeleted},
             </if>
             <if test="created!=null">
                  created=#{created},
             </if>
             <if test="updated!=null">
                  updated=#{updated},
             </if>
          </set>
          where id=#{id}
    </update>

2.2 foreach标签

循环标签.

查询:

<!-- select * from account where id in(2,3,5,7,0)
        如果你使用的为数组array  如果你使用的为集合 那么就用list
        collection:类型
        item:数组中每个元素赋值的变量名
        open: 以谁开始
        close:以谁结束
        separator:分割符
    -->
    <select id="findByIds" resultType="com.zjh.entity.Account">
        select * from account where id in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
             #{id}
        </foreach>
    </select>

删除:

 <delete id="batchDelete">
        <foreach collection="array" item="id" open="delete from account where  id in(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

添加:

insert into account(name,isdeleted) values('ldh',0),('ldh2',0),('ldh4',0)

    <insert id="saveBatch">
        insert into account(name,isdeleted) values
        <foreach collection="list" item="acc" separator=",">
            (#{acc.name},#{acc.isdeleted})
        </foreach>
    </insert>

2.3 sql片段

在执行查询语句时不建议大家使用select *, 建议大家把查询的列写出。

3. mybatis映射文件处理特殊字符.

   <!--
       第一种:转义标签 &nbsp; &lt;  
       第二种: <![CDATA[sql]]>
    -->
    <select id="findByMaxMin" resultType="com.zjh.entity.Account">
           <![CDATA[select * from account where id >#{min} and id <#{max}]]>
    </select>

4. mybatis完成模糊查询。

select * from 表名 where 列名 like '%a%'

(1)使用字符串函数 完成拼接 concat

<select id="findByLike" resultType="com.zjh.entity.Account">
   select * from account where name like concat('%',#{name},'%')
</select>

(2) 使用${}

<select id="findByLike" resultType="com.zjh.entity.Account">
   select * from account where name like '%${name}%'
</select>

通过观察: 发现使用${}实际上是字符串拼接,它不能防止sql注入, 而#{}它是预编译,它可以防止sql注入问题,#{}实际使用的PreparedStatement.

总结:

动态sql标签: if where (choose when otherwise) set foreach sql 处理特殊字符: <![CDATA[sql]]> 转义符 模糊查询: concat('',#{},'')   ${}

5. 联表查询

  1. 多对一 : 从多的一方来查询一的一方。

班级表:

|1-n

学生表:

根据学生id查询学生信息并携带班级信息。

select * from tb_stu s join tb_class c on s.class_id=c.cid where stu_id=1

实体类:

package com.zjh.entity;
​
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
​
/**
 * @author: jh
 * @create: 2022/6/2
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private int id;
    private String name;
    private int age;
    private String sex;
    private Integer classId;
​
    private Clazz clazz;//学生所属的班级
}
//如何把联表查询体现到实体类上。
package com.zjh.entity;
​
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
​
/**
 * @author: jh
 * @create: 2022/6/2
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Clazz {
    private Integer cid;
    private String cname;
​
}

xml:

<resultMap id="baseMap" type="com.zjh.entity.Student">
            <id column="stu_id" property="id"/>
            <result column="stu_name" property="name"/>
            <result column="stu_age" property="age"/>
            <result column="sex" property="sex"/>
            <result column="class_id" property="classId"/>
            <!--association: 表示一的一方
                 property: 它表示属性名
                 javaType: 该属性名对应的数据类型
            -->
            <association property="clazz" javaType="com.zjh.entity.Clazz">
                <id column="cid" property="cid"/>
                <result column="cname" property="cname"/>
            </association>
    </resultMap>
    <select id="findStudentById" resultMap="baseMap">
         select * from tb_stu s join tb_class c on s.class_id=c.cid where stu_id=#{id}
    </select>

上面你要是绝的没有懂,那么这里可以使用的一个笨的方式: 但是不推荐。

返回类型就用map封装

//根据学生编号查询学员信息以及班级信息
 public Map findById(Integer id);


<!--key:value-->
    <select id="findById" resultType="java.util.Map">
        select <include refid="aa"/> from tb_stu s join tb_class c on s.class_id=c.id where s.id=#{id}
    </select>
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态SQL是指在SQL语句中根据条件的不同而动态生成不同的SQL语句的技术。在动态SQL中,可以使用一些特定的标签和函数来处理特殊字符和实现模糊查询。 在MyBatis中,常用的动态SQL标签包括if、where、choose、when、otherwise、set、foreach等。这些标签可以根据条件的不同来决定是否包含某个SQL片段。比如,如果要进行模糊查询,可以使用concat函数将查询条件拼接成模糊匹配的格式。 下面是两种实现模糊查询的方式: 方式1: ```xml <select id="queryBlogIf" resultType="blog" parameterType="map"> select * from mybatis.blog where 1 = 1 <if test="title!=null"> and title like #{title} </if> </select> ``` 在这个例子中,使用了if标签来判断是否有模糊查询的条件,如果有,则将条件拼接到SQL语句中。 方式2: ```xml <select id="queryBlogIf" resultType="blog" parameterType="map"> select * from mybatis.blog where 1 = 1 <if test="title!=null"> and title like concat('%',#{title},'%') </if> </select> ``` 在这个例子中,使用了concat函数将查询条件拼接成模糊匹配的格式。 通过使用这些动态SQL标签和函数,我们可以根据不同的条件来动态生成符合需求的SQL语句,实现灵活的模糊查询功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [动态SQL 模糊查询 联表查询](https://blog.csdn.net/Luckydogs3036/article/details/125130893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mybatis动态sql模糊查询方法](https://blog.csdn.net/niceYF/article/details/123370626)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值