[英]How to use 'EnumOrdinalTypeHandler' with mybatis's typeHandler in Spring's xml?
I am using mybatis with spring. 我在春天使用mybatis。 I want to store Enum type with int value(which called 'ordinal') in mysql.There is a simple way in mybatis:
我想在MySQL中存储带有int值的Enum类型(称为'ordinal')。mybatis有一个简单的方法:
<!-- mybatis-config.xml -->
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.stackoverflow.MyEnumType"/>
</typeHandlers>
So when excute select or insert sql,the auto-mapper will automatically use EnumOrdinalTypeHandler. 因此,当执行选择或插入sql时,自动映射器将自动使用EnumOrdinalTypeHandler。
My question is: 我的问题是:
1:How to use this in Spring's context.xml like this or any other way: 1:如何像这样或其他方式在Spring的context.xml中使用它:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeHandlers">
...
</property>
</bean>
2:How can I configure for all enum types with less code such as 'component-scan'
or 'MapperScannerConfigurer'
,no need to list every enum type? 2:如何用较少的代码(例如
'component-scan'
或'MapperScannerConfigurer'
为所有枚举类型进行配置,而无需列出每种枚举类型?
1.Set your mybatis-config.xml
location via: 1.通过以下方式设置
mybatis-config.xml
位置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="mybatis-config.xml" />
</bean>
2.If you using java config, try using a loop: 2.如果使用java config,请尝试使用循环:
TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getObject().getConfiguration().getTypeHandlerRegistry();
for (Class<?> cls: EnumClassArrays) {
typeHandlerRegistry.register(cls, new EnumOrdinalTypeHandler<>(cls))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.