跟我一起学extjs5(29--加入模块和菜单定义[2建立java bean])
对上一节中加入的4个模块在java中建立相应的bean文件。
以上表建立好以后,需要建立java bean文件。在建立之前需要加入几个支持类。
先在com.jfok.server.common中建立包annotation,再在下面加入二个类库,这二个是自定义的注释类。分别用来注释每一个模块的信息和模块中每一字段的信息。
package com.jfok.server.common.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 用来注释模块
*
* @author jiangfeng
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface TableDefine {
// 此表的id号,为一个4位数字
int id();
// 模块名称
String title();
// 模块简称
String shortname() default "";
// 模块分组名称
String group();
// 模块的主键是否可以是分级的,如果是可以分级的,可定义为"2,2,2",表示有三级,每级代码长为2位,
// 比如会计科目可以这样来定义
// 10
// 1001
// 100101
// 100102
// 100102
// 1002
// ......
String codeLevel() default "";
// 模块是否有附件
boolean attachment() default false;
}
package com.jfok.server.common.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 用来注释模块中的每个字段
*
* @author jiangfeng
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldDefine {
String title();
int number() default 0;
String remark() default "";
boolean nameField() default false;
boolean hidden() default false;
String fieldGroup() default "默认组";
}
在com.jfok.server.hibernate中建立包system,把系统用到的bean文件都放在这个目录下。先建立一个接口文件:
package com.jfok.server.hibernate.system;
/**
* 有此接口的类,都是系统模块的控制类
*
* @author jfok
*
*/
public interface _IModuleControlInterface {
}
再建立_ModuleGroup.java
package com.jfok.server.hibernate.system;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import com.jfok.server.common.annotation.FieldDefine;
import com.jfok.server.common.annotation.TableDefine;
/**
* 模块分组类,每一个模块都有一个大类分组
*
* @author jfok 2012.11.7
*/
@SuppressWarnings("serial")
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@TableDefine(group = "系统模块", id = 9901, title = "模块分组")
public class _ModuleGroup implements _IModuleControlInterface, Serializable {
public static final String TITLE = "tf_title";
@Id
@FieldDefine(title = "模块分组序号", number = 10)
@Column(nullable = false, length = 10)
private String tf_moduleGroupId;
@FieldDefine(title = "模块分组名称", nameField = true, number = 20)
@Column(nullable = false, length = 50)
private String tf_title;
@FieldDefine(title = "描述", number = 30)
private String tf_description;
@FieldDefine(title = "图标地址", number = 40)
private String tf_iconURL;
@FieldDefine(title = "备注", number = 50)
private String tf_remark;
public _ModuleGroup() {
}
// setter and getter
}
上面这个类中,就用自定义的注释类来进行了注释,一个是注释在类名上,一个是注释在各个字段上。这二个类注释的功能是以后在增加一个模块的时候,会自动把这些注释的信息自动的加到保存的模块和模块字段的表中。
再加入_Module.java
package com.jfok.server.hibernate.system;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.jfok.server.common.annotation.FieldDefine;
import com.jfok.server.common.annotation.TableDefine;
/**
* 系统中每个模块的字义
*
* @author jfok 2012.11.7
*
*/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
@SuppressWarnings("serial")
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@TableDefine(group = "系统模块", id = 9902, title = "系统模块")
public class _Module implements _IModuleControlInterface, Serializable {
public static final String MODULEID = "tf_moduleId";
public static final String MODULENAME = "tf_moduleName";
@Id
@FieldDefine(title = "模块ID号", number = 10)
@Column(nullable = false, length = 10)
private String tf_moduleId; // 模块ID,模块序号
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinColumn(name = "tf_moduleGroupId")
@FieldDefine(title = "模块分组", number = 20)
private _ModuleGroup tf_ModuleGroup;
@FieldDefine(title = "模块标识", number = 30)
@Column(nullable = false, length = 50, updatable = false)
private String tf_moduleName; // 模块英文名称
@FieldDefine(title = "模块名称", nameField = true, number = 40)
@Column(nullable = false, length = 50)
private String tf_title; // 模块中文名称
@FieldDefine(title = "模块简称", number = 50)
@Column(length = 20)
private String tf_shortname; // 简称
@FieldDefine(title = "英文简称", number = 60)
@Column(length = 20)
private String tf_englishname; // 英文简称,在新增序号的时候,可以把这字母加进去
@FieldDefine(title = "表名", number = 70)
@Column(length = 50)
private String tf_tableName;
@FieldDefine(title = "主键", number = 80)
@Column(nullable = false, length = 50)
private String tf_primaryKey;// 模块主键
@FieldDefine(title = "显示标志字段", number = 81)
@Column(nullable = false, length = 50)
private String tf_nameFields;// 模块主要关键内容的字段,如合同的是合同名称,部门的是部门名称
@FieldDefine(title = "记录标题tpl", number = 82, remark = "如果显示标志字段不能描述记录,需要设置此内容")
@Column(length = 200)
private String tf_titleTpl;// 模块主要关键内容的字段,如合同的是合同名称,部门的是部门名称
@FieldDefine(title = "编码字段", number = 91)
@Column(length = 50)
private String tf_codeField;// 模块编码字段,如果有的话
@FieldDefine(title = "顺序号字段", number = 97)
@Column(length = 50)
private String tf_orderField;// 模块编码字段,如果有的话
@FieldDefine(title = "日期字段", number = 92)
@Column(length = 50)
private String tf_dateField;
@FieldDefine(title = "年度字段", number = 93)
@Column(length = 50)
private String tf_yearfield;
@FieldDefine(title = "月度字段", number = 94)
@Column(length = 50)
private String tf_monthField;
@FieldDefine(title = "季度字段", number = 95)
@Column(length = 50)
private String tf_seasonField;
@FieldDefine(title = "文件字段", number = 96)
@Column(length = 50)
private String tf_fileField;
@FieldDefine(title = "主页上顺序", number = 99)
@Column(length = 50)
private String tf_homePageTag;
@FieldDefine(title = "模块描述", number = 100)
@Column(length = 50)
private String tf_description;
@FieldDefine(title = "请求地址", number = 110)
@Column(nullable = false, length = 50)
private String tf_requestMapping;// 系统中后台服务的调用接入点 user.do,employee.do 等
@FieldDefine(title = "图标地址", number = 120)
@Column(length = 50)
private String tf_iconURL;
@FieldDefine(title = "默认排序字段", number = 130)
private String tf_defaultOrderField;
@FieldDefine(title = "行操作", remark = "新增及修改操作都行内完成", number = 140)
@Column(nullable = false)
private Boolean tf_isInlineOper;
// 如部门编码为 2,2,2,2,2
@FieldDefine(title = "编码级次", remark = "如果设置了编码级次,则主键的长度必须是此级次中的,并且必须有父级编码存在", number = 150)
@Column(length = 50)
String tf_codeLevel;
@FieldDefine(title = "联动模块", remark = "在本模块的数据增删改后,打开的联动模块都要刷新数据。", number = 155)
@Column(length = 200)
private String tf_linkedModule;
@FieldDefine(title = "可用", number = 160)
@Column(nullable = false)
private Boolean tf_isEnable = false;
@FieldDefine(title = "可浏览", number = 170)
@Column(nullable = false)
private Boolean tf_hasBrowse = false;
@FieldDefine(title = "可增加", number = 180)
@Column(nullable = false)
private Boolean tf_hasInsert = false;
@FieldDefine(title = "可修改", number = 190)
@Column(nullable = false)
private Boolean tf_hasEdit = false;
@FieldDefine(title = "可删除", number = 200)
@Column(nullable = false)
private Boolean tf_hasDelete = false;
@FieldDefine(title = "可执行", number = 210)
@Column(nullable = false)
private Boolean tf_hasExec = false;
@FieldDefine(title = "可审核", number = 220)
@Column(nullable = false)
private Boolean tf_hasAuditing = false;
@FieldDefine(title = "可审批", number = 230)
@Column(nullable = false)
private Boolean tf_hasApprove = false;
@FieldDefine(title = "可支付", number = 240)
@Column(nullable = false)
private Boolean tf_hasPayment = false;
@FieldDefine(title = "有附件", number = 250)
@Column(nullable = false)
private Boolean tf_hasAddition = false; // 是否需要附件
@FieldDefine(title = "可权限设置", remark = "用户可对此模块设置权限,选定的才可以浏览与操作", number = 260)
@Column(nullable = false)
private Boolean tf_canLimit = false; // 此模块是否能进行权限设置
@FieldDefine(title = "Excel导入", remark = "是否可以通过Excel导入新增记录", number = 270)
@Column(nullable = false)
private Boolean tf_allowInsertExcel = false;
@FieldDefine(title = "Excel修改", remark = "是否可以导出的Excel修改后再导入", number = 280)
@Column(nullable = false)
private Boolean tf_allowEditExcel = false;
@FieldDefine(title = "可图表", number = 290)
@Column(nullable = false)
private Boolean tf_hasChart = false;
@FieldDefine(title = "系统模块", number = 300, remark = "如果是系统模块,用户没有浏览权限,就不把模块定义发送到前端")
@Column(nullable = false)
private Boolean tf_isSystem = false;
@FieldDefine(title = "查询条件顺序号", number = 310, remark = "此模块放在综合查询的条件选择栏中的顺序")
private Integer tf_searchCondOrder;
@FieldDefine(title = "备注", number = 800)
private String tf_remark;
public _Module() {
}
// setter and getter
}
_MenuGroup.java
package com.jfok.server.hibernate.system;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import com.jfok.server.common.annotation.FieldDefine;
import com.jfok.server.common.annotation.TableDefine;
/**
* 菜单分组
*
* @author jfok
*
*/
@SuppressWarnings("serial")
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@TableDefine(group = "系统模块", id = 9920, title = "菜单分组")
public class _MenuGroup implements _IModuleControlInterface, Serializable {
@Id
@FieldDefine(title = "顺序号", remark = "菜单分组按顺序号显示在菜单条上", number = 10)
@Column(length = 10, nullable = false)
private String tf_menuGroupId;
@FieldDefine(title = "分组名称", nameField = true, number = 20)
@Column(length = 50, nullable = false)
private String tf_title;
@FieldDefine(title = "展开", remark = "在树状菜单下默认是否展开", number = 30)
private Boolean tf_expand;
@FieldDefine(title = "图标glyph", number = 40)
private Boolean tf_glyph;
@FieldDefine(title = "图标文件名", remark = "图标放置于/images/module/目录下", number = 50)
@Column(length = 50)
private String tf_iconURL;
@FieldDefine(title = "分组描述", number = 60)
@Column(length = 50)
private String tf_description;
@FieldDefine(title = "备注", number = 70)
private String tf_remark;
@Transient
private List<_MenuModule> tf_menuModules;
public _MenuGroup() {
}
//setter and getter
}
_MenuModule.java
package com.jfok.server.hibernate.system;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;
import com.jfok.server.common.annotation.FieldDefine;
import com.jfok.server.common.annotation.TableDefine;
/**
* 系统可使用的菜单项
*
* @author jfok
*
*/
@SuppressWarnings("serial")
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@TableDefine(group = "系统模块", id = 9921, title = "系统菜单")
public class _MenuModule implements _IModuleControlInterface, Serializable {
@Id
@FieldDefine(title = "ID号", hidden = true, number = 10)
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Integer tf_menuModuleId;
@FieldDefine(title = "顺序号", remark = "按顺序号显示在菜单中", number = 20)
@Column(nullable = false)
private Integer tf_orderId;
@JsonIgnore
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinColumn(name = "tf_menuGroupId", nullable = false)
@FieldDefine(title = "菜单分组", nameField = true, number = 30)
private _MenuGroup tf_MenuGroup;
@JsonIgnore
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinColumn(name = "tf_moduleId", nullable = false)
@FieldDefine(title = "系统模块", number = 40)
private _Module tf_Module;
@FieldDefine(title = "父菜单标题", number = 50)
@Column(length = 20)
private String tf_parentMenu;
@FieldDefine(title = "分隔下一条", number = 60)
private Boolean tf_addSeparator;
@FieldDefine(title = "菜单标题", number = 70)
@Column(length = 50)
private String tf_title;
@FieldDefine(title = "父模块约束设置", number = 80)
private String tf_parentFilter;
// 这个字段用于传送json到前台,放在json中
@Column(insertable = false, updatable = false)
private String tf_ModuleId;
public _MenuModule() {
}
// setter and getter
}
再修改 hibernate.cfg.xml,把上面4个class加入到配置文件中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://10.211.55.5:1433;databaseName=extjs5</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.autocommit">false</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<!-- 系统模块 -->
<mapping class="com.jfok.server.hibernate.system._ModuleGroup" />
<mapping class="com.jfok.server.hibernate.system._Module" />
<mapping class="com.jfok.server.hibernate.system._MenuGroup" />
<mapping class="com.jfok.server.hibernate.system._MenuModule" />
</session-factory>
</hibernate-configuration>
到现在为止,我们建立好了数据表以及java bean文件,下面可以开发前后台进行协同工作的内容了。
各个文件的目录结构: