最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis常用標(biāo)簽整理

 更新時(shí)間:2024年04月01日 16:41:02   作者:小小野豬  
日常開(kāi)發(fā)中,MyBatis中標(biāo)簽有著舉足輕重的重要性,以下是一些MyBatis框架中常見(jiàn)的標(biāo)簽及案例,感興趣的朋友跟隨小編一起看看吧

日常開(kāi)發(fā)中,MyBatis中標(biāo)簽有著舉足輕重的重要性。以下是一些MyBatis框架中常見(jiàn)的標(biāo)簽及案例:

1. <select> 標(biāo)簽

<!-- 查詢(xún)語(yǔ)句:resultType="com.example.User" -->
<select id="selectUserById" parameterType="int" resultType="com.example.User">
    SELECT * FROM T_Users WHERE id = #{id}
</select>
<!-- 查詢(xún)語(yǔ)句:resultType="java.lang.String -->
<select id="queryAliyunPictureName" resultType="java.lang.String">
   SELECT * FROM T_User WHERE USER_ID=#{userId,jdbcType=VARCHAR}
</select>
<!-- 查詢(xún)語(yǔ)句:parameterType="long" -->
<select id="queryPrizeNum" parameterType="long" resultType="java.lang.Integer">
    SELECT count(*)  FROM T_User_RECORD where user_id= #{userId,jdbcType=BIGINT}
</select>
<!-- 查詢(xún)語(yǔ)句:resultMap="templateMap" -->
<select id="selectUserById" parameterType="int" resultMap="templateMap">
    SELECT * FROM T_Users WHERE id = #{id}
</select>
<!-- 查詢(xún)語(yǔ)句:resultType="java.lang.Integer -->
<select id="queryAllUserNum" resultType="java.lang.Integer">
    SELECT count(*)  FROM T_USER_RECORD
</select>
<!-- 查詢(xún)語(yǔ)句:添加<if> 標(biāo)簽,union語(yǔ)法,foreach語(yǔ)法 -->
<select id="queryUser" parameterType="com.example.UserVO" resultType="com.example.User">
    SELECT USER_ID,USER_NAME,DEPT_CODE,DEPT_NAME 
    FROM
    T_USER
    where STATUS = 'Y'
    <if test="(flag_column== 'N'.toString()) ">
        AND dept_leader = 'Y'
    </if>
    UNION
    SELECT USER_ID,USER_NAME,DEPT_CODE,DEPT_NAME 
    FROM
    T_USER
    where STATUS = 'Y'
    <if test="list!= null and list.size > 0">
        AND
        <foreach collection="list" index="index" item="item" open="(" separator="OR" close=")">
            T.USER_ID =  #{item,jdbcType=VARCHAR}
        </foreach>
    </if>) 
</select>
<!-- 執(zhí)行存儲(chǔ)過(guò)程 -->
<select id="callPprocedure" parameterType="String" useCache="false" statementType="CALLABLE">
   <![CDATA[
    {call P_PROCEDURE(
    #{name,mode=IN,jdbcType=VARCHAR},
    #{age,mode=OUT,jdbcType=VARCHAR}
    )}
    ]]>
</select>

解釋?zhuān)哼@是用于執(zhí)行SQL查詢(xún)的最基本標(biāo)簽,根據(jù)傳入的id參數(shù)從users表中檢索用戶(hù)信息。

2. <insert> 標(biāo)簽

