select a.ID as id, a.SHOP_ID as shopId, a.LARGE_AREA_ID as largeId, a.SMALL_AREA_ID as smallId, a.COMPANY_ID as companyId, b.SHOP_NAME as shopName from yysport_dev.shop_offline_info as a left join yysport_dev.shop as b on b.ID = a.SHOP_ID where 1=1 and (a.LARGE_AREA_ID = 1 and a.SMALL_AREA_ID = 15 and a.COMPANY_ID =14) OR (a.LARGE_AREA_ID = 1 and a.SMALL_AREA_ID = 17 and a.COMPANY_ID =1)
在MyBatis中怎么自定义后面这样的条件语句
注意:OR后面可能还会继续拼加
<select id="selectShop" parameterType="java.util.Map" resultType="java.util.Map"> select a.ID as id, a.SHOP_ID as shopId, a.LARGE_AREA_ID as largeId, a.SMALL_AREA_ID as smallId, a.COMPANY_ID as companyId, b.SHOP_NAME as shopName from yysport_dev.shop_offline_info as a left join yysport_dev.shop as b on b.ID = a.SHOP_ID where 1=1 ?????????????????? <if test="paginator != null and paginator.offset != null and paginator.itemsPerPage != null"> LIMIT #{paginator.offset,jdbcType=INTEGER},#{paginator.itemsPerPage,jdbcType=INTEGER} </if> </select>
?PredicateSql? 使用这种写法,会原样传递的,这样你就可以拼好条件,传递给mybatis了。
?PredicateSql?,不知道Mybatis.net有没有类似的写法?
@晓菜鸟: 就是mybatis的用法哇,参数化是#param#,原样替换是?param?
用mybatis提供的trim和foreach貌似比较复杂,所以直接自己写sql语句作为参数传给mapper,然后执行,简单操作步骤:
<select id="queryShop" parameterType="com.yysports.util.SQLAdapter" resultType="java.util.Map"> ${sql} </select>
自定义一个sql适配器,将sql塞入,适配器作为参数传入如上的xml,
适配器定义:
package com.yysports.util; import java.io.Serializable; /** * sql适配器 * SQLAdapter * * ****** * 2015年8月26日-下午1:38:03 * * @version 1.0.0 * */ public class SQLAdapter implements Serializable{ /** * @since 1.0.0 */ private static final long serialVersionUID = 1L; private String sql; public String getSql() { return sql; } public void setSql(String sql) { this.sql = sql; } }
必须要实现序列化,不然mybatis无法识别该参数
(自己提问完,网上搜的答案,不知道直接配置xml有没有办法解决这种传递参数组的情况)
完!谢谢@幻天芒 的答案 虽然没看懂
不知道Mybatis.net能不能这样做?