一、forEach

接口:

public List<Entity> queryById(List<String> userids);

语法:

<select id="queryById" resultMap="BaseReslutMap" >
select * FROM entity
where id in
<foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
#{userid}
</foreach>
</select>

二、concat (模糊查询)

语法:

<select id="queryById" resultMap="BascResultMap" parameterType="entity">
SELECT * from entity
<where>
<if test="name!=null">
name like concat('%',concat(#{name},'%'))
</if>
</where>
</select>

  

三、choose(when、otherwise)标签

语法:

<!-- choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">
  SELECT *
  FROM User u
    <where>
      <choose>
        <when test="username !=null ">
          u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')
        </when >
        <when test="sex != null and sex != '' ">
          AND u.sex = #{sex, jdbcType=INTEGER}
        </when >
        <when test="birthday != null ">
          AND u.birthday = #{birthday, jdbcType=DATE}
        </when >
      <otherwise>
      </otherwise>
    </choose>
  </where>
</select>

四、selectKey(自动赋值添加对象生成的组件)

<!-- 插入学生 自动主键-->
<insert id="createStudentAutoKey" parameterType="liming.student.manager.data.model.StudentEntity" keyProperty="studentId">
<selectKey keyProperty="studentId" resultType="String" order="BEFORE">
select nextval('student')
</selectKey>
INSERT INTO STUDENT_TBL(STUDENT_ID,
STUDENT_NAME,
STUDENT_SEX,
STUDENT_BIRTHDAY,
STUDENT_PHOTO,
CLASS_ID,
PLACE_ID)
VALUES (#{studentId},
#{studentName},
#{studentSex},
#{studentBirthday},
#{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
#{classId},
#{placeId})
</insert>

五、参数为ArrayList类型

<!— 7.1 foreach(循环array参数) - 作为where中in的条件 -->
<select id="getStudentListByClassIds_foreach_array" resultMap="resultMap_studentEntity">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
ST.STUDENT_SEX,
ST.STUDENT_BIRTHDAY,
ST.STUDENT_PHOTO,
ST.CLASS_ID,
ST.PLACE_ID
FROM STUDENT_TBL ST
WHERE ST.CLASS_ID IN
<foreach collection="array" item="classIds" open="(" separator="," close=")">
#{classIds}
</foreach>
</select> 

六、if + where

<!-- 3 select - where/if(判断参数) - 将实体类不为空的属性作为where条件 -->
<select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
ST.STUDENT_SEX,
ST.STUDENT_BIRTHDAY,
ST.STUDENT_PHOTO,
ST.CLASS_ID,
ST.PLACE_ID
FROM STUDENT_TBL ST
<where>
<if test="studentName !=null ">
ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
</if>
<if test="studentSex != null and studentSex != '' ">
AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
</if>
<if test="studentBirthday != null ">
AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
</if>
<if test="classId != null and classId!= '' ">
AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
</if>
<if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
</if>
<if test="placeId != null and placeId != '' ">
AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
</if>
<if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
</if>
<if test="studentId != null and studentId != '' ">
AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
</if>
</where>
</select>

最新文章

  1. mssql 2008 游标 临时表 作业批处理失败问题
  2. hdu 1247:Hat’s Words(字典树,经典题)
  3. ios 屏幕概况
  4. delphi 从TWebBrowser WebBrowser得到全部html源码
  5. IOS面试攻略
  6. android Json解析详解(详细代码)
  7. servlet第2讲(下集)----创建servlet实例(继承GenericServlet)
  8. string,char*,int 之间的转化
  9. CentOS7 64位 安装MySQL5.7
  10. 获取Android设备唯一标识码
  11. vue 全局变量
  12. fast-spring-boot快速开发项目
  13. ajax上传文件以及实现上传进度条(转载)
  14. Oracle中nvl()、instr()、及执行多条sql事务操作
  15. date命令转换日期命令提示date: illegal time format
  16. 015-Go 数据库操作注意事项
  17. SSH框架整合开发具体解释(个人笔记)
  18. EBS标准的查看供应商地址
  19. Flex4 outerDocument
  20. C语言字符串的输入输出

热门文章

  1. 第 10 章 gdb
  2. 基于SpringBoot框架企业级应用系统开发全面实战
  3. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
  4. java通过HSSFWorkbook导出xls文件
  5. ZJNU 1528 - War--高级
  6. upstream实现内网网站在公网访问
  7. ILSVRC2012下载
  8. mysql创建视图和存储过程,变量
  9. Android开发学习3
  10. Codeforces Educational Round 81 解题报告