mybatis中动态SQL

MyBatis的映射文件中支持在基础SQL上添加一些逻辑操作,并动态拼接成完整的SQL之后再执行,以达到SQL复用、简化编程的效果。

1.<sql>

<mapper namespace="com.cos.qf.Dao.EmpMapper">
    <!-- sql 存放sql语句 可以帮助我们进行sql语句的复用,例如查询的字段 ,基础表查询 select xx from xxx-->
    <sql id="base_sql">
        eno,ename,esex,salary,pwd,dno,borndate
    </sql>
    <select id="getEmp" resultType="com.cos.qf.entity.Emp">
        select
        <!-- <include refid="base_sql"></include>-->
        <include refid="base_sql"/>  <!-- 通过ID引用SQL片段 -->
        from emp
    </select>
</mapper>

2.<where>

  <!-- <where> 会自动帮我们拼接where 关键字 WHERE,并且会自动忽略前后缀(如:and | or)-->
    <select id="findEmpByKey" resultType="com.cos.qf.entity.Emp">
        select
        <include refid="base_sql"></include>
        from emp
        <where>
            <if test="eno!=null and eno!=0">
                and eno=#{eno}
            </if>
            <if test="ename!=null and ename!=''">
                and ename=#{ename}
            </if>
            <if test="esex!=null and esex!=''">
                and esex=#{esex}
            </if>
        </where>
    </select>
<!-- and salary<=#{esalary} 选中后try/catch的快捷键弹出 -->
    <select id="findEmpByKey2" resultType="com.cos.qf.entity.Emp">
        select
        <include refid="base_sql"></include>
        from emp
        <where>
            <if test="ssalary!=null and ssalary!=0">
                and salary>=#{ssalary}
            </if>
            <if test="esalary!=null and esalary!=0">
                <![CDATA[
                and salary<=#{esalary}
                ]]>
            </if>
        </where>
    </select>

3.<set>

 <!-- set子句中满足条件的if,会自动忽略后缀(如:,) -->
    <update id="updateEmpByKey">
        update emp
        <set>
            <if test="ename!=null">
                ename=#{ename},
            </if>
            <if test="esex!=null">
                esex=#{esex},
            </if>
        </set>
        where eno=#{eno}
    </update>

4.<trim>

< trim prefix="添加前缀" suffix="添加后缀"   prefixOverrides="自动忽略前缀" suffixOverrides="自动忽略后缀" >代替< where > 、< set >

 <select id="findEmpByKey3" resultType="com.cos.qf.entity.Emp">
        select
        <include refid="base_sql"/>
        from emp
        <trim prefix="Where" prefixOverrides="And | OR" suffixOverrides="," suffix="order by salary desc">
            <if test="ssalary!=null and ssalary!=0">
                and salary>=#{ssalary}
            </if>
            <if test="esalary!=null and esalary!=0">
                <![CDATA[
                and salary<=#{esalary}
                ]]>
            </if>
        </trim>
    </select>
<update id="updateBookByCondition">
		UPDATE t_books
		<trim prefix="SET" suffixOverrides=","> <!-- 增加SET前缀,自动忽略后缀 -->
				<if test="name != null">
						name = #{name} ,
				</if>

				<if test="author != null">
						author = #{author} ,
				</if>

				<if test="publish != null">
						publish = #{publish} ,
				</if>

				<if test="sort != null">
						sort = #{sort}
				</if>
    </trim>
		WHERE id = #{id}
</update>

5.<foreach>

  <!-- delete from emp in (1,2,3,4,5,6) -->
<delete id="deleteBookByIds">
		DELETE FROM t_books
		WHERE id IN
		<foreach collection="list" open="(" separator="," close=")"  item="id" index="i">
				#{id}
		</foreach>
</delete>
   <!-- 批量新增 -->
    <!--   INSERT INTO emp VALUES ((),()) -->
    <!--
      for (Emp emp : empByKey2) {
            System.out.println(emp);
        }
    -->
    <insert id="addEmps" >
        insert into emp values
        <foreach collection="list" separator="," item="e">
            (default,#{e.ename},#{e.esex},#{e.salary},#{e.pwd},#{e.dno},#{e.bornDate})
        </foreach>
    </insert>
参数描述取值
collection容器类型list、array、map
open起始符(
close结束符)
separator分隔符,
index下标号从0开始,依次递增
item当前项任意名称(循环中通过 #{任意名称} 表达式访问)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值