[英]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.