因为咱们每次编写Hibernate的时候都须要写实体,写映射文件。并且Hibernate的映射文件也容易出错。而逆向工程能够帮咱们自动生成实体和映射文件,这样就很是方便了。sql
在设计数据库表时,咱们使用PowerDesigner来生成概念模型\物理模型…数据库
设计一我的员组织架构:有机构、部门、员工、领导、角色、权限。markdown
最后生成物理模型是这样子的:架构
咱们能够单个生成,一个一个复制ide
也能够把整个物理模型的sql语句一块儿生成:idea
/*==============================================================*/ /* DBMS name: MySQL 5.0 */ /* Created on: 2017/6/5 20:22:52 */ /*==============================================================*/ drop table if exists person_role; drop table if exists t_company; drop table if exists t_dept; drop table if exists t_employee; drop table if exists t_person; drop table if exists t_privilege; drop table if exists t_role; drop table if exists t_role_privilege; /*==============================================================*/ /* Table: person_role */ /*==============================================================*/ create table person_role ( person_id varchar(32) not null, role_id varchar(32) not null, state varchar(32), primary key (person_id, role_id) ); /*==============================================================*/ /* Table: t_company */ /*==============================================================*/ create table t_company ( company_id varchar(32) not null, name varchar(32), primary key (company_id) ); /*==============================================================*/ /* Table: t_dept */ /*==============================================================*/ create table t_dept ( dept_id varchar(32) not null, company_id varchar(32) not null, name varchar(32), primary key (dept_id) ); /*==============================================================*/ /* Table: t_employee */ /*==============================================================*/ create table t_employee ( person_id varchar(32) not null, dept_id varchar(32), name varchar(32), employee_id varchar(32), primary key (person_id) ); /*==============================================================*/ /* Table: t_person */ /*==============================================================*/ create table t_person ( person_id varchar(32) not null, dept_id varchar(32) not null, name varchar(32), primary key (person_id) ); /*==============================================================*/ /* Table: t_privilege */ /*==============================================================*/ create table t_privilege ( privilege_id varchar(32) not null, name varchar(32), primary key (privilege_id) ); /*==============================================================*/ /* Table: t_role */ /*==============================================================*/ create table t_role ( role_id varchar(32) not null, name varchar(32), primary key (role_id) ); /*==============================================================*/ /* Table: t_role_privilege */ /*==============================================================*/ create table t_role_privilege ( role_id varchar(32) not null, privilege_id varchar(32) not null, primary key (role_id, privilege_id) ); alter table person_role add constraint FK_person_role foreign key (person_id) references t_person (person_id) on delete restrict on update restrict; alter table person_role add constraint FK_person_role2 foreign key (role_id) references t_role (role_id) on delete restrict on update restrict; alter table t_dept add constraint FK_companty_dept foreign key (company_id) references t_company (company_id) on delete restrict on update restrict; alter table t_employee add constraint FK_inherit foreign key (person_id) references t_person (person_id) on delete restrict on update restrict; alter table t_person add constraint FK_dept_person foreign key (dept_id) references t_dept (dept_id) on delete restrict on update restrict; alter table t_role_privilege add constraint FK_belong foreign key (role_id) references t_role (role_id) on delete restrict on update restrict; alter table t_role_privilege add constraint FK_own foreign key (privilege_id) references t_privilege (privilege_id) on delete restrict on update restrict;
在数据库生成八张表:spa
参考博文!.net
值得注意的是:Intellij idea下生成出来的映射文件是没有对应的关联关系的。也就是说:一对多或多对多的关系,它是不会帮你自动生成的【好像是这样子的】。。。所以,须要咱们本身添加Set【若是须要】设计