2023/8/15 Java EE复习笔记(mybatis-2 映射文件sql优化技巧)

一、动态sql拼接技巧

1、sql语句段的拼接

通用语句段

<sql id="partOfSql">
    id
    ,`name`,age,gender,banji_id
</sql>

拼接调用示例

<select id="selectById" parameterType="Integer" resultMap="studentMap">
    select
    <include refid="partOfSql"/>
    from student
    where id = #{id}
</select>

2、where+if标签拼接

这里不存在else的情况,满足条件就将相应信息添加在where中,没有满足的条件就不加where。

<select id="selectByDynamicCondition" parameterType="Student" resultMap="studentMap">
    select *
    from student 
    <where>
        <if test="name!=null and name!='' ">
            and x.name like concat('%',#{name},'%')
        </if>
        <if test="age!=null">
            and age = #{age}
        </if>
        <if test="gender!=null and gender!='' ">
            and gender = #{gender}
        </if>
    </where>
</select>

3、choose标签拼接

choose拼接时,choose中的语句最多执行一条,所以当参数没有满足任何条件时,where显得多余,此时需要在otherwise标签里加上1=1。

<select id="selectByChoose" parameterType="student" resultType="student">
    select id,name,age,gender
    from xuesheng
    where
    <choose>
        <when test="id!=null and id!=0">
            id=#{id}
        </when>
        <when test="name!=null and name!=''">
            name like concat('%',#{name},'%')
        </when>
        <when test="gender!=null and gender!=''">
            gender=#{gender}
        </when>
        <otherwise>
            1=1
        </otherwise>
    </choose>
</select>

4、in()循环拼接

(1)参数为List集合

<delete id="deleteAllByList">
    delete
    from student
    where id in
    <foreach collection="list" open="(" item="id" separator="," close=")">
        #{id}
    </foreach>
</delete>

(2)参数为数组

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

5、set+if标签拼接

<update id="updateCondition" parameterType="student">
    update student
    <set>
        <if test="name!=null and name!=''">
            name=#{name},
        </if>
        <if test="age!=null">
            age=#{age},
        </if>
        <if test="gender!=null and gender!=''">
            gender=#{gender},
        </if>
    </set>
    <where>
        <if test="id!=null">
            and id=#{id}
        </if>
    </where>
</update>

二、resultMap返回属性自定义匹配标签

1、名称不匹配

<resultMap id="studentMap" type="Student">
        <result property="name" column="student_name"></result>
        <result property="banjiId" column="class_id"></result>
</resultMao>

2、单向一对一关系

举例:学生->班级

<resultMap id="studentMap" type="Student">
    <result property="id" column="id"></result>
    <result property="name" column="name"></result>
    <result property="age" column="age"></result>
    <result property="gender" column="gender"></result>
    <result property="banjiId" column="banjiId"></result>
    <!--association用于封装一对一的跟随属性-->
    <association property="banji" javaType="Banji">
        <id column="banjiId" property="id"></id>
        <result column="banjiName" property="name"></result>
    </association>
</resultMap>

3、单向一对多关系

举例:班级->学生

<resultMap id="banjiMap" type="Banji">
    <id property="id" column="id"></id>
    <result column="name" property="name"></result>
    <!--collection用于封装一对多的跟随属性-->
    <collection property="studentList" ofType="Student">
        <id property="id" column="studentId"></id>
        <result column="studentName" property="name"></result>
        <result column="studentAge" property="age"></result>
        <result column="studentGender" property="gender"></result>
        <result column="id" property="banjiId"></result>
    </collection>
</resultMap>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flww*星火燎原

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值