Mybatis标签简单介绍

本文介绍了如何在Spring MVC中通过<foreach>标签实现SQL动态查询,展示了如何根据传入的列表参数动态构造WHERE子句,以及结合<if>标签进行条件判断。同时,还涵盖了分页插件配置和多种技术领域的应用实例。
摘要由CSDN通过智能技术生成

<foreach>标签 

传统写法,参数只能写死,不灵活

 <select id="findByIds" parameterType="list" resultType="user">
        select * from user where id in(1,2,3)   
    </select>

    使用<foreach>标签

 <select id="findByIds" parameterType="list" resultType="user">
        select * from user
        <where>
            <foreach collection="list" open="id in(" close=")" separator="," item="id">
                #{id}
         </foreach>
        </where>
    </select>

collection表示要遍历的类型,open表示传统写法开头的部分,close表示传统写法结束的部分,separator表示传统写法用的什么分隔符,item表示给要遍历的变量取个名字,必须和下面的#{}里面的一致

sql语句抽取

<!--sql语句抽取-->
    <sql id="selectUser">select * from user </sql>

抽取后可通过<include>标签提取插入

<if>标签和sql语句抽取一起使用

<select id="findByCondition" parameterType="user" resultType="user">
        /*这里使用sql语句抽取的方式*/
        <include refid="selectUser"></include>
        <where>
        <if test="id !=0">
            and  id=#{id}
        </if>

        <if test="username !=null">
            and  username=#{username}
        </if>

        <if test="password !=null">
         and password=#{password}
        </if>
        </where>
    </select>

<plugins>标签

 <!--配置分页助手插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值