Mybatis動態sql語句及模糊查詢札記

1 單條件模糊查詢

  • sql中字符串拼接模糊匹配
<select id="getComponentByName" parameterType="String" resultMap="component">
	SELECT * FROM dmp_component WHERE name like CONCAT(CONCAT('%',#{name}),'%')
</select>
  • 忽略大小寫匹配
<select id="getComponentByName" parameterType="String" resultMap="component">
	SELECT * FROM dmp_component where upper(code_name) like CONCAT(CONCAT(‘%’,upper(#{codeName})),’%’)
</select>

這裏稍微岔開話題,Mysql默認的字符檢索策略:utf8_general_ci,表示不區分大小寫;此情況下,當用戶認證系統中若查詢字段是關鍵字時,大小寫可能都能登錄成功,如下語句返回的結果也是一樣。

select * from user where username = 'admin' and password = 'admin';
select * from user where username = 'Admin' and password = 'admin';
select * from user where username = 'ADMIN' and password = 'admin';

因此我們在創建表時,應設置表的collate屬性爲utf8_general_cs或者utf8_bin;如果已經創建表,則直接修改字段的Collation屬性爲utf8_general_cs或者utf8_bin。utf8_general_cs表示區分大小寫,utf8_bin表示二進制比較,同樣也區分大小寫 。(Mysql5.6.10版本不支持utf8_genral_cs)

2 多條件查詢動態sql語句

多條件查詢的要點是判斷查詢條件是否爲空,再拼接sql語句。在mybatis中提供了if標籤和where 標籤。兩種標籤的用法如下:

  • if標籤
    mybatis 中的if標籤有些類似於EL表達式的使用,test中可以直接寫入類中的屬性或者key值。
<select id="selectComponentByNameAndType" resultMap="component"> 
	SELECT 
	* 
	FROM 
	dmp_component 
	WHERE 1=1 
	<if test="name != null and name != ''"> 
		and name = #{name} 
	</if> 
	<if test="type != null and type != ''"> 
		and type = #{type} 
	</if> 
</select> 
  • where標籤
    where標籤 替換了上述代碼中的 WHERE 1=1 。 mybatis中的where 標籤會判斷標籤內是否有內容, 如果有內容則自動生成where 並把 where 後面的第一個and 和一個空格,or和一個空格 去掉。
<select id="selectFIleByNameAndType" resultMap="HdfsFile">
   SELECT 
   * 
   FROM
   dmp_hdfs_file
   <where>
	   <if test="name != null and name != ''">
	   	and name = #{name}
	   </if>
	   <if test="type != null and type != ''">
	   	and type= #{type}
	   </if>
   </where>
  </select>
  • choose , when 和 otherwise 標籤
    當所有條件不滿足時,執行otherwise標籤的內容。
<select id="selectComponentCat" resultMap="componentCategory">
   SELECT 
   * 
   FROM
   dmp_component_category
   <where>
   <choose>
    <when test="category_id != null and category_id != ''">
       and category_id = #{categoryId}
    </when>
    <when test="name != null and name != ''">
       and name = #{name}
    </when>
    <otherwise>
      category = #{category}
    </otherwise>
   </choose>
   </where>
  </select>

choose跟 JAVA 中的if-else(也有說switch)效果差不多,是按照條件的順序進行匹配,當 when 中有條件滿足的時候,就會跳出 choose,即所有的 when 和 otherwise 條件中,只有一個會輸出,當所有的when條件都不滿足時,輸出 otherwise 中的內容。所以上述語句的意思非常簡單。

  • set標籤
    set標籤常用於update操作,並且會自動過濾其他無關字段
 <update id="updateComponent" >
  UPDATE 
  dmp_component
  <set>
    <if test="codeName != null and codeName != ''">
       codeName = #{codeName }
    </if>
    <if test="platform != null and platform != ''">
       , platform = #{platform}
    </if>
  </set>
  WHERE id = #{id} 
  </update>
  • foreach標籤
    foreach 用於處理數組或者list集合,下面是一個批量更新List的例子
<update id="updateModel">
    <foreach separator=";" collection="list" item="meta" index="index">
        update dmp_model
        <set>
           expriment_id=#{meta.exprimentId}
        </set>
           where id =#{meta.id}
    </foreach>
</update>

注,如果運用了set語法,mysql的批量更新是要我們主動去配置數據庫連接的,連接地址需加上&allowMultiQueries=true
如:

type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dmp_experiment?characterEncoding=utf-8&allowMultiQueries=true
username: root
password: 123456

批量添加例子如下:

<insert id="insertParameter">
  INSERT INTO 
  dmp_parameter
  (tab, label, value_name, value_type, value_note, value_data) 
  VALUES
  <foreach collection="list" item="meta" separator=","> 
   (#{meta.tab},#{meta.label},#{meta.valueName},#{meta.valueType},#{meta.valueNote},#{meta.valueData})
  </foreach>
</insert>

若參數爲數組, 則collection只能爲“array” ;參數爲List集合,則collection只能爲 “list” ,item指代集合中的每一個對象,上述寫作meta。separator=”,”的含義是每條數據以 , 分割。 其他動態語句中有屬性 open 和 close, 其含義是指在遍歷開始和結束時分別添加其內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章