mybatis 处理枚举类型
程序员文章站
2023-11-23 08:01:45
MyBatis支持持久化enum类型属性。假设t_user表中有一列gender(性别)类型为 varchar2(10),存储 MALE 或者 FEMALE 两种值。并且,User对象有一个enum类型的gender 属性,如下所示: 默认情况下MyBatis使用EnumTypeHandler来处理 ......
mybatis支持持久化enum类型属性。假设t_user表中有一列gender(性别)类型为 varchar2(10),存储 male 或者 female 两种值。并且,user对象有一个enum类型的gender 属性,如下所示:
public enum gender { male,female; }
默认情况下mybatis使用enumtypehandler来处理enum类型的java属性,并且将其存储为enum值的名称。我们不需要为此做任何额外的配置。可以像使用基本数据类型属性一样使用enum类型属性,如下:
create table t_user( id number primary key, name varchar2(50), gender varchar2(10) );
public class user{ private integer id; private string name; private gender gender; //setters and getters }
映射文件:
<insert id="insertuser" parametertype="user"> <selectkey keyproperty="id" resulttype="int" order="before"> select my_seq.nextval from dual </selectkey> insert into t_user(id,name,gender) values(#{id},#{name},#{gender}) </insert>
映射接口:
public interface xxxxmapper{ int insertuser(user user); }
测试方法:
@test public void test_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文件中配置专门的类型处理器,并指定它处理的枚举类型是哪个。
<typehandler handler="org.apache.ibatis.type.enumordinaltypehandler" javatype="com.briup.special.gender"/>
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. */ package org.apache.ibatis.type; import java.sql.callablestatement; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; /** * @author clinton begin */ public class enumordinaltypehandler<e extends enum<e>> extends basetypehandler<e> { private final class<e> type; private final e[] enums; public enumordinaltypehandler(class<e> 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, e parameter, jdbctype jdbctype) throws sqlexception { ps.setint(i, parameter.ordinal()); } @override public e getnullableresult(resultset rs, string columnname) throws sqlexception { int i = rs.getint(columnname); if (rs.wasnull()) { return null; } else { try { return enums[i]; } catch (exception ex) { throw new illegalargumentexception("cannot convert " + i + " to " + type.getsimplename() + " by ordinal value.", ex); } } } @override public e getnullableresult(resultset rs, int columnindex) throws sqlexception { int i = rs.getint(columnindex); if (rs.wasnull()) { return null; } else { try { return enums[i]; } catch (exception ex) { throw new illegalargumentexception("cannot convert " + i + " to " + type.getsimplename() + " by ordinal value.", ex); } } } @override public e getnullableresult(callablestatement cs, int columnindex) throws sqlexception { int i = cs.getint(columnindex); if (cs.wasnull()) { return null; } else { try { return enums[i]; } catch (exception ex) { throw new illegalargumentexception("cannot convert " + i + " to " + type.getsimplename() + " by ordinal value.", ex); } } } }
枚举类型的【顺序值】是根据enum中的声明顺序赋值的。如果改变了gender里面对象的声明顺序,则数据库存储的数据和此顺序值就不匹配了。
上一篇: day009--python文件操作