【SSH项目实战】国税协同平台-22.逆向工程

本文介绍如何在测试环境下利用逆向工程在MyEclipse中创建数据库表和实体类,包括引入数据库、执行SQL语句、设置Hibernate框架、进行逆向工程以及生成实体类和映射文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下来我们要利用“逆向工程”来创建表和实体类。

因为是测试,所以我们所有的代码均在test包下完成。
首先,我们在我们工程的test.conf包下创建database.sql文件,把上一次我们得到的sql语句输入进去:
[sql]  view plain copy
  1. /*==============================================================*/  
  2. /* DBMS name:      MySQL 5.0                                    */  
  3. /* Created on:     2015/11/14 7:39:35                          */  
  4. /*==============================================================*/  
  5.   
  6. drop table if exists emp_role;  
  7.   
  8.   
  9. drop table if exists role_pri;  
  10.   
  11.   
  12. drop table if exists t_dept;  
  13.   
  14.   
  15. drop table if exists t_emp;  
  16.   
  17.   
  18. drop table if exists t_leader;  
  19.   
  20.   
  21. drop table if exists t_org;  
  22.   
  23.   
  24. drop table if exists t_privilege;  
  25.   
  26.   
  27. drop table if exists t_role;  
  28.   
  29.   
  30. /*==============================================================*/  
  31. /* Table: emp_role                                              */  
  32. /*==============================================================*/  
  33. create table emp_role  
  34. (  
  35.    emp_id               varchar(32) not null,  
  36.    role_id              varchar(32) not null,  
  37.    state                int,  
  38.    primary key (emp_id, role_id)  
  39. );  
  40.   
  41.   
  42. /*==============================================================*/  
  43. /* Table: role_pri                                              */  
  44. /*==============================================================*/  
  45. create table role_pri  
  46. (  
  47.    role_id              varchar(32) not null,  
  48.    pin_id               varchar(32) not null,  
  49.    primary key (role_id, pin_id)  
  50. );  
  51.   
  52.   
  53. /*==============================================================*/  
  54. /* Table: t_dept                                                */  
  55. /*==============================================================*/  
  56. create table t_dept  
  57. (  
  58.    dept_id              varchar(32) not null,  
  59.    org_id               varchar(32) not null,  
  60.    name                 varchar(50),  
  61.    primary key (dept_id)  
  62. );  
  63.   
  64.   
  65. /*==============================================================*/  
  66. /* Table: t_emp                                                 */  
  67. /*==============================================================*/  
  68. create table t_emp  
  69. (  
  70.    emp_id               varchar(32) not null,  
  71.    dept_id              varchar(32),  
  72.    name                 varchar(50) not null,  
  73.    primary key (emp_id)  
  74. );  
  75.   
  76.   
  77. /*==============================================================*/  
  78. /* Table: t_leader                                              */  
  79. /*==============================================================*/  
  80. create table t_leader  
  81. (  
  82.    emp_id               varchar(32) not null,  
  83.    dept_id              varchar(32),  
  84.    name                 varchar(50) not null,  
  85.    position             int,  
  86.    primary key (emp_id)  
  87. );  
  88.   
  89.   
  90. /*==============================================================*/  
  91. /* Table: t_org                                                 */  
  92. /*==============================================================*/  
  93. create table t_org  
  94. (  
  95.    org_id               varchar(32) not null,  
  96.    name                 varchar(50),  
  97.    primary key (org_id)  
  98. );  
  99.   
  100.   
  101. /*==============================================================*/  
  102. /* Table: t_privilege                                           */  
  103. /*==============================================================*/  
  104. create table t_privilege  
  105. (  
  106.    pin_id               varchar(32) not null,  
  107.    name                 varchar(50),  
  108.    primary key (pin_id)  
  109. );  
  110.   
  111.   
  112. /*==============================================================*/  
  113. /* Table: t_role                                                */  
  114. /*==============================================================*/  
  115. create table t_role  
  116. (  
  117.    role_id              varchar(32) not null,  
  118.    name                 varchar(50),  
  119.    primary key (role_id)  
  120. );  
  121.   
  122.   
  123. alter table emp_role add constraint FK_emp_role foreign key (emp_id)  
  124.       references t_emp (emp_id) on delete restrict on update restrict;  
  125.   
  126.   
  127. alter table emp_role add constraint FK_emp_role2 foreign key (role_id)  
  128.       references t_role (role_id) on delete restrict on update restrict;  
  129.   
  130.   
  131. alter table role_pri add constraint FK_belong foreign key (role_id)  
  132.       references t_role (role_id) on delete restrict on update restrict;  
  133.   
  134.   
  135. alter table role_pri add constraint FK_own foreign key (pin_id)  
  136.       references t_privilege (pin_id) on delete restrict on update restrict;  
  137.   
  138.   
  139. alter table t_dept add constraint FK_org_dept foreign key (org_id)  
  140.       references t_org (org_id) on delete restrict on update restrict;  
  141.   
  142.   
  143. alter table t_emp add constraint FK_dept_emp foreign key (dept_id)  
  144.       references t_dept (dept_id) on delete restrict on update restrict;  
  145.   
  146.   
  147. alter table t_leader add constraint FK_extends foreign key (emp_id)  
  148.       references t_emp (emp_id) on delete restrict on update restrict;  

然后我们为了方便看数据库,我们在MyEclipse中来引入数据库。
首先点击Windows--->show view,在视窗上面搜索db Bower视图,然后成功创建视图,然后在新属兔上右键点击,在出现的菜单中选择“New”:


之后进行如下操作:


此时成功将数据库引入MyEclipse:




我们执行一下sql语句,和之前我们在Sqlyog图形界面管理工具中一样,我们成功创建了数据库表:



数据库表创建以后,我们正式进入我们的逆向工程实现。
我们使用逆向工程之前,首先要告诉我们的编译器,我们使用了hibernate框架(因为我们是手动配置的),这样逆向工程才会去主动使用我们的hibernate规则。

具体做法, 首先点击这里(高版本的是在Project Facets中):


然后我们在出现的对话框中选取如下操作:



然后我们的工程图标就变成存在hibernate的样子了:


下面我们就要开始逆向了,我们选中刚刚创建的表,然后点击逆向工程:



在弹出的对话框中进行如下操作:




点击Finish之后,我们就可以看到我们的数据库表的实体类和映射文件在我们指定的文件夹中生成了:



各种关联关系、联合主键各种东西都不需要自己去写,我们以后开发要使用这种方式。(唯一不一样的是我们要把生成的hbm映射文件的catlog这个参数给删除)

以后开发的流程就是:设计-->逆向-->开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值