数据字典的作用:
1.贯穿系统的所有数据项,开发过程中,动态的维护系统数据项:
2.保证数据的录入安全,业务表使用数据字典的时候,存放的是数据项的编号,而不是数据项的值。
3.方便系统的统计。
1.数据库的设计:
#数据字典
create table Elec_SystemDDL(
seqID INT NOT NULL primary key, #主键ID(自增长)
keyword VARCHAR(20) NULL, #数据类型
ddlCode INT NULL, #数据项的code
ddlName VARCHAR(50) NULL #数据项的value
);
创建Elec_SystemDDL.java文件
package com.itheima.elec.bean; import java.io.Serializable; @SuppressWarnings("serial") public class ElecSytemDDL implements Serializable{ private Integer seqID; private String keyword; private Integer ddlCode; private String ddlName; public Integer getSeqID() { return seqID; } public void setSeqID(Integer seqID) { this.seqID = seqID; } public String getKeyword() { return keyword; } public void setKeyword(String keyword) { this.keyword = keyword; } public Integer getDdlCode() { return ddlCode; } public void setDdlCode(Integer ddlCode) { this.ddlCode = ddlCode; } public String getDdlName() { return ddlName; } public void setDdlName(String ddlName) { this.ddlName = ddlName; } }
创建对应的hbm.xml文件 ElecSystemDDL.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.itheima.elec.bean.ElecSystemDDL" table="Elec_SystemDDL"> <id name="seqID" type="integer" column="seqID"> <generator class="increment"></generator> </id> <property name="keyword" type="string" column="keyword"></property> <property name="ddlCode" type="integer" column="ddlCode"></property> <property name="ddlName" type="string" column="ddlName"></property> </class> </hibernate-mapping>
然后在hibernate.cfg.xml文件中添加文件
<mapping resource="com/itheima/elec/bean/ElecSystemDDL.hbm.xml"/>
IElecSystemDDL.java
package com.itheima.elec.dao; import com.itheima.elec.bean.ElecSystemDDL; public interface IElecSystemDDLDao extends ICommonDao<ElecSystemDDL> { public static final String SERVICE_NAME = "com.itheima.elec.dao.impl.ElecSystemDDLDaoImpl"; }
ElecSystemDDLDaoImpl.java
package com.itheima.elec.dao.impl; import org.springframework.stereotype.Repository; import com.itheima.elec.bean.ElecSystemDDL; import com.itheima.elec.dao.IElecSystemDDLDao; /** * @Repository * 相当于在spring中定义:<bean id="elecTextDaoImpl" class="com. **.ElecTextDaoImpl"> */ @Repository(IElecSystemDDLDao.SERVICE_NAME) public class ElecSystemDDLDaoImpl extends CommonDaoImpl<ElecSystemDDL> implements IElecSystemDDLDao{ }
IElecSystemDDLService.java
package com.itheima.elec.service; import java.util.List; import com.itheima.elec.bean.ElecSystemDDL; public interface IElecSystemDDLService { public static final String SERVICE_NAME="com.itheima.elec.service.impl.ElecSystemDDLServiceImpl"; }
ElecSystemDDLServiceImpl.java
package com.itheima.elec.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.itheima.elec.dao.IElecSystemDDLDao; import com.itheima.elec.service.IElecSystemDDLService; //事务控制:spring的声明事务处理,在service层添加@Transactional @Service(IElecSystemDDLService.SERVICE_NAME) @Transactional(readOnly=true) public class ElecSystemDDLServiceImpl implements IElecSystemDDLService { /*数据字典Dao*/ @Resource(name=IElecSystemDDLDao.SERVICE_NAME) IElecSystemDDLDao elecSystemDDLDao; }
ElecSystemDDLAction.java
package com.itheima.elec.web.action; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.itheima.elec.bean.ElecSystemDDL; import com.itheima.elec.service.IElecSystemDDLService; import com.itheima.elec.service.IElecTextService; @SuppressWarnings("serial") @Controller("elecSystemDDLAction") @Scope(value="prototype") public class ElecSystemDDLAction extends BaseAction<ElecSystemDDL>{ ElecSystemDDL elecSystemDDL = this.getModel(); //注入Service @Resource(name=IElecSystemDDLService.SERVICE_NAME) IElecSystemDDLService elecSystemDDLService; /** * 数据字典的首页显示 * 跳转到:/system/dictionIndex.jsp页面 */ public String home(){ return "home"; } }
struts.xml
<action name="elecSystemDDLAction_*" class="elecSystemDDLAction" method="{1}">
<result name="home">/WEB-INF/page/system/dictionaryIndex.jsp</result>
</action>
写完了dao,service,action,然后配置struts.xml
然后修改script/menuDate.js中的配置
{ mid:'aq', pid:'am', name:'数据字典维护', icon:'../images/MenuIcon/shujuzidianguanli.gif', target:'mainFrame', /*url:'../system/dictionaryIndex.jsp',*/ url:'../system/elecSystemDDLAction_home.do', isParent:false }
启动,页面显示: