本文介绍了如何配置dao文件以处理插入List< String>在MyBatis中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,该对象的变量是这样的List:

I have an object that has a variable that's a List like so:

ExportQueue.java

public class ExportQueue implements Serializable {
    private List<String> errors;

    public List<String> getErrors() {
        return errors;
    }

    public void setErrors(List<String> errors) {
        this.errors = errors;
    }
    public void addError(String error) {
        if(this.errors == null) this.errors = new ArrayList<String>();
        this.errors.add(error);
    }
}

我已经为此定义了ResultMap ...

I've defined the ResultMap for this...

ExportQueueDao.xml

<mapper namespace="...">
    <resultMap id="exportQueueResultMap" type="...ExportQueue">
         <result property="errors" column="errors"
            typeHandler="...CommaSeparatedStringListTypeHandler" />
    </resultMap>
</mapper>

ExportQueueDao.java

@Insert(INSERT_UPDATE)
@Options(useGeneratedKeys = true, keyProperty = "id")
int insertOrUpdate(ExportQueue ExportQueue);

我定义了 CommaSeparatedStringListTypeHandler ,但是尝试插入对象时出现错误.据我了解,INSERT不使用ResultMap,因此看不到TypeHander,因此它不知道如何处理List错误.

I have a CommaSeparatedStringListTypeHandler defined but I'm getting an error when I try to INSERT the object. As far as I understand INSERT doesn't use the ResultMap and therefore doesn't see the TypeHander so it doesn't know what to do with the List errors.

这是我在当前设置下遇到的错误...

This is the error I get with the current set up...

Caused by: org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter errors of statement ....dao.ExportQueueDao.insertOrUpdate

我该如何配置以便MyBatis知道如何使用List<String> errors?

How do I configure this so MyBatis knows what to do with the List<String> errors?

推荐答案

您可以在myBatis-config中定义使用CommaSeparatedStringListTypeHandler作为List类型的默认句柄

You can define in your myBatis-config to use CommaSeparatedStringListTypeHandler as a default handle for the type List

一旦定义了这个,就不必在结果映射中特别提到typeHandler的错误",而在插入MyBatis时,默认情况下将使用CommaSeparatedStringListTypeHandler来处理错误.

Once you define this , you wont have to specially mention in the result map the typeHandler for "errors" also while inserting MyBatis will by default use CommaSeparatedStringListTypeHandler for your errors.

<typeHandlers>
        <typeHandler javaType='List' handler='CommaSeparatedStringListTypeHandler' />
</typeHandlers>

这篇关于如何配置dao文件以处理插入List&lt; String&gt;在MyBatis中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:28