Mybatis Tag SQL

<sql></sql> tags: Extract reusable SQL fragments for later reference

<!--
		Extract reusable SQL fragments for later reference
		1. SQL extraction: often the column names to be queried, or the column names for insertion are extracted for easy reference
		2.include to reference the extracted SQL
		3. Include can also customize some properties, and you can use custom inside SQL tags
			Correct way to get value: ${property}
			 #{This method cannot be used}
	 -->
	<sql id="insertColumn">
		deptno,dname,loc<!-- ,${column1} -->
	</sql>
To save in batches use:
<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>
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809068&siteId=291194637