自动维护数据库表结构的 Java 框架
你只负责维护实体,数据库的事交给我
| 特性 | 说明 | |
|---|---|---|
| 🚀 | 开箱即用 | 一个 @AutoTable 注解激活,零配置启动 |
| 🔌 | 9 种数据库 | MySQL、PostgreSQL、Oracle、达梦、人大金仓、H2、SQLite、Doris、MariaDB |
| 🌐 | 多库适配 | 同一实体适配多种数据库,通过 dialect 属性自动切换字段配置 |
| 🏗️ | 自动建库 | 连数据库都帮你建好,真正的开箱即用 |
| 📦 | 数据初始化 | 建表后自动灌入初始数据,支持 SQL 文件和 Java 方法 |
| 🎯 | ORM 兼容 | Mybatis-Plus、Mybatis-Flex、原生 Mybatis |
| 💾 | 多数据源 | 一套代码管理多个数据库,自动按数据源分组处理 |
| 🛡️ | 生产友好 | validate / update / create 多模式,SQL 变更可审计追溯 |
| 🔔 | 生命周期 | 10 种事件回调 + 4 种拦截器,完全掌控执行过程 |
| 🔧 | 高度扩展 | 自定义类型映射、支持扩展新数据库策略 |
定义实体:
@Data
@AutoTable(comment = "用户表")
public class User {
@PrimaryKey(autoIncrement = true)
private Long id;
@AutoColumn(comment = "用户名", notNull = true)
private String username;
@AutoColumn(comment = "邮箱")
@Index
private String email;
@ColumnComment("状态")
@ColumnDefault("0")
private Integer status;
}启动应用后,自动生成表结构:
CREATE TABLE `user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL COMMENT '用户名',
`email` varchar(255) DEFAULT NULL COMMENT '邮箱',
`status` int DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `auto_idx_user_email` (`email`)
) COMMENT='用户表';后续修改实体,表结构自动同步! 新增字段、修改类型、添加索引,全部自动处理。
<!-- Spring Boot 项目(兼容 Spring Boot 2.x 和 3.x) -->
<dependency>
<groupId>org.dromara.autotable</groupId>
<artifactId>auto-table-spring-boot-starter</artifactId>
<version>最新版本</version>
</dependency>💡 最新版本请查看 Maven Central
@EnableAutoTable
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@Data
@AutoTable
public class Article {
@PrimaryKey(autoIncrement = true)
private Long id;
private String title;
private String content;
}启动项目,表自动创建完成!🎉
同一个实体,轻松适配多种数据库!通过 @AutoColumns + dialect 属性,为不同数据库配置不同的字段类型:
@AutoTable
public class Article {
@PrimaryKey(autoIncrement = true)
private Long id;
// 不同数据库使用不同的大文本类型
@AutoColumns({
@AutoColumn(type = "longtext", dialect = "MySQL"),
@AutoColumn(type = "text", dialect = "PostgreSQL"),
@AutoColumn(type = "clob", dialect = "Oracle")
})
private String content;
// 不同数据库使用不同的字段长度
@AutoColumns({
@AutoColumn(length = 100, dialect = "MySQL"),
@AutoColumn(length = 200, dialect = "PostgreSQL")
})
private String summary;
}💡 未指定
dialect的配置作为默认值,框架会根据当前数据库自动选择匹配的配置!
开启后,连数据库都不用手动创建,真正的开箱即用:
auto-table:
auto-build-database: true # 自动创建数据库适合产品类项目、开源框架,启动即具备完整的库表结构。
建表后自动初始化数据,支持三种方式:
src/main/resources/sql/
├── user.sql # 方式1:自动匹配表名的 SQL 文件
├── _init_.sql # 全局初始化脚本
└── ...
@AutoTable
public class User {
// 方式2:Java 方法返回初始数据
@InitDataList
public static List<User> initData() {
return Arrays.asList(
new User("admin", "管理员"),
new User("guest", "访客")
);
}
}
// 方式3:指定 SQL 文件路径
@AutoTable(initSql = "classpath:sql/{dialect}/user.sql")
public class User { ... }所有表结构变更可追溯,支持记录到数据库或文件:
auto-table:
record-sql:
enable: true
record-type: db # db / file / custom
version: 1.0.0 # 版本号,方便管理生产环境可配合 Flyway 使用,自动生成迁移脚本。
一套实体代码,自动按数据源分组处理:
@AutoTable
@DS("master") // 主库
public class User { ... }
@AutoTable
@DS("slave") // 从库
public class Order { ... }10 种事件回调 + 4 种拦截器,完全掌控执行过程:
@Component
public class MyCallback implements CreateTableFinishCallback {
@Override
public void afterCreateTable(String dialect, TableMetadata metadata) {
log.info("表 {} 创建完成,开始初始化数据...", metadata.getTableName());
}
}| 特性 | JPA | AutoTable |
|---|---|---|
| 自动建表 | ✅ | ✅ |
| 自动建库 | ❌ | ✅ |
| 增量更新结构 | ✅ 完整 | |
| 索引管理 | ✅ 完整 | |
| 字段顺序保持 | ❌ | ✅ MySQL |
| Mybatis 生态 | ❌ | ✅ |
| 多数据库支持 | ✅ | ✅ 9种 |
| 多数据库适配 | ❌ | ✅ dialect |
| 数据初始化 | ❌ | ✅ 3种方式 |
| 生产模式(仅校验) | ❌ | ✅ |
| SQL 变更审计 | ❌ | ✅ |
| 生命周期钩子 | ✅ 10+4 |
| 数据库 | 测试版本 | 状态 | 维护者 |
|---|---|---|---|
| MySQL | 5.7+ | ✅ | |
| MariaDB | 对应 MySQL 版本 | ✅ | |
| PostgreSQL | 15.5 | ✅ | |
| SQLite | 3.35.5 | ✅ | |
| H2 | 2.2.220 | ✅ | |
| Oracle | 11g / 23ai | ✅ | @lizhian |
| Doris | 2.0 | ✅ | @lizhian |
| 达梦 | dm8 | ✅ | @minfc |
| 人大金仓 | V009R001C002B0014 | ✅ | @minfc |
🙌 其他数据库暂未支持,期待你的 PR!
| 框架 | 扩展包 | 说明 |
|---|---|---|
| Mybatis-Plus | mybatis-plus-ext | 免手写 Mapper、数据填充、关联查询等 |
| Mybatis-Flex | mybatis-flex-ext | 数据填充(类似 JPA 审计) |
完整文档请访问:https://autotable.tangzc.com
感谢所有为 AutoTable 做出贡献的开发者!
如果 AutoTable 对你有帮助,请给我们一个 ⭐ Star!

