简体   繁体   English

如何使用ibatis将类型为java.util.Set的数据插入到我的sql数据库中?

[英]How to insert data of type java.util.Set into my sql database with column of type set using ibatis?

How to insert data of type java.util.Set into mysql db column of type set(mysql set type) ? 如何将类型java.util.Set的数据插入类型为set(mysql set type)的mysql db列中?

My POJO : 我的POJO:

public class UserTEO 
{
    private Integer id;
    private Set changedfieldset;
    //getters and setters
}

xml file : xml文件:

<sqlMap namespace="user"> 

    <typeAlias alias="USER" type="com.howtodoinjava.ibatis.demo.dto.UserTEO" />
    <insert id="addUser" parameterClass="USER">
        INSERT INTO USERINFO (ID,CHANGEDFIELDSET)
         VALUES(#id#,#changedfieldset#);
    </insert>
</sqlMap>

database : 数据库:

CREATE TABLE USERINFO
(
    ID INT,
    CHANGEDFIELDSET SET('')
);

Exception : 例外情况:

com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in user.xml.  
--- The error occurred while applying a parameter map.  
--- Check the user.addUser-InlineParameterMap.  
--- Check the parameter mapping for the 'changedfieldset' property. 

Please help. 请帮忙。 Thanks ! 谢谢 !

I guess you explicitly want to work with (old) ibatis and not Mybatis. 我猜您明确地想使用(旧)ibatis,而不是Mybatis。 So here is the documentation I referenced to. 这是我参考的文档

The Mysql SET expects a string of set values separated by commas and without white spaces: StringUtils.join(set, ",") . Mysql SET期望使用逗号分隔且不带空格的设置值字符串: StringUtils.join(set, ",") So you have to use a type handler to transform the java Set into this string: Extend BaseTypeHandler , speceially overriding setParameter method. 因此,您必须使用类型处理程序将java Set转换为以下字符串:Extend BaseTypeHandler ,特别是覆盖setParameter方法。

Then call as follows: 然后调用如下:

INSERT INTO USERINFO (ID,CHANGEDFIELDSET)
         VALUES(#id#,#changedfieldset,handler=YourCustomTypeHandlerTypeAlias#)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MappingException:无法确定java.util.Set的类型 - MappingException: Type can not be determined for java.util.Set 参数值与预期类型不匹配 java.util.Set - Parameter value did not match expected type java.util.Set 基本的JPA问题:“无法确定类型:java.util.Set” - Basic JPA issue: “Could not determine type for: java.util.Set” 无法确定:java.util.Set的类型,在表中 - Could not determine type for: java.util.Set, at table hibernate MappingException:无法确定类型:java.util.Set - hibernate MappingException: Could not determine type for: java.util.Set Sprint引导数据JPA:没有类型为'java.util.Set <javax.persistence.EntityManager>'的限定bean - Sprint Boot Data JPA: No qualifying bean of type 'java.util.Set<javax.persistence.EntityManager>' available org.hibernate.MappingException:无法确定类型:java.util.Set,在表:Company中,用于列:[org.hibernate.mapping.Column(users)] - org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Company, for columns: [org.hibernate.mapping.Column(users)] 获取 org.hibernate.MappingException:无法确定类型:java.util.Set,在表:使用@ElementCollection 后的作者 - Getting a org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Authors after using @ElementCollection 使用@ElementCollection时出错:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:对于列 - Error while using @ElementCollection: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: for columns 无法从类型 [java.util.LinkedHashSet<!--?--> ] 输入 [java.util.Set<java.lang.long> ]</java.lang.long> - Failed to convert from type [java.util.LinkedHashSet<?>] to type [java.util.Set<java.lang.Long>]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM