今天我就把上个月搭的MVC2+Spring.net+Nhibernate+Jquery开发框架介绍给大家,虽然本框架已经被老板“下架”,但是我还是想把这个实践经验介绍给大家。
首先先看看文件夹结构。

先看看Dao层。


其中Implement下放的是实现接口的Dao。Interface下放的是接口。我们再看看实现类BaseDao










































































BaseDao去实现IDao类。我们再看看每个Dao类去继承BaseDao实现对应的接口

















我们再看看其对应的接口由于篇幅有限,我们接着看Model层

其中Models文件夹下放的是实体类,Mappings下放的是Nhibernate映射的xml文件。
Util下放的是要使用的关于加载Json解析的类。
接下来我们再看看我的程序集SpringConfig,其中的Controller_Dao.xml是对控制器中所使用用到的Dao注入到控制器。Dao.xml是对Dao的Spring配置。Spring_Hibernate.xml是对Spring+Nhibernate组合的一个配置。
我们大致来看看,Dao.xml
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< objects xmlns ="http://www.springframework.net"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
< object id ="CodeDao" type ="Dao.Implement.SysManager.SysCodeDao,Dao" autowire ="byName" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysOrgDao" type ="Dao.Implement.SysManager.SysOrgDao,Dao" autowire ="byName" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysCodeDao" type ="Dao.Implement.SysManager.SysCodeDao,Dao" autowire ="byName" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysUserDao" type ="Dao.Implement.SysManager.SysUserDao,Dao" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysLogInfoDao" type ="Dao.Implement.SysManager.SysLogInfoDao" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysModuleDao" type ="Dao.Implement.SysManager.SysModulDao" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
</ objects >
< objects xmlns ="http://www.springframework.net"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
< object id ="CodeDao" type ="Dao.Implement.SysManager.SysCodeDao,Dao" autowire ="byName" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysOrgDao" type ="Dao.Implement.SysManager.SysOrgDao,Dao" autowire ="byName" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysCodeDao" type ="Dao.Implement.SysManager.SysCodeDao,Dao" autowire ="byName" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysUserDao" type ="Dao.Implement.SysManager.SysUserDao,Dao" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysLogInfoDao" type ="Dao.Implement.SysManager.SysLogInfoDao" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
< object id ="sysModuleDao" type ="Dao.Implement.SysManager.SysModulDao" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ object >
</ objects >
Spring_Dao.xml
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< objects xmlns ="http://www.springframework.net"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
<!-- 部门管理所需加载的对象 -->
< object id ="ssCodeId" type ="Model.SS_CODEID,Model" autowire ="byName" />
< object id ="ssOrga1" type ="Model.SS_ORGA,Model" autowire ="byName" > </ object >
< object id ="ssCode" type ="Model.SS_CODE,Model" autowire ="byName" > </ object >
< object id ="ListItem" type="Utility.Common.ListItem<Model.SS_CODE >,Utility" autowire="byName">
</ object >
< object id ="OrgaController" type ="EduCationManage.Controllers.SysManager.OrgaController,EduCationManage" singleton ="false" >
< property name ="ssOrga" ref ="ssOrga1" > </ property >
< property name ="ssCode" ref ="ssCode" > </ property >
< property name ="sysOrgDao" ref ="sysOrgDao" > </ property >
< property name ="sysCodeDao" ref ="sysCodeDao" > </ property >
< property name ="ssCodeId" ref ="ssCodeId" > </ property >
< property name ="myListItem" ref ="ListItem" > </ property >
</ object >
< object id ="ssUser1" type ="Model.SS_USER,Model" autowire ="byName" > </ object >
< object id ="ssLogInfos" type ="Model.SS_LOG_INFO,Model" autowire ="byName" > </ object >
< object id ="LoginController" type ="EduCationManage.Controllers.LoginController,EduCationManage" singleton ="false" >
< property name ="ssUser" ref ="ssUser1" />
< property name ="ssLogInfos" ref ="ssLogInfos" > </ property >
< property name ="sysUserDao" ref ="sysUserDao" > </ property >
< property name ="sysLogInfoDao" ref ="sysLogInfoDao" > </ property >
< property name ="sysModuleDao" ref ="sysModuleDao" > </ property >
</ object >
</ objects >
< objects xmlns ="http://www.springframework.net"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
<!-- 部门管理所需加载的对象 -->
< object id ="ssCodeId" type ="Model.SS_CODEID,Model" autowire ="byName" />
< object id ="ssOrga1" type ="Model.SS_ORGA,Model" autowire ="byName" > </ object >
< object id ="ssCode" type ="Model.SS_CODE,Model" autowire ="byName" > </ object >
< object id ="ListItem" type="Utility.Common.ListItem<Model.SS_CODE >,Utility" autowire="byName">
</ object >
< object id ="OrgaController" type ="EduCationManage.Controllers.SysManager.OrgaController,EduCationManage" singleton ="false" >
< property name ="ssOrga" ref ="ssOrga1" > </ property >
< property name ="ssCode" ref ="ssCode" > </ property >
< property name ="sysOrgDao" ref ="sysOrgDao" > </ property >
< property name ="sysCodeDao" ref ="sysCodeDao" > </ property >
< property name ="ssCodeId" ref ="ssCodeId" > </ property >
< property name ="myListItem" ref ="ListItem" > </ property >
</ object >
< object id ="ssUser1" type ="Model.SS_USER,Model" autowire ="byName" > </ object >
< object id ="ssLogInfos" type ="Model.SS_LOG_INFO,Model" autowire ="byName" > </ object >
< object id ="LoginController" type ="EduCationManage.Controllers.LoginController,EduCationManage" singleton ="false" >
< property name ="ssUser" ref ="ssUser1" />
< property name ="ssLogInfos" ref ="ssLogInfos" > </ property >
< property name ="sysUserDao" ref ="sysUserDao" > </ property >
< property name ="sysLogInfoDao" ref ="sysLogInfoDao" > </ property >
< property name ="sysModuleDao" ref ="sysModuleDao" > </ property >
</ object >
</ objects >
Spring_Hibernate.xml
ok,上面的就是本项目比较关键的配置。紧接着我们赶紧看看控制器



































































