Mybatis中的mapper.xml文件学习和使用

Mybatis中的mapper.xml文件学习和使用

本文是基于Windows 10系统环境,进行Mybatis中的mapper.xml的学习和使用

  • Windows 10
  • Mybatis

一、mapper.xml书写规范

(1) select语句

<select id="getUserByMap" parameterType="java.util.Map" resultType="java.util.Map">
        select * from user
        <where>
            1=1
            <if test="parameter != null and parameter.username != null and parameter.username != ''">
                and username like concat('%',#{parameter.username},'%')
            </if>
            <if test="parameter != null and parameter.age != null">
                and age = #{parameter.age}
            </if>
            <if test="filters != null and filters.authority != null">
                and authority in
                <foreach item="item" index="index" collection="filters.authority" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="filters != null and filters.nickname != null">
                and nickname in
                <foreach item="item" index="index" collection="filters.nickname" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <choose>
            <when test="type != null and mobile != null and type=='mobile'" >
                and mobile = #{mobile}
            </when>
            <otherwise>
                and account = #{account}
            </otherwise>
        </choose>
        </where>
        <if test="sorter != null and sorter.field != null and sorter.order != null">
            order by ${sorter.field} ${sorter.order}
        </if>
        <if test="pagination != null and pagination.pageSize != null and pagination.offset != null">
            limit #{pagination.offset},#{pagination.pageSize}
        </if>
    </select>

二、注意事项

(1) order by后面的参数不生效

在mybatis中使用下面的语句,不会生效

select XXXX from table order by #{column} #{order}

mybatis会将上面的语句翻译为

select XXXX from table order by "authority" "desc"

将#{ }改为${ },语句生效

select XXXX from table order by ${column} ${order}

mybatis会将上面的语句翻译为

select XXXX from table order by authority desc

发布了76 篇原创文章 · 获赞 111 · 访问量 128万+

猜你喜欢

转载自blog.csdn.net/qq_32599479/article/details/104410064