跟我一起学extjs5(40--增加一个自定义模块[1建立表和bean文件])
经过上面的工作,一个独立模块的CRUD已经可以运行了,为了更好的可以看到其他自定义功能,我们做一个自定义的独立模块,这个模块中包括了各种的数据类型。
新增一个“销售合同“的独立模块,模块名称为Agreement,里面包括了20几个字段,把主要的字段类型都设置了,有字符串型,整型,浮点型,金额型,日期型,布尔型,百分比,还包括计算字段。
建立表的sql语句如下:
/****** 对象: Table [dbo].[Agreement] 脚本日期: 10/16/2014 14:13:11 ******/
CREATE TABLE [dbo].[Agreement](
[tf_agreementId] [int] NOT NULL,
[tf_code] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[tf_name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[tf_customerNme] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[tf_orderId] [int] NULL,
[tf_signDate] [datetime] NOT NULL,
[tf_productName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[tf_salesman] [nchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
[tf_number] [int] NOT NULL,
[tf_allLength] [money] NULL,
[tf_unitPrice] [money] NOT NULL,
[tf_unitText] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[tf_discount] [money] NOT NULL CONSTRAINT [DF_Agreement_tf_discount] DEFAULT ((1)),
[tf_amount] AS (round(([tf_number]*[tf_unitPrice])*[tf_discount],(2))),
[tf_receivedPrice] [money] NULL,
[tf_receivedPercent] AS (case when ([tf_number]*[tf_unitPrice])*[tf_discount]<>(0) then [tf_receivedPrice]/round(([tf_number]*[tf_unitPrice])*[tf_discount],(2)) else (0) end),
[tf_finished] [bit] NULL,
[tf_isImportant] [bit] NULL,
[tf_beginDate] [datetime] NULL,
[tf_endDate] [datetime] NULL,
[tf_remark] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_Agreement] PRIMARY KEY CLUSTERED
(
[tf_agreementId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** 对象: Index [IX_Agreement_tf_name] 脚本日期: 10/16/2014 14:13:53 ******/
CREATE UNIQUE NONCLUSTERED INDEX [IX_Agreement_tf_name] ON [dbo].[Agreement]
(
[tf_name] ASC
)WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
package com.jfok.server.hibernate.app;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import com.jfok.server.common.annotation.FieldDefine;
import com.jfok.server.common.annotation.TableDefine;
/**
* 一个用于演示单个模块所有自定义属性的类
*
* @author jfok
*
* 2014.10.16
*
*/
@SuppressWarnings("serial")
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@TableDefine(group = "合同管理", id = 2010, title = "销售合同")
public class Agreement implements Serializable {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@FieldDefine(title = "ID号", number = 10, hidden = true)
private Integer tf_agreementId;
@FieldDefine(title = "合同编码", number = 20)
@Column(nullable = false, length = 20)
private String tf_code;
// 这个字段是这个模块的namefields字段,在删除等操作时会提示显示这个字段的信息
@FieldDefine(title = "合同名称", nameField = true, number = 30)
@Column(length = 50, nullable = false)
private String tf_name;
// 因为是一个独白模块,没有做相应的客户单位,产品名称的表来关联
@FieldDefine(title = "客户单位名称", number = 40)
@Column(length = 50, nullable = false)
private String tf_customerNme;
@FieldDefine(title = "顺序号", number = 50)
private Integer tf_orderId;
@FieldDefine(title = "签订日期", number = 60)
@Column(nullable = false)
private Date tf_signDate;
@FieldDefine(title = "产品名称", number = 70)
@Column(length = 50, nullable = false)
private String tf_productName;
@FieldDefine(title = "销售员", number = 80)
@Column(length = 10, nullable = false)
private String tf_salesman;
@FieldDefine(title = "数量", number = 90)
@Column(nullable = false)
private Integer tf_number;
@FieldDefine(title = "总长度", number = 100)
private Double tf_allLength;
@FieldDefine(title = "单价", number = 110, money = true)
private Double tf_unitPrice;
@FieldDefine(title = "计量单位", number = 120)
@Column(length = 10)
private String tf_unitText;
// 默认折扣是 1.00,即不打折
@FieldDefine(title = "折扣", number = 130, percent = true)
private Double tf_discount = 1.0;
@FieldDefine(title = "总金额", number = 140, money = true, remark = "总金额=数量*单价*折扣")
@Column(updatable = false, insertable = false)
private Double tf_amount;
@FieldDefine(title = "已收金额", number = 150, money = true)
private Double tf_receivedPrice;
@FieldDefine(title = "已收比例", number = 160, percent = true, remark = "已收比例=已收金额/总金额")
@Column(updatable = false, insertable = false)
private Double tf_receivedPercent;
@FieldDefine(title = "是否完成", number = 170)
private Boolean tf_finished;
@FieldDefine(title = "是否重要", number = 180)
private Boolean tf_isImportant;
@FieldDefine(title = "开始时间", number = 190)
private Date tf_beginDate;
@FieldDefine(title = "结束时间", number = 200)
private Date tf_endDate;
@FieldDefine(title = "备注", number = 290)
private String tf_remark;
public Agreement() {
}
//getter and setter
}
在hibernate.cfg.xml文件中加入
<!-- 合同管理 -->
<mapping class="com.jfok.server.hibernate.app.Agreement" />
根据我这套系统的最终设计目标,新增一个模块只要做上面二步就行了,那么下面我们来具体的实现一下过程。
第一步:在前台执行功能,根据标注在Agreement上的注解和标注在各个字段上的注解来自动生成成_Module,_ModuleField中的记录,并生成一个缺省的列表方案和一个表单方案。
前面我们也做过这个功能,但是是在浏览器中输入一个地址来实现的,现在我们把这个功能移到前台。
在Top.js的toolbar中再加入一按钮,放在“搜索“后面,用来输入模块名称,然后把该模块的定义和字段定义加入到各个表中。
{
text : '导入',
handler : function() {
Ext.MessageBox.prompt('增加模块', '请输入要增加模块的类名称:', function(btn, text) {
if (btn == 'ok') {
Ext.Ajax.request({
scope : this,
url : 'systemframe/addmodule.do',
params : {
moduleName : text
},
success : function(response) {
if (response.responseText)
Ext.MessageBox.show({
title : '导入失败',
msg : '导入模块失败<br/><br/>' + response.responseText,
buttons : Ext.MessageBox.OK,
icon : Ext.MessageBox.ERROR
});
else {
Ext.toastInfo('增加模块', '模块:' + text
+ '的定义和grid,form定义已经到加系统中!');
}
},
failure : function() {
window.alert(text + '保存失败!')
}
})
}
})
}
}
现在可以来导入Agreement 的信息了。
确定后,如果导入成功,会显示如下提示:
后台的程序会根据模块名称来进行导入操作。_Module表的数据
_ModuleField的数据