简体   繁体   English

Spring Data JDBC 中的字段特定转换器

[英]Field specific converter in Spring Data JDBC

I want to write a custom converter in Spring Data JDBC like this -我想像这样在 Spring Data JDBC 中编写一个自定义转换器 -

@ReadingConverter
public class IntegerToStringConverter implements Converter<Integer, String> {

    @Override
    public String convert(Integer source) {
         return source != null && source == 0 ? "ABC" : "XYZ";
    }
}

I want this conversion to be applied only on a few fields of the response as in all Integers should not be converted to String using the above reading converter rule?我希望此转换仅应用于响应的几个字段,因为在所有整数中不应使用上述读取转换器规则将其转换为字符串? Spring Data JDBC Documentation doesn't mention any restricted conversion example Spring Data JDBC 文档没有提到任何受限制的转换示例

You currently cannot define field specific converters in Spring Data (JDBC or otherwise).您目前无法在 Spring Data(JDBC 或其他)中定义特定于字段的转换器。

But if you replace your String by a custom class in your domain model you can just register an appropriate converter and make your model more expressive as well, since it now would be obvious that the field doesn't accept an arbitrary String .但是,如果您用域模型中的自定义类替换 String ,您只需注册一个适当的转换器并使您的模型也更具表现力,因为现在很明显该字段不接受任意String

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM