1. 程式人生 > >Mybatis常用sql

Mybatis常用sql

mybatis-generator能自動生成簡單的CRUD操作,而條件及多表查詢需自定義,下述為常用的SQL操作的mybatis示例

合併結果集

  • UNION 操作符用於合併兩個或多個 SELECT 語句的結果集。
  • UNION 操作符選取不同的值。如果允許重複的值,請使用 UNION ALL

DAO

List<City> selectCity(List<String> wordList);

Mapper

<select id="selectCity" resultType="com.test.City">
<foreach collection="list" index="index" item="word"> <if test="index > 0"> UNION ALL </if> SELECT id, city, province FROM hanzilib WHERE city = #{word} OR province = #{word} </foreach> </select>

IN 操作符常量拼接

  • IN 操作符允許我們在 WHERE
    子句中規定多個值。

DAO

List<City> selectCity(List<String> wordList);

Mapper

<select id="selectCity" resultType="com.test.City">
    SELECT
    <include refid="Common_Column_List"/>
    FROM test
    WHERE
    id IN
    <foreach item="item" collection="list" separator=","
open="(" close=")" index="">
#{item} </foreach> </select>