<!-- 新增user -->
<insert id="insertUser" parameterType="com.example.User">
    INSERT INTO users(name, email, created_at)
    VALUES (#{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, SYSDATE)
</insert>
<!-- 從序列中獲取key -->
<insert id="addUser" parameterType="com.example.User">
    <selectKey   keyProperty="id" resultType="long" order="BEFORE" >
        SELECT SEQ_USER.NEXTVAL FROM DUAL
    </selectKey>
    INSERT INTO T_USER(ID,NAME,EMAIL,CREATE_TIME)
    VALUES(#{id,jdbcType=BIGINT},#{name,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR},SYSDATE)
</insert>
<!-- MERGE INTO語(yǔ)句 -->
<insert id="addOrUpdateUser" parameterType="com.example.User">
	    MERGE INTO T_USER_WISHS W
	    USING (SELECT
	                #{id,jdbcType=BIGINT} ID,
	                #{name,jdbcType=VARCHAR} name,
	            FROM DUAL ) D
	    ON (W.ID = D.ID)
	    WHEN MATCHED THEN UPDATE SET
	        W.NAME= D.NAME,
	        W.CREATE_TIME = D.CREATE_TIME,
	        W.UPDATE_TIME = SYSDATE
	    WHEN NOT MATCHED THEN
	    INSERT  (
	        W.ID,
	        W.NAME,
	        W.CREATE_TIME
	    )
	    VALUES(
	        D.ID,
	        D.NAME,
	        SYSDATE
	    )
</insert>   
<!-- 批量新增 -->
<insert id="addSUserList" parameterType="java.util.List">
    INSERT INTO
    T_USER(ID,USER_ID,USER_NAME,CREATE_TIME)
    SELECT SEQ_USER.NEXTVAL,T.* FROM (
    <foreach collection="list" item="item" index="index" separator="union all">
        SELECT
        #{item.userId,jdbcType=VARCHAR} USER_ID,
        #{item.userName,jdbcType=VARCHAR} USER_NAME,
        SYSDATE CREATE_TIME        
        FROM DUAL
    </foreach>) T
</insert> 
<!-- 批量merge into -->
<insert id="batchInsertUser">
    MERGE INTO T_USER_SCORE S
    USING (
    <foreach collection="list" item="item" index="index" separator="UNION ALL">
        SELECT
        #{item.userId,jdbcType=VARCHAR} USER_ID,
        #{item.userName,jdbcType=VARCHAR} USER_NAME,
        #{item.score,jdbcType=VARCHAR} SCORE
        from dual
    </foreach>
    ) D ON (S.STAFF_ID = D.STAFF_ID AND S.USER_NAME= D.USER_NAME)
    WHEN MATCHED THEN UPDATE SET
    S.SCORE = D.SCORE,
    S.UPDATE_TIME = SYSDATE
    WHEN NOT MATCHED THEN
    INSERT (
    S.ID,
    S.USER_ID,
    S.USER_NAME,
    S.SCORE,
    S.CREATE_TIME)
    VALUES(
    SEQ_USER_SCORE.NEXTVAL,
    D.USER_ID,
    D.USER_NAME,
    D.SCORE,
    SYSDATE)
</insert>

3. <update> 標(biāo)簽

<!-- 更新user -->
<update id="updateUser" parameterType="com.example.User">
    UPDATE users
    <set>
        name = #{name},
        email = #{email}
    </set>
    WHERE id = #{id}
</update>
<!-- 更新user:使用<if> -->
<update id="updUserByUserId">
    UPDATE T_USER_INFO
    SET AGE= #{age,jdbcType=VARCHAR},
    <if test="finishFlag != null">
        FINISH_FLAG = #{age,jdbcType=VARCHAR},
    </if>
    STATUS = #{status,jdbcType=VARCHAR}
    WHERE USER_ID = #{userId,jdbcType=VARCHAR}
</update>
<!-- 更新user:在where后面使用<if> -->
<update id="updateProtocalFlagExt" parameterType="java.lang.String">
    UPDATE T_USER 
    SET 
        UPDATE_TIME = SYSDATE,
    WHERE USER_FLAG='Y'
    <if test="isFr != null and isFr== 'Y'.toString()">
        AND USER_ID = #{userId,jdbcType=VARCHAR}
    </if>
</update>
<!-- 更新user:使用set標(biāo)簽 -->
<update id="updateUser" parameterType="com.example.User">
    UPDATE T_USER
    <set>
        <if test="status != null and status != ''">
            STATUS = #{status,jdbcType=VARCHAR},
        </if>
        UPDATE_DATE = SYSDATE
    </set>
    WHERE USER_ID = #{userId}
</update>
<!-- 批量更新user -->
<update id="updateUser" >
       <foreach collection="list" item="item" open="begin" close=";end;" separator=";" index="index">
           UPDATE T_USER
               SET age= #{item.age, jdbcType=VARCHAR}
           WHERE USER_ID = #{item.userId, jdbcType=VARCHAR}
       </foreach>
</update>
<!-- 批量更新user -->
<update id="updateUser">
    <foreach collection="items" item="item" separator=";" index="index">
        UPDATE ${item.tablesName} SET ${item.columnsName} = #{item.age}
        WHERE ${item.columnsUser} = #{item.userId}
    </foreach>
</update>

解釋?zhuān)焊鶕?jù)id更新用戶(hù)記錄,只有nameemail非空時(shí)才會(huì)更新相應(yīng)的字段。

