首页 > 数据库技术 > 详细

Mybatis动态sql和sql片段

时间:2016-01-20 22:47:43      阅读:275      评论:0      收藏:0      [点我收藏+]


<?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

Mybatis动态sql和sql片段

原文:http://tianxingzhe.blog.51cto.com/3390077/1736937

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!