解決MyBatis中Enum字段參數(shù)解析問題
MyBatis操作的基本User對(duì)象結(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字段對(duì)應(yīng)的類型為SexEnum枚舉類型,因此同時(shí)設(shè)置了如下的TypeHandler,從而在前端傳入?yún)?shù)和從數(shù)據(jù)庫(kù)中取值時(shí)進(jìn)行自動(dòng)的名稱轉(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); } /** * 通過存儲(chǔ)過程讀取性別 */ @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); }}請(qǐng)求參數(shù)解析問題
下面在使用axios post請(qǐng)求來(lái)更新用戶信息,請(qǐng)求的JSON參數(shù)如下:
{ id: id, userName: username, sex: sex === ’MALE’ ? 1 : 2, // 1: 男,2: 女 note: note}
其中由于sex字段的枚舉類型,因此這里將sex根據(jù)select得到的option來(lái)轉(zhuǎn)換為了枚舉中的id對(duì)應(yīng)的值。也就是:
1: MALE2: FAMALE
但在發(fā)出請(qǐng)求之后,服務(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'])]
問題解決對(duì)于該問題,可以使用枚舉類型的desc來(lái)作為參數(shù)傳遞。當(dāng)使用如下desc屬性映射時(shí),將JSON請(qǐng)求參數(shù)改成如下就可以解析成功不報(bào)錯(cuò):
{ id: id, userName: username, sex: sex, note: note}
此時(shí)對(duì)應(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>
同時(shí)注意:enum字段sex對(duì)應(yīng)的數(shù)據(jù)庫(kù)列的設(shè)置中,該列的數(shù)據(jù)類型為int,而不能為tinyint。tinyint數(shù)據(jù)類型對(duì)應(yīng)于java中的boolean,1表示true,0表示false。
到此這篇關(guān)于解決MyBatis中Enum字段參數(shù)解析問題的文章就介紹到這了,更多相關(guān)MyBatis Enum字段參數(shù)解析內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Window7安裝MariaDB數(shù)據(jù)庫(kù)及系統(tǒng)初始化操作分析2. ORA-06512數(shù)字或值錯(cuò)誤字符串緩沖區(qū)太小異常詳解3. Vista下安裝SQL Server 2005,附加數(shù)據(jù)庫(kù)報(bào)錯(cuò)4. Mybatis Plus 自定義方法實(shí)現(xiàn)分頁(yè)功能的示例代碼5. MySQL之高可用集群部署及故障切換實(shí)現(xiàn)6. MyBatis 實(shí)現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例7. MySQL中庫(kù)的基本操作指南(推薦!)8. SQL Server ISNULL 不生效原因及解決9. oracle數(shù)據(jù)庫(kù)增量備份腳本10. 分享Sql Server 存儲(chǔ)過程使用方法
