码迷,mamicode.com
首页 > 数据库 > 详细

Mybatis动态sql和sql片段

时间:2016-01-20 22:50:57      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:mybatis 动态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

Mybatis动态sql和sql片段

标签:mybatis 动态sql

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!