MyBatis SQL语句写法

一、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>

 

转载于:https://www.cnblogs.com/ldl326308/p/10956504.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值