Mybatis标签SQL

来源:互联网 发布:手机淘宝txt 编辑:程序博客网 时间:2024/06/08 06:47

<sql></sql>标签:抽取可重复使用的SQL片段,方便后面引用

<!-- 抽取可重复使用的SQL片段,方便后面引用1.SQL抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用2.include来引用已经抽取的SQL3.include还可以自定义一些property,SQL标签内部就能使用自定取值的正确方式:${property} #{不能使用这种方式} --><sql id="insertColumn">deptno,dname,loc<!-- ,${column1} --></sql>
批量保存使用:
<insert id="addDepts" databaseId="oracle"
    parameterType="com.jadeon.mybatis.bean.Dept">
    <!-- Oracle:批量保存方法1  -->
        <!-- <foreach collection="depts" item="dept" open="begin" close="end;">
               insert into dept(deptno,dname,loc)
                   values (dept_seq.nextval,#{dept.dname},#{dept.loc});
        </foreach> -->
    <!-- Oracle:批量保存方法2  -->
    insert into dept(
        <include refid="insertColumn">
            <!-- <property name="column1" value="abc"/> -->
        </include>
        
    )
        select dept_seq.nextval,dname,loc from(
            <foreach collection="depts" item="dept" separator="union" >
                       select #{dept.dname} dname ,#{dept.loc} loc from dual
            </foreach>
        )
    </insert>



原创粉丝点击