4. <delete> 標(biāo)簽

<!-- 刪除user -->
<delete id="deleteUser" parameterType="int">
    DELETE FROM users WHERE id = #{id}
</delete>

解釋?zhuān)焊鶕?jù)提供的id刪除users表中的某條記錄。

5. 動(dòng)態(tài)SQL標(biāo)簽

a. <if> 標(biāo)簽

<update id="conditionalUpdate">
    UPDATE users
    <set>
        <if test="name != null">name = #{name},</if>
        <if test="email != null">email = #{email}</if>
    </set>
    WHERE id = #{id}
</update>

解釋?zhuān)簝H當(dāng)傳入的參數(shù)nameemail不為null時(shí),才會(huì)更新相應(yīng)的字段。

b. <choose>, <when>, <otherwise> 標(biāo)簽

<update id="chooseUpdate">
    UPDATE users
    <set>
        <choose>
            <when test="operation == 'updateName'">
                name = #{newName}
            </when>
            <when test="operation == 'updateEmail'">
                email = #{newEmail}
            </when>
            <otherwise>
                status = #{newStatus}
            </otherwise>
        </choose>
    </set>
    WHERE id = #{userId}
</update>

解釋?zhuān)焊鶕?jù)傳入的操作類(lèi)型(operation)選擇更新不同的字段。

c. <where> 標(biāo)簽

<update id="complexUpdate">
    UPDATE users
    <set>
        last_login = NOW()
    </set>
    <where>
        <if test="id != null">id = #{id}</if>
        <if test="email != null">AND email = #{email}</if>
    </where>
</update>

解釋?zhuān)?code><where>標(biāo)簽確保生成的SQL中WHERE條件句語(yǔ)法正確,即使條件為空也不會(huì)出現(xiàn)多余的AND

d. <foreach> 標(biāo)簽(循環(huán)遍歷集合)

<insert id="batchInsertUsers">
    INSERT INTO users(id, name)
    VALUES
    <foreach item="item" index="index" collection="list" separator=",">
        (#{item.id}, #{item.name})
    </foreach>
</insert>

解釋?zhuān)号坎迦胗脩?hù),list參數(shù)是一個(gè)包含多個(gè)用戶(hù)對(duì)象的集合。

6. 引用其他SQL片段

<sql id="userColumns">id, name, email</sql>
<select id="selectAllUsers" resultType="com.example.User">
    SELECT 
    <include refid="userColumns"/>
    FROM users
</select>

解釋?zhuān)憾x可重用的SQL片段并通過(guò)<include>標(biāo)簽引入。

7. <resultMap> 標(biāo)簽

<resultMap id="userResultMap" type="com.example.User">
    <id property="id" column="user_id"/>
    <result property="name" column="username"/>
    <association property="address" javaType="com.example.Address">
        <id property="addressId" column="address_id"/>
        <result property="street" column="street"/>
    </association>
</resultMap>

解釋?zhuān)河糜谟成洳樵?xún)結(jié)果到Java對(duì)象,支持一對(duì)一、一對(duì)多等復(fù)雜映射關(guān)系。

       上面整理了MyBatis中一些非常實(shí)用和常用的標(biāo)簽。希望能幫助大家。

聲明

本內(nèi)容版權(quán)歸屬于CSDN-小小野豬,任何未經(jīng)授權(quán)的復(fù)制、轉(zhuǎn)載、傳播、販賣(mài)、轉(zhuǎn)贈(zèng)等均屬違法行為,必將追究法律責(zé)任?。?!

到此這篇關(guān)于Mybatis常用標(biāo)簽整理的文章就介紹到這了,更多相關(guān)Mybatis常用標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

阳原县| 登封市| 章丘市| 安吉县| 万全县| 台东市| 贵港市| 凤凰县| 横峰县| 逊克县| 榆中县| 天门市| 上高县| 饶阳县| 裕民县| 浮山县| 龙山县| 云南省| 成武县| 双峰县| 白玉县| 刚察县| 望奎县| 洞口县| 常熟市| 蛟河市| 新余市| 垫江县| 合水县| 淳安县| 建始县| 亳州市| 芜湖市| 普陀区| 酒泉市| 石景山区| 临泉县| 永嘉县| 深水埗区| 上杭县| 伊吾县|