Mybatis常用sql
阿新 • • 發佈:2018-12-02
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>