动态SQL

动态SQL: 根据条件动态的对SQL进行拼接组装

执行原理:使用OGNL从SQL参数中计算表达式的值,根据表达式的值动态的拼接SQL,以此完成动态SQL功能

标签:如;if标签,where、trim、foreach等标签

if标签:

通过student中的条件查询对象或对象结果

通过id查询:select * from student where SID= ?

通过name查询:select * from student where Name= ?

通过id和name查询:select * from student where id=? and name =?

全表查询:select * from student

if标签使用示例:

  <select id="selectStudent" parameterType="student" resultType="student">
        select * from student
        where  1=1
        <if test="id != null and id != 0" >
            and  SID = #{id}
        </if>
        <if test="name != null" >
           and Sname = #{name}
        </if>
    </select>

if标签通常是根据条件做判断,一般作为where子句的一部分

if表达式判断是否传递参数在其后面有一个test属性,该属性必填,为true或false,

为true时,会拼接该if中SQL片段,为false,则不拼接SQL子句

该test的判断是通过OGNL表达式判断

传递name和id属性:

        //新建一个student对象
        Student student = new Student();
        //按照id查询
        student.setId(1);
        //按照姓名查询
        student.setName("zhangsan");
        //执行方法
        studentMapper.selectStudent(student);

执行结果:

只传递id:

    Student213 student213 = new Student213();
        student213.setId(1);
        studentMapper213.selectStudent(student213);

执行结果:

不传递参数:

        Student213 student213 = new Student213();
        studentMapper213.selectStudent(student213);

执行结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值