首页 新闻 搜索 专区 学院

Mybatis自定义动态sql

0
悬赏园豆:50 [已关闭问题] 关闭于 2016-01-13 17:17
        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>
VinceChueng的主页 VinceChueng | 初学一级 | 园豆:81
提问于:2015-08-26 13:22
< >
分享
所有回答(4)
0

?PredicateSql?   使用这种写法,会原样传递的,这样你就可以拼好条件,传递给mybatis了。

幻天芒 | 园豆:36652 (高人七级) | 2015-08-26 13:48

?PredicateSql?,不知道Mybatis.net有没有类似的写法?

支持(0) 反对(0) 晓菜鸟 | 园豆:2594 (老鸟四级) | 2015-09-02 14:49

@晓菜鸟: 就是mybatis的用法哇,参数化是#param#,原样替换是?param?

支持(0) 反对(0) 幻天芒 | 园豆:36652 (高人七级) | 2015-09-02 17:02
0

用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有没有办法解决这种传递参数组的情况)

完!谢谢@幻天芒 的答案 虽然没看懂

VinceChueng | 园豆:81 (初学一级) | 2015-08-26 14:41
0

不知道Mybatis.net能不能这样做?

晓菜鸟 | 园豆:2594 (老鸟四级) | 2015-09-02 14:49
0

MyBatis开发WEB插件,即插即用,很震撼。

http://wiki.osgi.help/pages/viewpage.action?pageId=22578029

wmzsoft | 园豆:210 (菜鸟二级) | 2016-03-05 20:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册