Mybatis 动态Sql练习

建表

CREATE TABLE `student` (
  `s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `s_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
  `s_birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
  `s_sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`s_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

在这里插入图片描述

1. if语句

根据 name和 sex 来查询数据。如果name为空,那么将只根据sex来查询;反之只根据name来查询

<select id="findStudentByNameAndSex" resultType="StudentEntity">
        select * from student where 1 = 1
        <if test="sName != null">
            and s_name = #{sName}
        </if>
        <if test="sSex != null">
            and s_sex = #{sSex}
        </if>
    </select>

可以看到必须要加个1 = 1 ,不然的会就会多个and, 如果去掉第一个sql也有可能第一个刚好没执行,执行的第二个, 又多个and

2. if + where

  <select id="findStudentByNameAndSex2" resultType="StudentEntity">
        select * from student
        <where>
            <if test="sName != null">
                s_name = #{sName}
            </if>
            <if test="sSex != null">
                and s_sex = #{sSex}
            </if>
        </where>
    </select>

这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

3. set

<update id="updateStudentById" parameterType="StudentEntity">
        update student
        <set>
            <if test="sName != null and sName != ''">
                s_name = #{sName},
            </if>
            <if test="sSex != null and sSex != ''">
                s_sex = #{sSex}
            </if>
        </set>
        where s_id = #{sId}
    </update>

4. choose

只想选择其中的一个,查询条件有一个满足即可,

<select id="findStudentByChoose" parameterType="StudentEntity" resultType="StudentEntity">
        select * from student
        <where>
           <choose>
               <when test="sId != null and sId != ''">
                   s_id = #{sId}
               </when>
               <when test="sName != null and sName != ''">
                   and s_name = #{sName}
               </when>
               <otherwise>
                   and s_sex = #{sSex}
               </otherwise>
           </choose>
        </where>
    </select>

5. trim

 <select id="findStudentByNameAndSex3" resultType="StudentEntity">
        select * from student
        <trim prefix="where" prefixOverrides="and | or">
            <if test="sName != null and sName != ''">
                and s_name = #{sName}
            </if>
            <if test="sSex != null and sSex != ''">
                and s_sex = #{sSex}
            </if>
        </trim>

    </select>

prefix:前缀

prefixoverride:去掉第一个and或者是or

6. sql片段

 <sql id="findStudentByNameAndSex4SQL">
        <if test="sName != null and sName != ''">
            s_name = #{sName}
        </if>
        <if test="sSex != null and sSex != ''">
            and s_sex = #{sSex}
        </if>
    </sql>
    <select id="findStudentByNameAndSex4" resultType="StudentEntity">
        select * from student where 
        <include refid="findStudentByNameAndSex4SQL"></include>
    </select>

7. foreach

@Data
public class StudentVO {
    private List<String> ids;
}
 <select id="findStudentsByIds" parameterType="com.wang.vo.StudentVO" resultType="StudentEntity">
        select * from student
        <where>
            s_id in
            <foreach collection="ids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </select>
posted @ 2021-07-14 15:47  iucbjb  阅读(75)  评论(0)    收藏  举报