MyBatis支持持久化enum类型属性。假设t_user表中有一列gender(性别)类型为 varchar2(10),存储 MALE 或者 FEMALE 两种值。并且,User对象有一个enum类型的gender 属性,如下所示:

public enumGender {

MALE,FEMALE;

}

默认情况下MyBatis使用EnumTypeHandler来处理enum类型的Java属性,并且将其存储为enum值的名称。我们不需要为此做任何额外的配置。可以像使用基本数据类型属性一样使用enum类型属性,如下:

create tablet_user(

idnumber primary key,

namevarchar2(50),

gendervarchar2(10)

);

public classUser{privateInteger id;privateString name;privateGender gender;//setters and getters

}

映射文件:

select my_seq.nextval from dualinsert into t_user(id,name,gender)

values(#{id},#{name},#{gender})

映射接口:

public interfaceXxxxMapper{intinsertUser(User user);

}

测试方法:

@Testpublic voidtest_insertUser(){

SqlSession sqlSession= null;try{

sqlSession=MyBatisSqlSessionFactory.openSession();

SpecialMapper mapper= sqlSession.getMapper(SpecialMapper.class);

User user= new User("tom",Gender.MALE);

mapper.insertUser(user);

sqlSession.commit();

}catch(Exception e) {

e.printStackTrace();

}

}

当执行insertStudent语句的时候MyBatis会取Gender枚举(FEMALE/MALE)的名称,存储到GENDER列中。

如果你想存储FEMALE为0,MALE为1到gender列中,需要在mybatis-config.xml文件中配置专门的类型处理器,并指定它处理的枚举类型是哪个。

EnumOrdinalTypeHandler这是个类型处理器,源码中有个set方法就是在帮助我们存值,源码如下

/*** Copyright 2009-2017 the original author or authors.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

*http://www.apache.org/licenses/LICENSE-2.0*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.*/

packageorg.apache.ibatis.type;importjava.sql.CallableStatement;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;/***@authorClinton Begin*/

public class EnumOrdinalTypeHandler> extends BaseTypeHandler{private final Classtype;private finalE[] enums;public EnumOrdinalTypeHandler(Classtype) {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.");

}

}

@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throwsSQLException {

ps.setInt(i, parameter.ordinal());

}

@Overridepublic E getNullableResult(ResultSet rs, String columnName) throwsSQLException {int i =rs.getInt(columnName);if(rs.wasNull()) {return null;

}else{try{returnenums[i];

}catch(Exception ex) {throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);

}

}

}

@Overridepublic E getNullableResult(ResultSet rs, int columnIndex) throwsSQLException {int i =rs.getInt(columnIndex);if(rs.wasNull()) {return null;

}else{try{returnenums[i];

}catch(Exception ex) {throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);

}

}

}

@Overridepublic E getNullableResult(CallableStatement cs, int columnIndex) throwsSQLException {int i =cs.getInt(columnIndex);if(cs.wasNull()) {return null;

}else{try{returnenums[i];

}catch(Exception ex) {throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);

}

}

}

}

枚举类型的【顺序值】是根据enum中的声明顺序赋值的。如果改变了Gender里面对象的声明顺序,则数据库存储的数据和此顺序值就不匹配了。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