Mybatis动态sql

这个博客展示了如何使用MyBatis进行数据库的更新和查询操作。`StudentDao`映射器中包含了更新(updates)和多个查询(findBy、findByIdName、findBysid、findBypage)方法,利用了MyBatis的动态SQL特性,如`<if>`、`<trim>`和`<foreach>`标签来处理条件语句和分页查询。
摘要由CSDN通过智能技术生成
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.dao.StudentDao">
    <update id="updates" parameterType="com.pojo.Student">

        update school.student
        <trim suffix="where sid=#{sid}" suffixOverrides=",">
        <set>
            <if test="name!=null and name!=''">name = #{name},</if>
            <if test="sex!=null and sex!=''">sex = #{sex},</if>
            <if test="height!=null and height!=''">height = #{height},</if>
        </set>

        </trim>


    </update>

    <select id="findBy" resultType="Student">
        select *
        from student
        <where>
            <trim suffixOverrides="and">
                <if test="name!=null and name!=''">
                    name like concat(#{name},'%')
                </if>
                <if test="sex!=null and sex!=''">
                    and sex=#{sex}
                </if>
            </trim>
        </where>
    </select>

    <select id="findByIdName" resultType="com.pojo.Student">
        select *
        from student
        where 1 = 1
        <choose>
            <when test="sid!=0">
                and sid = #{sid}
            </when>
            <when test="name!=null and name!=''">
                and name = #{name}
            </when>
            <otherwise>
                and sid = 0
            </otherwise>
        </choose>

    </select>

    <select id="findBysid" resultType="com.pojo.Student">
        select *
        from school.student
        where sid in
        <foreach collection="list" item="sid" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>


    <select id="findBypage" resultType="com.pojo.Student">
        select *
        from school.student
        limit #{pageSize},#{pageCode};
    </select>


</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值