看看问题下的评论以了解这里发生了什么......基本上你需要一个新的 DatabaseType (如果使用 MariaDB,那么显然只是子类化 MySQL 一个):
public class MariaDBType extends MysqlDatabaseType {
private final static String DATABASE_URL_PORTION = "mysql";
private final static String DRIVER_CLASS_NAME = "org.mariadb.jdbc.Driver";
private final static String DATABASE_NAME = "MariaDB";
@Override
public boolean isDatabaseUrlThisType(String url, String dbTypePart) {
return DATABASE_URL_PORTION.equals(dbTypePart);
}
@Override
protected String getDriverClassName() {
return DRIVER_CLASS_NAME;
}
@Override
public String getDatabaseName() {
return DATABASE_NAME;
}
@Override
protected void appendDateType(StringBuilder sb, FieldType fieldType, int fieldWidth) {
/**
* TIMESTAMP in MySQL does some funky stuff with the last-modification time. Values are 'not null' by default
* with an automatic default of CURRENT_TIMESTAMP. Strange design decision.
*/
if (isDatetimeFieldWidthSupported()) {
sb.append("DATETIME(").append(fieldWidth).append(")");
} else {
sb.append("DATETIME");
}
}
public boolean isDatetimeFieldWidthSupported() {
return true;
}
}
现在我可以像正常一样使用日期数据类型并获得毫秒或微秒精度......在我的例子中,我使用 Gray 现有的注释参数作为宽度(适用于这种情况,我只需要毫秒精度,而不是微秒)。
@DatabaseField(columnName = Constants.TIMESTAMP, dataType = DataType.DATE, width = 3)
private Date timestamp;
最后,如果您正在尝试解决这个特定问题,请不要忘记小数秒标志:
private static JdbcPooledConnectionSource cSource = new JdbcPooledConnectionSource();
cSource.setDatabaseType(new MariaDBType());
cSource.setUrl("jdbc:mysql://localhost:3306/database?useFractionalSeconds=true");
cSource.setUsername("username");
cSource.setPassword("password");