1、XML式微,注解日隆

自从mybatis将注解当做亮点推出的时候,人们逐渐认识到,相对于xml文件来说,mybatis注解更加的便捷,从而导致XML式微,注解日隆。即便注解功能日益强大,它也很难具备SQL片段这般强大的去重利器。

2、<sql>标签简介

<sql>是一个与<insert>,<select>等增删改查同级别的标签,作用是抽取可重用的sql片段,避免重复。

2.1、创建动态SQL片段

<sql id="sqlCount">select count(*)</sql>

2.2、使用SQL片段

在使用sql片段时使用include标签通过sql片段的id进行引用,sql片段的ID在当前空间必须为唯一的

<select id="selectCountByParam" parameterType="map" resultType="int">

    <include refid="sqlCount"/> from table_name

</select>

2.3、<include>动态功能

<include>中还可以使用<property> 标签往sql中加入字段

<sql id="selectColumn">
    id,name,email,${otherColomn}
</sql>

<select id="selectCountByParam" parameterType="map" resultType="int">

     <!-- 引用外部定义的sql -->
    <include refid="selectColumn">
        <property name="otherColomn" value="address"/>
    </include> from table_name

</select>

标签: none

[网站公告]-[2024年兼职介绍]


添加新评论