object 枚举类型

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
        UIViewAutoresizingFlexibleHeight       = 1 << 4,
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
 
 
 
简单的数据的偏移

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/RoysPhoneBlog/p/4617231.html

在MyBatis中,我们可以使用TypeHandler来处理Java中的枚举类型与SQL中的映射关系。具体操作如下: 1. 创建一个实现了TypeHandler接口的枚举类型换器类。 ``` public class MyEnumTypeHandler<E extends Enum<E>> implements TypeHandler<E> { private Class<E> type; public MyEnumTypeHandler(Class<E> type) { if (type == null) throw new IllegalArgumentException("Type argument cannot be null"); this.type = type; } @Override public void setParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException { if (parameter == null) { ps.setNull(i, jdbcType.TYPE_CODE); } else { ps.setInt(i, parameter.ordinal()); } } @Override public E getResult(ResultSet rs, String columnName) throws SQLException { int ordinal = rs.getInt(columnName); if (rs.wasNull()) { return null; } else { try { Method method = type.getMethod("values"); Object[] objects = (Object[]) method.invoke(type); return (E) objects[ordinal]; } catch (Exception e) { throw new IllegalArgumentException("Cannot convert " + ordinal + " to " + type.getSimpleName() + " by ordinal value.", e); } } } @Override public E getResult(ResultSet rs, int columnIndex) throws SQLException { int ordinal = rs.getInt(columnIndex); if (rs.wasNull()) { return null; } else { try { Method method = type.getMethod("values"); Object[] objects = (Object[]) method.invoke(type); return (E) objects[ordinal]; } catch (Exception e) { throw new IllegalArgumentException("Cannot convert " + ordinal + " to " + type.getSimpleName() + " by ordinal value.", e); } } } @Override public E getResult(CallableStatement cs, int columnIndex) throws SQLException { int ordinal = cs.getInt(columnIndex); if (cs.wasNull()) { return null; } else { try { Method method = type.getMethod("values"); Object[] objects = (Object[]) method.invoke(type); return (E) objects[ordinal]; } catch (Exception e) { throw new IllegalArgumentException("Cannot convert " + ordinal + " to " + type.getSimpleName() + " by ordinal value.", e); } } } } ``` 2. 在MyBatis的配置文件中注册这个TypeHandler。 ``` <typeHandlers> <typeHandler handler="com.example.MyEnumTypeHandler" javaType="com.example.MyEnum"/> </typeHandlers> ``` 3. 在Java对象中使用枚举类型。 ``` public class User { private Long id; private String name; private Gender gender; // getter and setter } ``` 4. 在SQL语句中使用枚举类型。 ``` <select id="getUser" parameterType="long" resultType="com.example.User"> select * from user where id = #{id} </select> ``` 这样,MyBatis就会自动将数据库中的值换为Java对象中的枚举类型,或者将Java对象中的枚举类型换为数据库中的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值