关于Mybatis的几个问题

Posted caozx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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>

 

解决方案:将标红部分改为_parameter

 

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

 

 

 

 

以上是关于关于Mybatis的几个问题的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis关于复杂的SQL查询的处理&Mybatis的缓存机制

关于代码片段的时间复杂度

关于片段生命周期

mybatis学习(39):动态sql片段

mybatis动态sql片段与分页,排序,传参的使用

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper