解決MyBatis中Enum字段參數(shù)解析問題
MyBatis操作的基本User對象結(jié)構(gòu)如下:
@Data@Alias(value = 'user')public class User implements Serializable { private static final long serialVersionUID = -4947062488310146862L; private Long id; @NotNull(message = '用戶名不能為空') private String userName; @NotNull(message = '備注不能為空') private String note; @NotNull(message = '性別不能為空') private SexEnum sex;}
其中sex字段對應(yīng)的類型為SexEnum枚舉類型,因此同時設(shè)置了如下的TypeHandler,從而在前端傳入?yún)?shù)和從數(shù)據(jù)庫中取值時進行自動的名稱轉(zhuǎn)換。
@MappedJdbcTypes(JdbcType.INTEGER)@MappedTypes(value = SexEnum.class)public class SexTypeHandler extends BaseTypeHandler<SexEnum> { /** * 設(shè)置非空性別參數(shù) */ @Override public void setNonNullParameter(PreparedStatement preparedStatement, int i, SexEnum sexEnum, JdbcType jdbcType) throws SQLException {preparedStatement.setInt(i, sexEnum.getId()); } /** * 通過列名讀取性別 */ @Override public SexEnum getNullableResult(ResultSet resultSet, String s) throws SQLException {int sex = resultSet.getInt(s);if (sex != 1 && sex != 2) { return null;}return SexEnum.getEnumById(sex); } /** * 通過下標(biāo)讀取性別 */ @Override public SexEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {int sex = resultSet.getInt(i);if (sex != 1 && sex != 2) { return null;}return SexEnum.getEnumById(sex); } /** * 通過存儲過程讀取性別 */ @Override public SexEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {int sex = callableStatement.getInt(i);if (sex != 1 && sex != 2) { return null;}return SexEnum.getEnumById(sex); }}請求參數(shù)解析問題
下面在使用axios post請求來更新用戶信息,請求的JSON參數(shù)如下:
{ id: id, userName: username, sex: sex === ’MALE’ ? 1 : 2, // 1: 男,2: 女 note: note}
其中由于sex字段的枚舉類型,因此這里將sex根據(jù)select得到的option來轉(zhuǎn)換為了枚舉中的id對應(yīng)的值。也就是:
1: MALE2: FAMALE
但在發(fā)出請求之后,服務(wù)端日志中出現(xiàn)如下的問題:
2020-03-02 22:59:50.722 WARN 10864 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `cn.zyt.springbootlearning.domain.SexEnum` from number 2: index value outside legal index range [0..1]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `cn.zyt.springbootlearning.domain.SexEnum` from number 2: index value outside legal index range [0..1] at [Source: (PushbackInputStream); line: 1, column: 40] (through reference chain: cn.zyt.springbootlearning.domain.User['sex'])]
問題解決對于該問題,可以使用枚舉類型的desc來作為參數(shù)傳遞。當(dāng)使用如下desc屬性映射時,將JSON請求參數(shù)改成如下就可以解析成功不報錯:
{ id: id, userName: username, sex: sex, note: note}
此時對應(yīng)的sex字段選擇select標(biāo)簽如下:
<tr> <td>sex:</td> <td><select name='sex' value={sex} onChange={this.handleChange}> <option value='MALE'>MALE</option> <option value='FEMALE'>FEMALE</option> </select></td></tr>
同時注意:enum字段sex對應(yīng)的數(shù)據(jù)庫列的設(shè)置中,該列的數(shù)據(jù)類型為int,而不能為tinyint。tinyint數(shù)據(jù)類型對應(yīng)于java中的boolean,1表示true,0表示false。
到此這篇關(guān)于解決MyBatis中Enum字段參數(shù)解析問題的文章就介紹到這了,更多相關(guān)MyBatis Enum字段參數(shù)解析內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. idea連接sql sever2019圖文教程(超詳細(xì))2. Sqlserver之死鎖查詢以及批量解鎖的實現(xiàn)方法3. Mysql入門系列:對MYSQL查詢中有疑問的數(shù)據(jù)進行編碼4. 詳解MySQL中的數(shù)據(jù)類型和schema優(yōu)化5. mysql的like模式6. Mysql入門系列:建立MYSQL客戶機程序的一般過程7. Linux安裝ODBC連接SQLServer數(shù)據(jù)庫的步驟8. 數(shù)據(jù)庫相關(guān)的幾個技能:ACCESS轉(zhuǎn)SQL9. Oracle學(xué)習(xí)筆記之二----查詢10. SqlServer 多種分頁方式 詳解(含簡單速度測試)