我们在使用类的时候,只需要声明无需实例化,因为实例化这个过程由Spring容器管理。还要讲的是,有接口的类我们都是用接口声明,而在spring配置中只要配置成实现类即可,这样有利于代码的安全。
再看看页面
<
%@ Page
Language
="C#" Inherits="System.Web.Mvc.ViewPage<Model.Models.SystemManage.Orga.SS_ORGA
>" %>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title >OrgaManage </title>
< link rel ="Stylesheet" href ="../../Content/form.css" />
< script type ="text/javascript" src ="../../Scripts/Project/SysManages/orgmanager.js" > </script>
</head>
< body >
< div id ="orgWapper" style ="overflow: hidden; margin: 0; padding-right: 0;" >
<div id="Basic_html" style="float: left; overflow: hidden; width: 16%; margin-left: 0px;
padding: 0;">
</div>
< div style ="overflow: auto; padding: 0; margin: 0;" >
< div id ="orgRightPane" style ="margin-left: 0px; padding: 0; float: right;" >
< table id ="dataGrid" >
</table>
< div id ="pager" style ="text-align: right; clear: both;" >
</div>
</div>
</div>
</div>
<div id="content" class="content ui-widjet window" style="text-align: center; width: 460px;
height: 260px; display: none">
< div class ="headbar" >
<img id="img_close" src="../../../Images/gbb.gif" style="float: right; cursor: pointer;"
alt="" />
<span style="float: left; height: 30px; line-height: 30px; color: #fff; font-size: 14px;
font-weight: bolder"> 部门信息 </span>
</div>
<% using (Html.BeginForm("", "Orga", FormMethod.Post, new { id = "orgaForm" }))
{ %>
< %: Html.HiddenFor(m = > m.C_FATHER_ID, new { id = "orgaFatherId", name = "CFatherId" })%>
< script language ="javascript" type ="text/javascript" >
var x = document.getElementById("orgaFatherId");
alert(x.value);
</script>
< %: Html.HiddenFor(m = > m.C_ORGA_ID, new { id = "orgaId", name = "COrgaId" })%>
< table border ="0" class ="tableStyle" >
< tr >
< td >
</td>
</tr>
< tr >
< td align ="right" >
部门名称
</td>
< td align ="left" >
< %: Html.TextBoxFor(m = > m.C_ORGA_NM, new { id = "orgaName", cssClass = "text required" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
< td align ="right" >
部门类型
</td>
< td align ="left" >
< %: Html.DropDownListFor(m = > m.C_ORGA_TP, ViewData["listItem"] as SelectList, new { id = "orgaType", name = "COrgaTp" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
</tr>
< tr >
< td align ="right" >
负责人
</td>
< td align ="left" >
< %:Html.TextBoxFor(m= >m.C_LEADER, new { id = "leader", cssClass = "text" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
< td align ="right" >
电话
</td>
< td align ="left" >
< %:Html.TextBoxFor(m = > m.C_PHONE, new { id = "phone", cssClass = "text", Size = "20px" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
</tr>
< tr >
< td align ="right" >
Email
</td>
< td colspan ="3" align ="left" >
< %:Html.TextBoxFor(m= >m.C_EMAIL, new { id = "email", cssClass = "text", Size = "60px" })%>
</td>
</tr>
< tr >
< td align ="right" valign ="top" >
部门描述
</td>
< td colspan ="3" align ="left" >
< %: Html.TextAreaFor(m= >m.C_ORGA_DESC, new { id = "orgaDesc", cssClass = "text", Columns = "58", Rows = "4" })%>
</td>
</tr>
< tr >
< td colspan ="4" align ="center" >
< button id ="orgModified" name ="modified" type ="button" class ="Pcip1" >
修 改
</button>
< button id ="orgSave" name ="orgSave" type ="submit" class ="Pcip1" >
保 存
</button>
< button id ="orgReset" type ="button" name ="orgReset" class ="Pcip1" >
重 置
</button>
< button id ="orgClose" name ="close" type ="button" class ="Pcip1" >
取 消
</button>
</td>
</tr>
</table>
< %} % >
</div>
< div id ="mask" class ="mask" >
</div>
</body>
</html>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title >OrgaManage </title>
< link rel ="Stylesheet" href ="../../Content/form.css" />
< script type ="text/javascript" src ="../../Scripts/Project/SysManages/orgmanager.js" > </script>
</head>
< body >
< div id ="orgWapper" style ="overflow: hidden; margin: 0; padding-right: 0;" >
<div id="Basic_html" style="float: left; overflow: hidden; width: 16%; margin-left: 0px;
padding: 0;">
</div>
< div style ="overflow: auto; padding: 0; margin: 0;" >
< div id ="orgRightPane" style ="margin-left: 0px; padding: 0; float: right;" >
< table id ="dataGrid" >
</table>
< div id ="pager" style ="text-align: right; clear: both;" >
</div>
</div>
</div>
</div>
<div id="content" class="content ui-widjet window" style="text-align: center; width: 460px;
height: 260px; display: none">
< div class ="headbar" >
<img id="img_close" src="../../../Images/gbb.gif" style="float: right; cursor: pointer;"
alt="" />
<span style="float: left; height: 30px; line-height: 30px; color: #fff; font-size: 14px;
font-weight: bolder"> 部门信息 </span>
</div>
<% using (Html.BeginForm("", "Orga", FormMethod.Post, new { id = "orgaForm" }))
{ %>
< %: Html.HiddenFor(m = > m.C_FATHER_ID, new { id = "orgaFatherId", name = "CFatherId" })%>
< script language ="javascript" type ="text/javascript" >
var x = document.getElementById("orgaFatherId");
alert(x.value);
</script>
< %: Html.HiddenFor(m = > m.C_ORGA_ID, new { id = "orgaId", name = "COrgaId" })%>
< table border ="0" class ="tableStyle" >
< tr >
< td >
</td>
</tr>
< tr >
< td align ="right" >
部门名称
</td>
< td align ="left" >
< %: Html.TextBoxFor(m = > m.C_ORGA_NM, new { id = "orgaName", cssClass = "text required" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
< td align ="right" >
部门类型
</td>
< td align ="left" >
< %: Html.DropDownListFor(m = > m.C_ORGA_TP, ViewData["listItem"] as SelectList, new { id = "orgaType", name = "COrgaTp" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
</tr>
< tr >
< td align ="right" >
负责人
</td>
< td align ="left" >
< %:Html.TextBoxFor(m= >m.C_LEADER, new { id = "leader", cssClass = "text" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
< td align ="right" >
电话
</td>
< td align ="left" >
< %:Html.TextBoxFor(m = > m.C_PHONE, new { id = "phone", cssClass = "text", Size = "20px" })%>
< font color ="red" style ="font-size: 20" >* </font>
</td>
</tr>
< tr >
< td align ="right" >
</td>
< td colspan ="3" align ="left" >
< %:Html.TextBoxFor(m= >m.C_EMAIL, new { id = "email", cssClass = "text", Size = "60px" })%>
</td>
</tr>
< tr >
< td align ="right" valign ="top" >
部门描述
</td>
< td colspan ="3" align ="left" >
< %: Html.TextAreaFor(m= >m.C_ORGA_DESC, new { id = "orgaDesc", cssClass = "text", Columns = "58", Rows = "4" })%>
</td>
</tr>
< tr >
< td colspan ="4" align ="center" >
< button id ="orgModified" name ="modified" type ="button" class ="Pcip1" >
修 改
</button>
< button id ="orgSave" name ="orgSave" type ="submit" class ="Pcip1" >
保 存
</button>
< button id ="orgReset" type ="button" name ="orgReset" class ="Pcip1" >
重 置
</button>
< button id ="orgClose" name ="close" type ="button" class ="Pcip1" >
取 消
</button>
</td>
</tr>
</table>
< %} % >
</div>
< div id ="mask" class ="mask" >
</div>
</body>
</html>
废话不多说,我们最后看看公共类。

一个是Json解析,另外的是一些加密,下拉绑定的公用类。
ok,大概就这么介绍下,如果那位仁兄想要我的源代码,请留言,在要之前,确保你的机子上装上了VS2010。
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/343101,如需转载请自行联系原作者