在应用开发中,总会遇到许多数据字典项,比如对象状态、对象类型等等,这些项一般都是固定的若干可选值选项,比如对象状态可能有新建、修改、删除等状态,这些数据字典项一旦定义完毕改动的频率非常低;在应用开发中,为了处理方便,一般要对这些数据字典项值选项进行数字编码(例如: 0表示新建,1表示修改,2表示删除等),以方便应用程序中使用。而UI显示对象信息时不能显示对象状态等的编码,对于编码值设计人员知道代表什么意思,但用户就不明白了,所以需要进行编码转换,从编码转换为文字描述(名称),也就是需要把状态编码0转换为“新建”,把1转换为“修改”,把2转换为“删除”等显示给用户,用户才明白对象当前的状态是什么。
具体实现
1、关系数据库
首先要创建实体对象表和数据字典表
其中实体对象表中的数据字典项为数据字典表的主键。
2、 实体对象
Customer
public class Customer {
private Long cust_id;
private String cust_name;
//使用数据字典对象代替
//private String cust_source;
//private String cust_industry;
//private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//引用关联的数据字典对象
private BaseDict cust_source; //客户来源 cust_source.dict_id
private BaseDict cust_industry; //客户行业
private BaseDict cust_level; //客户级别
......
BaseDict
public class BaseDict {
private String dict_id;
private String dict_type_code;
private String dict_type_name;
private String dict_item_name;
private String dict_item_code;
private String dict_memo;
private Integer dict_sort;
private Character dict_enable;
......
3、 配置文件
customer.hbm.xml
<hibernate-mapping package="cn.itcast.domain" >
<class name="Customer" table="cst_customer" >
<id name="cust_id" >
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name" ></property>
<!-- 使用数据字典对象代替 -->
<!-- <property name="cust_source" column="cust_source" ></property>
<property name="cust_industry" column="cust_industry" ></property>
<property name="cust_level" column="cust_level" ></property> -->
<property name="cust_linkman" column="cust_linkman" ></property>
<property name="cust_phone" column="cust_phone" ></property>
<property name="cust_mobile" column="cust_mobile" ></property>
<!-- 多对一 -->
<many-to-one name="cust_source" column="cust_source" class="BaseDict" ></many-to-one>
<many-to-one name="cust_industry" column="cust_industry" class="BaseDict" ></many-to-one>
<many-to-one name="cust_level" column="cust_level" class="BaseDict" ></many-to-one>
</class>
</hibernate-mapping>
baseDict.hbm.xml
<hibernate-mapping package="cn.itcast.domain" >
<class name="BaseDict" table="base_dict" >
<id name="dict_id" >
<generator class="assigned"></generator>
</id>
<property name="dict_type_code" ></property>
<property name="dict_type_name" ></property>
<property name="dict_item_name" ></property>
<property name="dict_item_code" ></property>
<property name="dict_memo" ></property>
<property name="dict_sort" ></property>
<property name="dict_enable" ></property>
</class>
</hibernate-mapping>
技术交流:713788313