springMVC與MyBatis中繫結列舉型別資料的轉換
阿新 • • 發佈:2019-01-29
最近專案中遇到這樣一個:後臺Controller引數是一個物件,物件裡面有個列舉型別的屬性,前臺提交過來的資料裡面這個列舉該怎麼接收呢,如何寫進資料庫?
資料庫用的MySQL,
該列舉欄位為:
實際儲存為:
解決方案如下:
實體類:
列舉類:
set與get方法省略……
接下來是springMVC 中列舉的轉換類(Converter)
public class StringToEnumConverter implements ConverterFactory<String,ExceptionTypeEnum> {
@Override
public <T extends ExceptionTypeEnum> Converter<String, T> getConverter(Class<T> aClass) {
return new StringToEnum(aClass);
}
private class StringToEnum<T extends Enum> implements Converter<String, T> {
private final Class<T> enumType;
public StringToEnum(Class<T> enumType) {
this.enumType = enumType;
}
public T convert(String source) {
if (source.length() == 0) {
return null;
}
return (T) Enum.valueOf(this.enumType, source.trim());
}
}
}
spring配置:
<!--自定義列舉類封裝 -->
<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<beans:property name="converters">
<beans:set>
<beans:bean class="com.breadtree.management.vo.StringToEnumConverter" />
</beans:set>
</beans:property>
</beans:bean>
<!--另外這裡加上,記得一定要加上-->
<mvc:annotation-driven conversion-service="conversionService"/>
Mybatis自定義轉換型別:
public class EnumKeyTypeHandler extends BaseTypeHandler<ExceptionTypeEnum>{
private Class<ExceptionTypeEnum> type;
private final ExceptionTypeEnum[] enums;
/**
* 設定配置檔案設定的轉換類以及列舉類內容,供其他方法更便捷高效的實現
* @param type 配置檔案中設定的轉換類
*/
public EnumKeyTypeHandler(Class<ExceptionTypeEnum> type) {
if (type == null)
throw new IllegalArgumentException("Type argument cannot be null");
this.type = type;
this.enums = type.getEnumConstants();
if (this.enums == null)
throw new IllegalArgumentException(type.getSimpleName()
+ " does not represent an enum type.");
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, ExceptionTypeEnum parameter, JdbcType jdbcType) throws SQLException {
// baseTypeHandler已經幫我們做了parameter的null判斷
ps.setInt(i, parameter.getIndex());
}
@Override
public ExceptionTypeEnum getNullableResult(ResultSet rs, String s) throws SQLException {
return convert(rs.getInt(s));
}
@Override
public ExceptionTypeEnum getNullableResult(ResultSet rs, int i) throws SQLException {
return convert(rs.getInt(i));
}
@Override
public ExceptionTypeEnum getNullableResult(CallableStatement cs, int i) throws SQLException {
return convert(cs.getInt(i));
}
private ExceptionTypeEnum convert(int status) {
ExceptionTypeEnum[] objs = type.getEnumConstants();
for (ExceptionTypeEnum em : objs) {
if (em.getIndex() == status) {
return em;
}
}
return null;
}
mapper.xml裡配置如下:
<resultMap id="BaseExcResultMap" type="XXX.XX.ExceptionEntity">
……省略其他屬性配置
<result column="exception_type" jdbcType="VARCHAR" property="exception_type" typeHandler="com.breadtree.management.vo.EnumKeyTypeHandler" />
……省略其他屬性配置
以上配置完,查詢基本就沒問題了,頁面效果如下,已經正常顯示列舉的VALUE值了:
對應的儲存資料庫欄位裡的值:
對比上面的列舉類,一 一對應,沒毛病!!!
insert或update時(要注意這裡!!!!!!)
賦值的時候
#{exception_type.index}
不要寫成: #{exception_type} 這樣寫存進資料庫的是前臺傳過來的列舉屬性,不是想要的效果!
用谷歌postman測試這個介面時,剛開始有點糾結這個列舉屬性exception_type,該如何給其賦值,其實得這樣如下:
在看看上面的列舉類:
對應controller介面,裡面只用一個物件介面:
@RequestMapping(value = "/addException", method = RequestMethod.POST)
public ExecuteResult addException(ExceptionEntity exceptionEntity)
斷點除錯,剛進此方法時:
我這裡做的是insert,最後儲存到資料庫如下:
大致就這樣了!!!暫時就只用了,不知道還有沒有其他方法!
參考網上的一些文章: