Mybatis动态sql和sql片段


<?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.alibaba.uyuni.dal.mapper.StudentDoMapperExt" >

<select id="getPageList" parameterType="int" resultType="com.alibaba.uyuni.biz.common.bo.dto.StudentDto">

select id ,name,age,sex,grade,class as team,teacher from student where is_deleted='n' order by id limit #{offset},8 
</select>

<select id="getTotoalNum" resultType="int" >
select count(*) from student where is_deleted='n'
</select >
<select id="condtionQuery" parameterType="com.alibaba.uyuni.biz.common.bo.dto.StudentDto" resultType="com.alibaba.uyuni.biz.common.bo.dto.StudentDto">
select id ,name,age,sex,grade,class as team,teacher from student 
      <where>
     <include refid="query_student_where"/>
     </where>
</select>

<sql id="query_student_where">

    <if test='name != null and name != ""'>
               and  name like '%${name}%'
               </if>
              <if test='age != null and age != ""'>
               and  age = #{age}
               </if>
               <if test='sex != null and sex != ""'>
               and  sex = #{sex}
               </if>
                 <if test=' grade != null and grade != ""'>
               and  grade = #{grade}
               </if>
                 <if test=' team != null and team != ""'>
               and  class = #{team}
               </if>
                  <if test=' teacher != null and teacher != ""'>
               and  teacher = #{teacher}
               </if>
              and is_deleted='n'
</sql>

</mapper>


使用了<where>标签就不需要加where关键字,<where>标签会自动去掉第一个条件的and,or



本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1736937

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值