mybatis和java数据类型,Mybatis的数据库字段类型JDBCType
和java。。。
我们在设计数据库的时候,往往需要注意数据库字段的类型和java的基本数据类型的对应关系,以便合理设计java实体;其实Mybaits⾥数据库字段类型
和java数据类型的转换,都是由⼀个个typeHandler进⾏转换,如:BooleanTypeHandler、ShortTypeHandler、LongTypeHandler。。。⽽他们的基类都是
BaseTypeHandler,如果我们想⾃定义⾃⼰的类型转换器,也需要继承于该类;如下图是Mybatis默认给我们注册和实现的typeHandler
register(Boolean.class, new BooleanTypeHandler());
register(Boolean.TYPE, new BooleanTypeHandler());
register(JdbcType.BOOLEAN, new BooleanTypeHandler());
register(JdbcType.BIT, new BooleanTypeHandler());
register(Byte.class, new ByteTypeHandler());
register(Byte.TYPE, new ByteTypeHandler());
register(JdbcType.TINYINT, new ByteTypeHandler());
register(Short.class, new ShortTypeHandler());
register(Short.TYPE, new ShortTypeHandler());
register(JdbcType.SMALLINT, new ShortTypeHandler());
register(Integer.class, new IntegerTypeHandler());
register(Integer.TYPE, new IntegerTypeHandler());
register(JdbcType.INTEGER, new IntegerTypeHandler());
register(Long.class, new LongTypeHandler());
register(Long.TYPE, new LongTypeHandler());
register(Float.class, new FloatTypeHandler());
register(Float.TYPE, new FloatTypeHandler());
register(JdbcType.FLOAT, new FloatTypeHandler());
register(Double.class, new DoubleTypeHandler());
register(Double.TYPE, new DoubleTypeHandler());
register(JdbcType.DOUBLE, new DoubleTypeHandler());
register(Reader.class, new ClobReaderTypeHandler());
register(String.class, new StringTypeHandler());
register(String.class, JdbcType.CHAR, new StringTypeHandler());
register(String.class, JdbcType.CLOB, new ClobTypeHandler());
register(String.class, JdbcType.VARCHAR, new StringTypeHandler());
register(String.class, JdbcType.LONGVARCHAR, new ClobTypeHandler());
register(String.class, JdbcType.NVARCHAR, new NStringTypeHandler());
register(String.class, JdbcType.NCHAR, new NStringTypeHandler());
register(String.class, JdbcType.NCLOB, new NClobTypeHandler());
register(JdbcType.CHAR, new StringTypeHandler());
register(JdbcType.VARCHAR, new StringTypeHandler());
register(JdbcType.CLOB, new ClobTypeHandler());
register(JdbcType.LONGVARCHAR, new ClobTypeHandler());
register(JdbcType.NVARCHAR, new NStringTypeHandler());
register(JdbcType.NCHAR, new NStringTypeHandler());
register(JdbcType.NCLOB, new NClobTypeHandler());
register(Object.class, JdbcType.ARRAY, new ArrayTypeHandler());
register(JdbcType.ARRAY, new ArrayTypeHandler());
register(BigInteger.class, new BigIntegerTypeHandler());
register(JdbcType.BIGINT, new LongTypeHandler());
register(BigDecimal.class, new BigDecimalTypeHandler());
register(JdbcType.REAL, new BigDecimalTypeHandler());
register(JdbcType.DECIMAL, new BigDecimalTypeHandler());
register(JdbcType.NUMERIC, new BigDecimalTypeHandler());
register(InputStream.class, new BlobInputStreamTypeHandler());
register([Ljava.lang.Byte.class, new ByteObjectArrayTypeHandler());
register([Ljava.lang.Byte.class, JdbcType.BLOB, new BlobByteObjectArrayTypeHandler());
register([Ljava.lang.Byte.class, JdbcType.LONGVARBINARY, new BlobByteObjectArrayTypeHandler()); register([B.class, new ByteArrayTypeHandler());
register([B.class, JdbcType.BLOB, new BlobTypeHandler());bigdecimal转换为integer
register([B.class, JdbcType.LONGVARBINARY, new BlobTypeHandler());
register(JdbcType.LONGVARBINARY, new BlobTypeHandler());
register(JdbcType.BLOB, new BlobTypeHandler());
register(Object.class, this.UNKNOWN_TYPE_HANDLER);
register(Object.class, JdbcType.OTHER, this.UNKNOWN_TYPE_HANDLER);
register(JdbcType.OTHER, this.UNKNOWN_TYPE_HANDLER);
register(java.util.Date.class, new DateTypeHandler());
register(java.util.Date.class, JdbcType.DATE, new DateOnlyTypeHandler());
register(java.util.Date.class, JdbcType.TIME, new TimeOnlyTypeHandler());
register(JdbcType.TIMESTAMP, new DateTypeHandler());
register(JdbcType.DATE, new DateOnlyTypeHandler());
register(JdbcType.TIME, new TimeOnlyTypeHandler());
register(java.sql.Date.class, new SqlDateTypeHandler());
register(Time.class, new SqlTimeTypeHandler());
register(Timestamp.class, new SqlTimestampTypeHandler());
try
{
register("java.time.Instant", "org.pe.InstantTypeHandler");
register("java.time.LocalDateTime", "org.pe.LocalDateTimeTypeHandler"); register("java.time.LocalDate", "org.pe.LocalDateTypeHandler");
register("java.time.LocalTime", "org.pe.LocalTimeTypeHandler");
register("java.time.OffsetDateTime", "org.pe.OffsetDateTimeTypeHandler"); register("java.time.OffsetTime", "org.pe.OffsetTimeTypeHandler");
register("java.time.ZonedDateTime", "org.pe.ZonedDateTimeTypeHandler"); register("java.time.Month", "org.pe.MonthTypeHandler");
register("java.time.Year", "org.pe.YearTypeHandler");
register("java.time.YearMonth", "org.pe.YearMonthTypeHandler");
register("java.time.chrono.JapaneseDate", "org.pe.JapaneseDateTypeHandler"); }
catch (ClassNotFoundException localClassNotFoundException)
{
}
register(Character.class, new CharacterTypeHandler());
register(Character.TYPE, new CharacterTypeHandler());
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。