mybatis sql動態語句

if 將實體類不爲空的屬性做爲where條件html

<select id="getStudentList_if" 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>   
</select>

 

where/if 將實體類不爲空的屬性做爲where條件java

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

if/set 將實體類不爲空的屬性更新apache

<update id="updateStudent_if_set" parameterType="liming.student.manager.data.model.StudentEntity">  
 UPDATE STUDENT_TBL  
 <set>  
     <if test="studentName != null and studentName != '' ">  
         STUDENT_TBL.STUDENT_NAME = #{studentName},  
     </if>  
     <if test="studentSex != null and studentSex != '' ">  
         STUDENT_TBL.STUDENT_SEX = #{studentSex},  
     </if>  
    <if test="studentBirthday != null ">  
         STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
     </if>  
     <if test="studentPhoto != null ">  
         STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},  
     </if>  
     <if test="classId != '' ">  
         STUDENT_TBL.CLASS_ID = #{classId}  
     </if>  
     <if test="placeId != '' ">  
         STUDENT_TBL.PLACE_ID = #{placeId}  
     </if>  
 </set>  
 WHERE STUDENT_TBL.STUDENT_ID = #{studentId};      
</update>

if/trim代替where(判斷參數) - 將實體類不爲空的屬性做爲where條件ide

<select id="getStudentList_if_trim" 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   
 <trim prefix="WHERE" prefixOverrides="AND|OR">  
     <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>  
 </trim>     
</select>

if/trim代替set(判斷參數) - 將實體類不爲空的屬性做爲where條件code

<update id="updateStudent_if_trim" parameterType="liming.student.manager.data.model.StudentEntity">  
 UPDATE STUDENT_TBL  
 <trim prefix="SET" suffixOverrides=",">  
     <if test="studentName != null and studentName != '' ">  
         STUDENT_TBL.STUDENT_NAME = #{studentName},  
     </if>  
     <if test="studentSex != null and studentSex != '' ">  
         STUDENT_TBL.STUDENT_SEX = #{studentSex},  
       </if>  
     <if test="studentBirthday != null ">  
         STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
     </if>  
     <if test="studentPhoto != null ">  
         STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},  
     </if>  
     <if test="classId != '' ">  
         STUDENT_TBL.CLASS_ID = #{classId},  
     </if>  
     <if test="placeId != '' ">  
         STUDENT_TBL.PLACE_ID = #{placeId}  
     </if>  
 </trim>  
 WHERE STUDENT_TBL.STUDENT_ID = #{studentId}  
</update>

choose(判斷參數) - 按順序將實體類第一個不爲空的屬性做爲where條件htm

<select id="getStudentList_choose" 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>  
     <choose>  
         <when test="studentName !=null ">  
             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
         </when >  
         <when test="studentSex != null and studentSex != '' ">  
             AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
         </when >  
         <when test="studentBirthday != null ">  
             AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
         </when >  
         <when test="classId != null and classId!= '' ">  
             AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
         </when >  
         <when test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
             AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
         </when >  
         <when test="placeId != null and placeId != '' ">  
             AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
         </when >  
         <when test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
             AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
         </when >  
         <when test="studentId != null and studentId != '' ">  
             AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
         </when >  
         <otherwise>  
         </otherwise>  
     </choose>  
 </where>    
</select>

foreach(循環array參數) - 做爲where中in的條件get

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

foreach(循環List<String>參數) - 做爲where中in的條件it

<select id="getStudentListByClassIds_foreach_list" 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="list" item="classIdList"  open="(" separator="," close=")">  
     #{classIdList}  
  </foreach>  
</select>

foreach(循環Map<String,String>參數) - 做爲where中in的條件io

<if test="sorts != null and sorts.size() != 0">
	order by 
	<foreach collection="sorts" separator="," index="key" item="val">
		${key} ${val}
	</foreach>
</if>
相關文章
相關標籤/搜索