Mybatis之mapper中xml语法考

93 阅读1分钟

查询(含表单筛选)

<sql id="selectPostVo">
   select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark  from sys_post
</sql>

查询全部

<select id="selectPostAll" resultMap="SysPostResult">
  <include refid="selectPostVo"/>
</select>

筛选查询

<select id="selectPostList" parameterType="SysPost" resultMap="SysPostResult">
  <include refid="selectPostVo"/>
  <where>
		<if test="postCode != null and postCode != ''">
			AND post_code like concat('%', #{postCode}, '%')
		</if>
		<if test="status != null and status != ''">
			AND status = #{status}
		</if>
		<if test="postName != null and postName != ''">
			AND post_name like concat('%', #{postName}, '%')
		</if>
   </where>
</select>

查看详情

<select id="selectPostById" parameterType="Long" resultMap="SysPostResult">
    <include refid="selectPostVo"/>
    where post_id = #{postId}
</select>

添加

<insert id="insertPost" parameterType="SysPost" useGeneratedKeys="true" keyProperty="postId">
    insert into sys_post(
        <if test="postId != null and postId != 0">post_id,</if>
        <if test="postCode != null and postCode != ''">post_code,</if>
        <if test="postName != null and postName != ''">post_name,</if>
        <if test="postSort != null and postSort != ''">post_sort,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
    )values(
        <if test="postId != null and postId != 0">#{postId},</if>
        <if test="postCode != null and postCode != ''">#{postCode},</if>
        <if test="postName != null and postName != ''">#{postName},</if>
        <if test="postSort != null and postSort != ''">#{postSort},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
    )
</insert>

修改

<update id="updatePost" parameterType="SysPost">
    update sys_post
    <set>
        <if test="postCode != null and postCode != ''">post_code = #{postCode},</if>
        <if test="postName != null and postName != ''">post_name = #{postName},</if>
        <if test="postSort != null and postSort != ''">post_sort = #{postSort},</if>
        <if test="status != null and status != ''">status = #{status},</if>
        <if test="remark != null">remark = #{remark},</if>
        <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
        update_time = sysdate()
    </set>
    where post_id = #{postId}
</update>

删除、批量删除

	<delete id="deletePostById" parameterType="Long">
		delete from sys_post where post_id = #{postId}
	</delete>
	
	<delete id="deletePostByIds" parameterType="Long">
 		delete from sys_post where post_id in
 		<foreach collection="array" item="postId" open="(" separator="," close=")">
 			#{postId}
        </foreach> 
 	</delete>