1. 程式人生 > 其它 >java讀取本地json檔案

java讀取本地json檔案

一、<if>元素

  <if>元素:判斷語句,用於單條件分支判斷。

    test屬性:對POJO物件的屬性進行非空判斷,如果傳入的查詢條件非空,就進行動態SQL組裝。

<select id="selectEmployee" resultType="employee" parameterType="employee">
        select * from employee where 1=1
        <if test="id != null and id != ''">
            and id = #{id}
        
</if> <if test="name != null and name != ''"> and name = #{name} </if> <if test="salary != null and salary != ''"> and salary = #{salary} </if> </select>

二、<choose>、<when>、<otherwise>元素

  <choose>元素:從多個選擇中選擇一個

    <when>元素:屬性test,判斷傳入的屬性是否非空

    <otherwise>元素:

  注意:<choose>、<when>、<otherwise>元素相當於switch...case....default

<select id="selectEmployee2" resultType="employee" parameterType="employee">
        select * from employee where 1=1
        <choose>
            <
when test="id != null and id != ''"> and id = #{id} </when> <when test="name != null and name != ''"> and name like concat('%',#{name},'%') </when> <otherwise> and salary > #{salary} </otherwise> </choose> </select>

三、<where>、<trim>元素

  替換掉動態SQL查詢時“where 1=1”的條件,衍生出來的<where>、<trim>元素,作用一致

  <where>元素:替代“where 1=1”,相當於where,會自動去除where之後多於的AND和OR

<select id="selectEmployee3" resultType="employee" parameterType="employee">
        select * from employee
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="salary != null">
                and salary > #{salary}
            </if>
        </where>
</select>

  <trim>元素:替代“where 1= 1”,方式區別稍區別於<where>元素

    prefix元素:代表語句的字首,用where連線

    prefixOverrides元素:去除那些多餘特殊的字串,比如and和or

<select id="selectEmployee4" resultType="employee" parameterType="employee">
        select * from employee
        <trim prefix="where" prefixOverrides="AND">
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="salary != null">
                and salary > #{salary}
            </if>
        </trim>
</select>

四、<set>元素

  概述:進行動態的SQL語句更新操作,讓程式更新需要更新的欄位

  主要作用:在動態的SQL語句前面輸出set關鍵字,並將SQL語句中最後一個多餘的逗號去除

  注意:如果<set>元素內包含的內容都為空,就會出現語法錯誤。所以在使用<set>元素進行欄位的資訊更新時,要確保傳入的更新欄位不能都為空

<update id="updateEmployee" parameterType="employee">
        update employee
        <set>
            <if test="name != null and name != ''">
              name = #{name},
          </if>
            <if test="salary != null">
                salary = #{salary},
            </if>
        </set>
        <where>
            id = #{id}
        </where>
</update>

五、<foreach>元素

  <foreach>元素:通常在構建IN條件語句時使用,對傳入的集合或陣列進行遍歷以及動態的SQL組裝。

    collection屬性:集合為list(或collection)、陣列為array

    item屬性:需要遍歷的項

    index屬性:當前元素的下標

    open、close屬性:配置以什麼符號將這些元素包裝起來

    separator屬性:配置各個元素之間的分隔符

<select id="selectEmployee5" resultType="employee" parameterType="List">
        select * from employee where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
</select>
<select id="selectEmployee6" resultType="employee" parameterType="int[]">
        select * from employee where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
 </select>

六、<bind>元素

  <bind>元素:覺接模糊查詢${}帶來的注入問題,concat()函式只使用於MySQL資料庫的問題

    name屬性:定義變數,直接引用該變數就可以進行動態的組裝

    value屬性:進行拼接查詢的字串

<select id="selectEmployee7" resultType="employee" parameterType="employee">
        <bind name="p_name" value="'%'+_parameter.getName()+'%'"/>
        select * from employee where name like ${p_name}
</select>