Fork me on GitHub

关于Mybatis的几个问题

今天在用mybatis开发的时候遇到两个问题,下面一一列出并给出解决方案。

问题一

最开始我设置的实体类中有个字段如isParent为boolean类型,set和get方法是eclispe自动生成的。

    private boolean isParent;

    public boolean isParent() {
        return isParent;
    }

    public void setParent(boolean isParent) {
        this.isParent = isParent;
    }    

在xml中是这么写的

<select id="getChildrenByParentId" parameterType="java.lang.String" resultType="org.sjzcgl.common.data.CommonTree">
        SELECT ID,
               NAME,
               CASE
                 WHEN EXISTS (SELECT 1 FROM TREE_YJS A WHERE A.PARENT_ID = T.ID) THEN
                  '1'
                 ELSE
                  '0'
               END isParent,
               PARENT_ID parentId,
               IS_METADATA_TYPE isMetadataType,
               SFYX
          FROM TREE_YJS T
          WHERE T.SFYX = '1'
            <if test="_parameter != null and _parameter != ''">
                AND T.PARENT_ID = #{0,jdbcType=VARCHAR}
            </if>
            <if test="_parameter == null or _parameter == ''">
                AND T.PARENT_ID IS NULL
            </if>
    </select>

其中CASE WHEN EXISTS (SELECT 1 FROM TREE_YJS A WHERE A.PARENT_ID = T.ID) THEN '1' ELSE '0' END isParent ,对于isParent这个字段无论是返回的是字符串‘1’、‘0‘,还是数字1、0,又或者直接是true、false都不能转换为booean。

解决方案:将isParent修改为String类型,并且修改get方法。

 

    private String isParent;

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
            
    public boolean getIsParent() {
        if(this.isParent.equals("1"))
            return true;
        else
            return false;
    }

 

问题二

还是这个xml,入参是String类型,但是在<if>标签里面(红色部分)不识别。

 

    <select id="getChildrenByParentId" parameterType="java.lang.String" resultType="org.sjzcgl.common.data.CommonTree">
        SELECT ID,
               NAME,
               CASE
                 WHEN EXISTS (SELECT 1 FROM TREE_YJS A WHERE A.PARENT_ID = T.ID) THEN
                  '1'
                 ELSE
                  '0'
               END isParent,
               PARENT_ID parentId,
               IS_METADATA_TYPE isMetadataType,
               SFYX
          FROM TREE_YJS T
          WHERE T.SFYX = '1'
            <if test="parentId != null and parentId != ''">
                AND T.PARENT_ID = #{0,jdbcType=VARCHAR}
            </if>
            <if test="parentId == null or parentId == ''">
                AND T.PARENT_ID IS NULL
            </if>
    </select>

 

解决方案:后来查到原因,在mybatis中String参数只用了一次,就不会报错,如果第二段代码还用来判断了不为空,多次使用的话,它就会报错,经更改之后的代码如下,将参数改为_parameter,这样它就会知道,你一直使用的都是传进来的String。

 

    <select id="getChildrenByParentId" parameterType="java.lang.String" resultType="org.sjzcgl.common.data.CommonTree">
        SELECT ID,
               NAME,
               CASE
                 WHEN EXISTS (SELECT 1 FROM TREE_YJS A WHERE A.PARENT_ID = T.ID) THEN
                  '1'
                 ELSE
                  '0'
               END isParent,
               PARENT_ID parentId,
               IS_METADATA_TYPE isMetadataType,
               SFYX
          FROM TREE_YJS T
          WHERE T.SFYX = '1'
            <if test="_parameter != null and _parameter != ''">
                AND T.PARENT_ID = #{0,jdbcType=VARCHAR}
            </if>
            <if test="_parameter == null or _parameter == ''">
                AND T.PARENT_ID IS NULL
            </if>
    </select>

 

 

 

 

posted @ 2018-04-16 19:56  爱跑步的星仔  阅读(330)  评论(2编辑  收藏  举报