简体   繁体   English

当MyBatis传递多个参数时,如何判断参数属性是否存在?

[英]When MyBatis passes multiple parameters, how to judge whether the parameter attribute exists or not?

When I use Mybatis, the DAO interface specifies that the parameter passed is called "param", which is a POJO with two attributes inside. 当我使用Mybatis时,DAO接口指定传递的参数称为“param”,这是一个内部有两个属性的POJO。 Now I need to make different queries based on the existence of these two attributes. 现在我需要根据这两个属性的存在来进行不同的查询。 I don't know How to do. 我不知道怎么办。

I know that dynamic SQL should be used, but I can only judge whether the POJO exists, and not whether its properties exist, otherwise MyBatis will prompt me that It can't find this property. 我知道应该使用动态SQL,但我只能判断POJO是否存在,而不是它的属性是否存在,否则MyBatis会提示我它找不到这个属性。

/ This is the DAO interface, which specifies the parameter name. / 这是DAO接口,它指定参数名称。 / public List findByPage(@Param("param")T o,Page page); / public List findByPage(@Param(“param”)T o,Page page);

/ This is its corresponding Mapper file, I can only judge whether one attribute exists, but not whether another attribute exists. / 这是它对应的Mapper文件,我只能判断是否存在一个属性,而不能判断是否存在其他属性。 / /

SELECT * FROM ps_jzg j,ps_bm b j.BM_DM = b.DM AND j.BM_DM = #{param.bmDm} SELECT * FROM ps_jzg j,ps_bm b j.BM_DM = b.DM AND j.BM_DM =#{param.bmDm}

Now, this parameter called "param" has two attributes: bmDm and processid. 现在,这个名为“param”的参数有两个属性:bmDm和processid。 I need to determine whether these two parameters exist, and then make different queries. 我需要确定这两个参数是否存在,然后进行不同的查询。 Now I can only judge whether the POJO "param" exists. 现在我只能判断POJO“param”是否存在。 I can't change the DAO interface because it's prescribed by the upper layer.This has been bothering me for a long time. 我无法改变DAO界面,因为它是由上层规定的。这一直困扰着我很长一段时间。 I really need your help. 我真的需要你的帮助。 Thank you. 谢谢。

Is your T a generic param? 你的T是一个通用的参数吗? If not, try this in your xml file. 如果没有,请在xml文件中尝试此操作。

<select id="findByPage" parameterType="com.xx.xx.xx.T">
        select * from  ps_jzg j,ps_bm b
        where j.BM_DM = b.DM 
        <if test = 'bmDm != null'> 
            AND j.BM_DM = #{bmDm}
        </if>
        <if test = 'processid != null'>
            and j.processid = #{processid}
        </if>
</select>

Meanwhile, remove @Param annotation from your interface. 同时,从您的界面中删除@Param注释。

Thank you very much. 非常感谢你。 I have found a solution to the problem: 我找到了解决问题的方法:

<select id="findByPage" resultMap="base"  parameterType="PsJzgXx">
    SELECT * FROM ps_jzg j,ps_bm b
    <where>
        j.BM_DM = b.DM
        <if test="param != null and param !='' ">
            <if test="param.bmDm != null and param.bmDm !='' ">
                AND j.BM_DM = #{param.bmDm}
            </if>
           <if test="param.processid != null and param.processid  !='' ">
                AND j.processid  = #{param.processid }
           </if>
        </if>
</where>
</select>

But if I write it as follows, the compiler will throw an exception,Because it doesn't know to find the parameter "bmDm": 但是如果我按如下方式编写它,编译器将抛出一个异常,因为它不知道找到参数“bmDm”:

<select id="findByPage" resultMap="base"  parameterType="PsJzgXx">
    SELECT * FROM ps_jzg j,ps_bm b
    <where>
        j.BM_DM = b.DM
        <if test="param.bmDm != null and param.bmDm !='' ">
            AND j.BM_DM = #{param.bmDm}
        </if>
        <if test="param.processid != null and param.processid  !='' ">
            AND j.processid  = #{param.processid }
        </if>
        </if>
    </where>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM