当我们保存或更新时,是否可以将值从布尔值转换为 "Y" / "N"

is it possible to convert values from boolean to "Y" / "N" when we save or update

我正在使用带有 xml 映射的 mybatis。碰巧的是,在我的数据库中,我必须以字符串格式存储布尔值,例如:"Y"/"N"。在我的 java 代码中,我有很多行,比如 setValue( someBooleanValue ? "Y" : "N" ); 有没有办法存储布尔值,然后所有这些值都转换为 "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