0

主题:MyBatis:布尔参数:MyBatis 正在使用 Getter

内容:

大家好,

我一直在寻找解决我几乎简单的 MyBatis 问题的方法:


给定代码(仅必要部分):

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <!-- Model types. -->
        <typeAlias type="com.blockhaus2000.bh2k.portal.data.model.Navigation" alias="Navigation" />
        <!-- ... more types ... --->
    </typeAliases>
    <mappers>
        <mapper resource="mapper/NavigationDAO.xml" />
        <!-- ... more mappers ... -->
    </mappers>
</configuration>

NavigationDAO.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.blockhaus2000.bh2k.portal.data.dao.NavigationDAO">
    <resultMap type="Navigation" id="navigationResultMap">
        <id property="id" column="navigationId" />
        <result property="title" column="navigationTitle" />
        <result property="order" column="navigationOrder" />
        <result property="visible" column="navigationVisible" />
        <discriminator javaType="String" column="resolve">
            <case value="yes" resultMap="enhancedNavigationResultMap" />
        </discriminator>
    </resultMap>
    <resultMap type="Navigation" id="enhancedNavigationResultMap" extends="navigationResultMap">
        <collection property="navigationEntries" ofType="NavigationEntry">
            <id property="navigationId" column="navigationId" />
            <id property="pageId" column="pageId" />
            <result property="title" column="navigationEntryTitle" />
            <result property="order" column="navigationEntryOrder" />
            <result property="visible" column="navigationEntryVisible" />
        </collection>
    </resultMap>

    <!-- Methods from BaseDAO. -->
    <insert id="insert" parameterType="Navigation" useGeneratedKeys="true" keyProperty="id" keyColumn="navigationId">
        <!-- SQL -->
    </insert>
    <insert id="save" parameterType="Navigation">
        <!-- SQL -->
    </insert>
    <delete id="delete" parameterType="Navigation">
        <!-- SQL -->
    </delete>
    <select id="exists" parameterType="Navigation" resultType="boolean">
        <!-- SQL -->
    </select>

    <!-- Methods from UpdateableDAO. -->
    <update id="update" parameterType="Navigation">
        <!-- SQL -->
    </update>

    <!-- Methods from NavigationDAO. -->
    <select id="findVisibleNavigations" parameterType="boolean" resultMap="navigationResultMap">
        SELECT
            N.navigationId,
            N.navigationTitle,
            N.navigationOrder,
            N.navigationVisible,
        <if test="resolve">
            NE.pageId,
            NE.navigationEntryTitle,
            NE.NavigationEntryOrder,
            NE.navigationEntryVisible,
        </if>
            IF (
                #{resolve} = 'true',
                'yes',
                'no'
            ) AS resolve
        FROM
            Navigation AS N
        <if test="resolve">
        INNER JOIN NavigationEntry AS NE ON N.navigationId = NE.navigationId
        </if>
        WHERE
            N.navigationVisible = TRUE
        <if test="resolve">
            AND
            NE.navigationEntryVisible = TRUE
            AND
            NE.navigationEntryTitle IS NOT NULL
        </if>
    </select>
    <select id="findHiddenNavigations" parameterType="boolean" resultMap="navigationResultMap">
        <!-- SQL -->
    </select>
    <select id="findNavigation" parameterType="Map" resultMap="navigationResultMap">
        <!-- SQL -->
    </select>
</mapper>

NavigationDAO.java:

package com.blockhaus2000.bh2k.portal.data.dao;

import java.util.SortedSet;

import com.blockhaus2000.bh2k.portal.data.model.Navigation;

public interface NavigationDAO extends BaseDAO<Navigation>, UpdateableDAO<Navigation> {
    SortedSet<Navigation> findVisibleNavigations(final boolean resolve);

    SortedSet<Navigation> findHiddenNavigations(final boolean resolve);

    Navigation findNavigation(final int id, final boolean resolve);
}

BaseDAO.java:

package com.blockhaus2000.bh2k.portal.data.dao;

import com.blockhaus2000.bh2k.portal.data.model.BaseModel;

public interface BaseDAO<T extends BaseModel<T>> {
    int insert(final T obj);

    int save(final T obj);

    int delete(final T obj);

    boolean exists(final T obj);
}

