我正在使用带有 xml 映射的 mybatis。碰巧,在我的数据库中,我必须以字符串格式存储 boolean 值,例如:“Y”/“N”。在我的java代码中,我有很多行,例如 setValue( someBooleanValue ? "Y": "N");
有没有办法存储 boolean 值,然后所有这些值转换为“Y” :“N”?也许我可以定义一个方法或函数来进行转换?
有任何想法吗?
谢谢
最佳答案
您需要使用TypeHandlers为此。
Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.
You can override the type handlers or create your own to deal with unsupported or non-standard types. To do so, implement the interface org.apache.ibatis.type.TypeHandler or extend the convenience class org.apache.ibatis.type.BaseTypeHandler and optionally map it to a JDBC type.
可以找到更完整的示例 here .
关于java - 当我们保存或更新时,是否可以将值从 boolean 值转换为 "Y"/"N",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35717364/