0

When I use Mybatis, the DAO interface specifies that the parameter passed is called "param", which is a POJO with two attributes inside. 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.

/This is the DAO interface, which specifies the parameter name./ 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./

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. I need to determine whether these two parameters exist, and then make different queries. Now I can only judge whether the POJO "param" exists. I can't change the DAO interface because it's prescribed by the upper layer.This has been bothering me for a long time. I really need your help. Thank you.

孙宇涵
  • 9
  • 2
  • Like `` you mean? – ave Jun 18 '19 at 06:42
  • You need to clarify what do you mean by `parameters exist`. Do I understand you correctly that `T` may be a type that has `processId` property or a type that does not have such property and you want to check if property exists (and not if the property is `null`)? – Roman-Stop RU aggression in UA Jun 18 '19 at 11:03
  • Thank you for your help. I have found a solution to the problem. I wrote it in my answer. Thank you. – 孙宇涵 Jun 19 '19 at 08:09

2 Answers2

0

Is your T a generic param? If not, try this in your xml file.

<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.

chaos
  • 1,359
  • 2
  • 13
  • 25
0

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":

<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>
孙宇涵
  • 9
  • 2