可更新DAO.java:

package com.blockhaus2000.bh2k.portal.data.dao;

import com.blockhaus2000.bh2k.portal.data.model.BaseModel;

public interface UpdateableDAO<T extends BaseModel<T>> extends BaseDAO<T> {
    int update(final T obj);
}

一切都正确开始,但是在调用

NavigationDAO.findVisibleNavigations(true) // Or false.

抛出以下异常:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'resolve' in 'class java.lang.Boolean'
    at org.mybatis.(...)
    at org.apache.ibatis.(...)
    at com.blockhaus2000.bh2k.portal.data.test.NavigationDAOSpec.test findVisibleNavigations(NavigationDAOSpec.groovy:224)
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'resolve' in 'class java.lang.Boolean'
    at org.apache.ibatis.(...)
    at org.mybatis.(...)
    ... 5 more

也就是说,解决方案可能是将“parameterType”更改为“Map”并将“resolve”替换为“param1”。但这也不起作用。另外,如果我删除

<if test="..."> ... </if>

它确实执行正确(当然,功能除外)。


我对每一个答案都很满意。

感谢您的帮助


编辑1

这是我的“导航”数据模型:

package com.blockhaus2000.bh2k.portal.data.model;

import java.util.SortedSet;

public class Navigation implements BaseModel<Navigation>, Comparable<Navigation> {
    private static final long serialVersionUID = 4019792023891297733L;

    /**
     * <p>
     * <b> PRIMARY KEY </b>
     * <br>
     * <b> AUTO_INCREMENT </b>
     * </p>
     */
    private int id = -1;
    private String title;
    private int order;
    private boolean visible;

    // ~ non-persistent fields ~

    private transient SortedSet<NavigationEntry> navigationEntries;

    // compateTo()
    // toString()
    // hashCode()
    // equals(Object)

    public int getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    public int getOrder() {
        return this.order;
    }

    public boolean isVisible() {
        return this.visible;
    }

    public SortedSet<NavigationEntry> getNavigationEntries() {
        return this.navigationEntries;
    }

    public Navigation setId(final int id) {
        this.id = id;

        return this;
    }

    public Navigation setTitle(final String title) {
        this.title = title;

        return this;
    }

    public Navigation setOrder(final int order) {
        this.order = order;

        return this;
    }

    public Navigation setVisible(final boolean visible) {
        this.visible = visible;

        return this;
    }

    public Navigation setNavigationEntries(final SortedSet<NavigationEntry> navigationEntries) {
        this.navigationEntries = navigationEntries;

        return this;
    }
}

我忘了解释“resolve”的作用:它是一个简单的布尔值,用于识别是否加载瞬态属性。

4

3 回答 3

1

在您的示例中,MyBatis 尝试调用getResolve()布尔参数(这显然无法正常工作),因为您的 Java 方法声明中的形式参数名称并未全部使用,因此您在 xml 中的“resolve”与参数名称“resolve”不匹配.

resolve在NavigationDAO.xml中为参数指定 MyBatis 名称parameterType="map"

...
<select id="findVisibleNavigations" parameterType="map" resultMap="navigationResultMap">
...
<select id="findHiddenNavigations" parameterType="map" resultMap="navigationResultMap">

并在 NavigationDAO.java 中注释您的参数@Param("resolve"),也许您必须删除 final,但我不确定:

....
SortedSet<Navigation> findVisibleNavigations(@Param("resolve") final boolean resolve);
SortedSet<Navigation> findHiddenNavigations(@Param("resolve") final boolean resolve);
...

更新1:

根据这个描述,你可能会留下 parameterType ,但我从来没有使用过。

于 2016-02-19T07:08:04.007 回答
0

您可以将#{resolve}替换为#{1},或使用注释@Param("resolve")

于 2019-07-05T08:57:28.663 回答
0

您应该只parameterType="boolean"从 select 语句中删除。然后 Mybatis 将使用实际的布尔值作为参数#{resolve}(或任何名称)。指定 parameterType 强制 Mybatis 将属性查找resolve到参数中。它实际上只对复杂类型是必需的。

或者,通过在映射器界面中使用注释来命名参数@Param("resolve")也可以完成这项工作。

于 2018-09-11T11:57:43.483 回答