MyBatis SQL statement written

A, forEach

interface:

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

grammar:

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

 

Two, concat (fuzzy query)

 

grammar:

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

  

Three, choose (when, otherwise) label

grammar:

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

Four, selectKey (automatically generated by the assignment component object added)

<!-- 插入学生 自动主键-->
<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>

 

Fifth, the parameters for the type 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>

 

Guess you like

Origin www.cnblogs.com/ldl326308/p/10956504.html