动态SQL

发布于:2024-06-28 ⋅ 阅读:(18) ⋅ 点赞:(0)

mapper结构:

1、 每一个实体类对应一个mapper.xml
2、 在每一个mapper中:

  <mapper  namespace=A” 这个A 是实体类mapper路径。 
 <resultMap  id =唯一id(随便起名字),type="实体类路径">
 //使用id标签  代表是数据库中主键
<id property="实体类中的id" column="数据库中的id" jdbcType="INTEGER"/> 
//使用result标签是 不是主键的 属性,
<result property="name" column="name" jdbcType="VARCHAR"/>
</resultMap>
<mapper namespace="com.example.mybatisplus.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.example.mybatisplus.domain.User">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="age" column="age" jdbcType="VARCHAR"/>
        <result property="email" column="email" jdbcType="VARCHAR"/>
    </resultMap>

if 标签:

//  id  是方法名,resultMap返回到map 中做映射,parameterType参数类型,如果是实体是路径,如果是值,写对应的类型即可。
   <select id="selectAll" resultMap="BaseResultMap" parameterType="com.example.mybatisplus.domain.User">
        select * from  user
        where 1=1
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="age != null">
            and age = #{age}
        </if>
    </select>

where 标签

代替 where 1=1 情况。

<select id="selectAll2" resultMap="BaseResultMap" parameterType="com.example.mybatisplus.domain.User">
    select * from user
    <where>
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="age != null">
            and age = #{age}
        </if>
    </where>
</select>

choose 标签
有时候,我们不想使用所有的条件,而是想从多个条件中选择一个使用,针对这种情况MyBatis提供的了choose元素,它有点像java中switch。

 <select id="selectAll3" resultMap="BaseResultMap" parameterType="com.example.mybatisplus.domain.User">
        select * from user
        <choose>
            <when test="name != null">
                and name = #{name}
            </when>
            <when test="age != null">
                and age = #{age}
            </when>
            <otherwise >
                ORDER BY id DESC
            </otherwise>

        </choose>
    </select>

set标签
当我们需要实现动态更新操作的时候,我们会使用到set标签
set标签就是管理 里面的逗号的。

 <update id="update1" parameterType="com.example.mybatisplus.domain.User">
        update user
        <set>
            <if test="name != null">
                 name = #{name},
            </if>
            <if test="age != null">
                 age = #{age},
            </if>
        </set>
       where
           id =#{id}
    </update>

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

    <select id="selectAll4" resultMap="BaseResultMap" parameterType="com.example.mybatisplus.domain.User">
        select * from user
--  1、替代where标签,截取头部的 and 或者or 去掉
       <trim prefix="where" prefixOverrides="and | or">
           <if test="name != null">
               and name = #{name}
           </if>
           <if test="age != null">
               and age = #{age}
           </if>
       </trim>


    </select>
    <update id="update2" parameterType="com.example.mybatisplus.domain.User">
        update user
--             替代set标签,将最后一个 逗号 去掉
        <trim prefix="set" suffixOverrides=",">
            <if test="name != null">
                name = #{name},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
        </trim>
        where
        id =#{id}
    </update>

foreach 标签
动态SQL的另一个常用的使用场景是对集合的遍历

//在mapper 中指定一下 ids ,在xml 中进行对应
    public List<User> selectAll5( @Param("ids") List<Integer> ids)


    <select id="selectAll5" resultMap="BaseResultMap" parameterType="list">

        -- select * from user where id in(1,2,5,7,9);
        select *
        from user
        where 1=1
        <if test="ids !=null">
           and  id in
            <foreach collection="ids" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </if>
    </select>

bind标签
bind元素允许在OGNL表达式以外创建一个变量,将该变量绑定到上下文中,可以后续使用

    <select id="selectAll6" resultType="com.example.mybatisplus.domain.User">
        <bind name="usernamelink" value="'%'+_parameter.getName+'%'"/>
        select * from user
        <where>
            <if test="name != null">
                and name like #{usernamelink}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>
    </select>