mybatis 源码中
为了看是否支持localDate而看了下代码,最终发现是支持的,而且jpa自动生成时,LocalDate对应Mysal的date类型,LocalDateTime对应datetime类型。Mybatis是LocalDate对应DATE,LocalDateTime 对应的是TIMESTAMP。
在Mybatis 的源码中看到了org.apache.ibatis.type下的ArrayTypeHandler类里。看到了
private static final ConcurrentHashMap<Class<?>, String> STANDARD_MAPPING;
static {
STANDARD_MAPPING = new ConcurrentHashMap<>();
STANDARD_MAPPING.put(BigDecimal.class, JdbcType.NUMERIC.name());
STANDARD_MAPPING.put(BigInteger.class, JdbcType.BIGINT.name());
STANDARD_MAPPING.put(boolean.class, JdbcType.BOOLEAN.name());
STANDARD_MAPPING.put(Boolean.class, JdbcType.BOOLEAN.name());
STANDARD_MAPPING.put(byte[].class, JdbcType.VARBINARY.name());
STANDARD_MAPPING.put(byte.class, JdbcType.TINYINT.name());
STANDARD_MAPPING.put(Byte.class, JdbcType.TINYINT.name());
STANDARD_MAPPING.put(Calendar.class, JdbcType.TIMESTAMP.name());
STANDARD_MAPPING.put(java.sql.Date.class, JdbcType.DATE.name());
STANDARD_MAPPING.put(java.util.Date.class, JdbcType.TIMESTAMP.name());
STANDARD_MAPPING.put(double.class, JdbcType.DOUBLE.name());
STANDARD_MAPPING.put(Double.class, JdbcType.DOUBLE.name());
STANDARD_MAPPING.put(float.class, JdbcType.REAL.name());
STANDARD_MAPPING.put(Float.class, JdbcType.REAL.name());
STANDARD_MAPPING.put(int.class, JdbcType.INTEGER.name());
STANDARD_MAPPING.put(Integer.class, JdbcType.INTEGER.name());
STANDARD_MAPPING.put(LocalDate.class, JdbcType.DATE.name());
STANDARD_MAPPING.put(LocalDateTime.class, JdbcType.TIMESTAMP.name());
STANDARD_MAPPING.put(LocalTime.class, JdbcType.TIME.name());
STANDARD_MAPPING.put(long.class, JdbcType.BIGINT.name());
STANDARD_MAPPING.put(Long.class, JdbcType.BIGINT.name());
STANDARD_MAPPING.put(OffsetDateTime.class, JdbcType.TIMESTAMP_WITH_TIMEZONE.name());
STANDARD_MAPPING.put(OffsetTime.class, JdbcType.TIME_WITH_TIMEZONE.name());
STANDARD_MAPPING.put(Short.class, JdbcType.SMALLINT.name());
STANDARD_MAPPING.put(String.class, JdbcType.VARCHAR.name());
STANDARD_MAPPING.put(Time.class, JdbcType.TIME.name());
STANDARD_MAPPING.put(Timestamp.class, JdbcType.TIMESTAMP.name());
STANDARD_MAPPING.put(URL.class, JdbcType.DATALINK.name());
}
在mybatis中看到的是,LocalDate对应的jdbcType是DATE,LocalDateTime 对应的是TIMESTAMP。
也就是说在xml中resultmap中就是上面这样的对应关系写法。
mybatis xml中的jdbctype 实则引用的是 org.apache.ibatis.type 下的枚举 JdbcType。
这个枚举里只有一个参数。
public final int TYPE_CODE;
private static Map<Integer,JdbcType> codeLookup = new HashMap<>();
static {
for (JdbcType type : JdbcType.values()) {
codeLookup.put(type.TYPE_CODE, type);
}
}
JdbcType(int code) {
this.TYPE_CODE = code;
}
public static JdbcType forCode(int code) {
return codeLookup.get(code);
}
参数TYPE_CODE 为int类型,引用的是jdk中java.sql 的Types类代码如下
package java.sql;
/**
* <P>The class that defines the constants that are used to identify generic
* SQL types, called JDBC types.
* <p>
* This class is never instantiated.
*/
public class Types {
/**
* <P>The constant in the Java programming language, sometimes referred
* to as a type code, that identifies the generic SQL type
* <code>BIT</code>.
*/
public final static int BIT = -7;
/**
* <P>The constant in the Java programming language, sometimes referred
* to as a type code, that identifies the generic SQL type
* <code>TINYINT</code>.
*/
public final static int TINYINT = -6;
...
// Prevent instantiation
private Types() {}
代码中得到的:
- 枚举中可以指定一个 private static Map<valuetype,enumType> 。在静态代码块中赋值。由此可提供一个用value值获取枚举对象的静态方法,直接由map获取。
2.我们会遇到一些需要指定固定值的类,上面的java.sql.Type 里面的属性为final 的static 的int类型,这种对象不需要实例化,将构造方法用private修饰,并是空方法,达到一个禁止实例化的做用。