定义列
以下注解,用于描述数据库列信息的,只能标注于字段上
@AutoColumn
列定义注解,聚合注解,集合了下面
@Column*开头的注解的能力
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | String | 否 | '' | 列名 |
| type | String | 否 | '' | 字段类型 |
| length | Integer | 否 | -1 | 字段长度 |
| decimalLength | Integer | 否 | -1 | 字段长度小数位数 |
| notNull | Boolean | 否 | false | 不为null |
| defaultValue | String | 否 | '' | 列的默认值 |
| defaultValueType | DefaultValueEnum(UNDEFINED, EMPTY_STRING, NULL) | 否 | UNDEFINED | 列的默认值类型,优先级高于value属性 |
| comment | String | 否 | '' | 列注释 |
| sort | Integer | 否 | 0 | 字段顺序,1:第一位,-1:最后一位 |
| dialect | String | 否 | '' | 强调该配置只生效于某种数据库策略,参考 org.dromara.autotable.core.constants.DatabaseDialect 常量 |
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
@AutoColumn("name")
private String name;
......
}@AutoColumns
@AutoColumn注解的集合注解,可以同时定义多个@AutoColumn,实现一套代码适配多种数据库
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | AutoColumn[] | 是 | 列注解集合 |
🌐 多数据库适配利器
当你的产品需要同时支持 MySQL、PostgreSQL、Oracle 等多种数据库时,@AutoColumns 配合 dialect 属性可以让同一个实体自动适配不同数据库的字段类型、长度等配置。框架会根据当前连接的数据库自动选择匹配的配置!
查看样例
@AutoTable
public class Article {
@PrimaryKey(autoIncrement = true)
private Long id;
// 标题:MySQL 用 100 长度,PostgreSQL 用 200 长度
@AutoColumns({
@AutoColumn(length = 100, dialect = DatabaseDialect.MySQL),
@AutoColumn(length = 200, dialect = DatabaseDialect.PostgreSQL)
})
private String title;
// 内容:不同数据库使用不同的大文本类型
@ColumnComment("文章内容")
@AutoColumns({
@AutoColumn(type = MysqlTypeConstant.LONGTEXT, dialect = DatabaseDialect.MySQL),
@AutoColumn(type = PgsqlTypeConstant.TEXT, dialect = DatabaseDialect.PostgreSQL),
@AutoColumn(type = "clob", dialect = DatabaseDialect.Oracle)
})
private String content;
// 未指定 dialect 的配置作为其他数据库的默认值
@AutoColumns({
@AutoColumn(type = MysqlTypeConstant.JSON, dialect = DatabaseDialect.MySQL),
@AutoColumn(type = PgsqlTypeConstant.JSONB, dialect = DatabaseDialect.PostgreSQL),
@AutoColumn(type = "text") // 其他数据库默认使用 text
})
private String metadata;
}工作原理:
- 框架启动时检测当前数据库类型
- 自动匹配
dialect与当前数据库一致的@AutoColumn配置 - 如果没有匹配的
dialect,使用未指定dialect的配置作为默认值 - 多个配置按声明顺序优先匹配
@ColumnName
列名称
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | String | 是 | 列名 |
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
@ColumnName("name")
private String name;
......
}@ColumnType
列类型及长度
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | String | 否 | '' | 字段的类型,不指定的情况下使用默认映射规则 |
| length | int | 否 | -1 | 字段长度,值小于 0 相当于 null |
| decimalLength | int | 否 | -1 | 小数点后小数位数,值小于 0 相当于 null |
| values | String[] | 否 | {} | 枚举可选值(目前仅支持 MySQL 的 enum 和 set 类型),如果字段是 java 的 Enum 类型,那么 values 可以不指定,默认取枚举的所有值 |
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 框架内部会自动赋值默认长度255
@ColumnType(MysqlTypeConstant.VARCHAR)
private String name;
......
}@ColumnNotNull
声明列必须非空
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 标记此列不能为空
@ColumnNotNull
private String name;
......
}@ColumnDefault
指定列的默认值
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | String | 否 | '' | 列的默认值 |
| type | DefaultValueEnum(UNDEFINED, EMPTY_STRING, NULL) | 否 | UNDEFINED | 列的默认值类型,优先级高于value属性 |
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 默认值为null
@ColumnDefault(type = DefaultValueEnum.NULL)
private String name;
// 默认值为空字符串
@ColumnDefault(type = DefaultValueEnum.EMPTY_STRING)
private String nickName;
// 默认值为字符串12345678
@ColumnDefault("12345678")
private String phone;
......
}@ColumnComment
列注释
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | String | 是 | 列注释 |
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 声明列注释
@ColumnComment("姓名")
private String name;
......
}@MysqlColumnCharset
MySQL 专属注解。指定列字符集和字符排序
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | String | 是 | 字符集 | |
| collate | String | 是 | 字符排序 |
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 声明列字符集和字符排序
@MysqlColumnCharset(value = "utf8mb4", collate = "utf8mb4_0900_ai_ci")
private String name;
......
}提示
以 Mysql 为前缀的注解说明是 Mysql 数据库专属的注解,对应的库注解只会作用于对应的数据库。
@MysqlColumnUnsigned
MySQL 专属注解。指定MySQL数字类型不允许负数,其范围从 0 开始
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 声明年龄是无符号数字
@MysqlColumnUnsigned
private Integer age;
......
}提示
以 Mysql 为前缀的注解说明是 Mysql 数据库专属的注解,对应的库注解只会作用于对应的数据库。
警告
该注解等同于mysql的修饰符UNSIGNED,只能作用于数字类型上
@MysqlColumnZerofill
MySQL 专属注解。指定MySQL数字类型在固定长度不足的情况下,进行前置补0
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 声明编号是10位的,位数不足的前面自动补0,例如number是12345,查询结果是0000012345
@MysqlColumnZerofill
@ColumnType(length = 10)
private Integer number;
......
}提示
以 Mysql 为前缀的注解说明是 Mysql 数据库专属的注解,对应的库注解只会作用于对应的数据库。
警告
该注解等同于mysql的修饰符ZEROFILL,只能作用于数字类型上,同时基于mysql的特性,改注解自动携带了@MysqlColumnUnsigned的能力
@Ignore
忽略标注的字段,不参与表结构维护,该注解的优先级大于所有注解
查看样例
// 测试表
@AutoTable
public class TestTable {
private Long id;
// 直接忽略name字段,即便声明了@ColumnName
@Ignore
@ColumnName("name") // 不生效
private String name;
......
}@PrimaryKey
声明该列为主键
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
boolean | 是 | 是否自增 | ||
| autoIncrement | boolean | 是 | 是否自增 |
查看样例
// 测试表
@AutoTable
public class TestTable {
@PrimaryKey(autoIncrement = true)
private Long id;
......
}@AutoIncrement
声明该列为自增,注意不同数据库的要求
| 属性 | 类型 | 必填 | 默认 | 释义 |
|---|---|---|---|---|
| value | boolean | 否 | 是否自增 |
查看样例
// 测试表
@AutoTable
public class TestTable {
@AutoIncrement
@Index(type = IndexTypeEnum.UNIQUE) // [!code ++] // mysql
private Long id;
......
}提示
以 Mysql 为为例,单纯使用@AutoIncrement会报错,需要结合@Index(type = IndexTypeEnum.UNIQUE),因为mysql要求自增字段必须为唯一索引
