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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值