现在的位置: 首页 > 综合 > 正文

======================2005-08-20====================

2013年08月30日 ⁄ 综合 ⁄ 共 5613字 ⁄ 字号 评论关闭

======================2005-08-20====================

在做别的工作之前,先把大概的整体思路写一下:
 DAO层,主要完成数据库操作,有FlightDAO接口及其在IMPL里面的实现FlightDAOHiberrnate类,主要完成了与数据库的单表交互的工作;FlightDAO继承了BaseDAO接口,
FlightDAOHiberrnate继承了AbstractDAOHibernate,AbstractDAOHibernate又继承了 HibernateDaoSupport类,这个类是hibernate的内部类。
 Model层,主要是.hbm.xml 生成的文件,FlightCriteria类继承了 BasePO类里面对应有各个字段的值,以及对应的get,set个方法,还有就是hibernate要求的equals和hashCode方法,
以及用来log日志输出时的toString方法。
 Service层,完成一些和逻辑相关的,不是单表操作的一些方法,需要调用dao里面的类的方法,FlightManager接口及其在impl下的实现类FlightManagerImpl,继承了AbstractManager这个抽象类。
 web层,里面有一个Controler包,里面是控制类,对应与各个Form都应该有一个控制类来完成从界面收集参数以及各种操作的处理等操作,
FlightQueryFormController是在dispatcher-servlet.xml中制定的处理"/fq.do"的控制类。

在工程中有一些.properties文件,这个是用来配置一些诸如连接的参数之类的东西的,
如Projects.properties中的代码是:maven.hibernate.input.dir=${basedir}/src/resource
                maven.hibernate.input.includes=**/*.hbm.xml
                maven.hibernate.codeGeneration.input.dir=${maven.hibernate.input.dir}
                maven.hibernate.codeGeneration.input.includes=${maven.hibernate.input.includes}
                maven.hibernate.codeGeneration.output.dir=${maven.src.dir}/java
                
                
                maven.middlegen.hibernate.include=true
                maven.middlegen.run.0=true
                maven.middlegen.0.appname=businessbible
                maven.middlegen.0.database.driver=org.gjt.mm.mysql.Driver
                maven.middlegen.0.database.url=jdbc:mysql://localhost:3306/businessbible
                maven.middlegen.0.database.userid=root
                maven.middlegen.0.database.password=
                maven.middlegen.0.hibernate.columnsincequals=true
                maven.middlegen.0.hibernate.columnsgenproperty=true
                maven.middlegen.0.hibernate.destination=${basedir}/src/resource
                maven.middlegen.0.hibernate.prefsdir=${basedir}/src/conf/
                maven.middlegen.0.hibernate.package=com.sincetimes.businessbible.model
                maven.middlegen.0.hibernate.getxdoclettags=true
                maven.middlegen.0.hibernate.validatable=false
                maven.middlegen.0.hibernate.equalshashcode=false
                maven.middlegen.0.hibernate.selectbeforeupdate=true
                maven.middlegen.0.hibernate.extends=com.sincetimes.businessbible.model.BasePO
                maven.middlegen.0.hibernate.columnsinctostring=true
                maven.middlegen.0.hibernate.dynamicupdate=true
                maven.middlegen.0.hibernate.dynamicinsert=true

今天发现原来model模块中的各个类里面的equals() and hashCode()方法是Hibernate要求的。
在.hbm.xml中的对应一个字段的映射:
              <property name="times" type="int" column="times" not-null="true" length="11">
               <meta attribute="use-in-equals">true</meta>
               <meta attribute="use-in-tostring">true</meta>
              </property>
除了要在hibernate.cfg.xml中配置对应的.hbm.xml的文件的映射以外,在D:/projects/businessbible/src/webapp/WEB-INF/applicationContext-hibernate.xml
下也要影射:
             <property name="mappingResources">
              <list>
               <value>com/sincetimes/businessbible/model/City.hbm.xml</value>
               <value>com/sincetimes/businessbible/model/FlightCriteria.hbm.xml</value>
               <value>com/sincetimes/businessbible/model/Guestbook.hbm.xml</value>
               <value>com/sincetimes/businessbible/model/HotelCriteria.hbm.xml</value>
               <value>com/sincetimes/businessbible/model/LoyaltyPointHistory.hbm.xml</value>
               <value>com/sincetimes/businessbible/model/User.hbm.xml</value>
               <value>com/sincetimes/businessbible/model/UserCity.hbm.xml</value>
                           <value>com/sincetimes/businessbible/model/SurveyGroup.hbm.xml</value>
                           <value>com/sincetimes/businessbible/model/SurveyQuestion.hbm.xml</value>
                           <value>com/sincetimes/businessbible/model/SurveyAnswer.hbm.xml</value>
              </list>
             </property>
             
在web.xml中,有
    <listener>
     <listener-class>
     org.springframework.web.context.ContextLoaderListener
     </listener-class>
    </listener>
这个ServletContextListener在web程序初始化时调用,它自动去寻找Spring’s configuration file at WEB-INF/applicationContext.xml.
也可以自己制定这个位置和文件,
     <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
这个项目里面调用了所有形式为applicationContext*.xml的文件

applicationContext-hibernate.xml中定义了数据库连接,和hibernate的交互等等的东西,“TODO:需要进一步研读。”
 当做好一个DAO中的IMPL之后,还要将这个类在applicationContext-hibernate.xml中定义一下,如下面的代码所示:
   <bean id="abstractDAO" abstract="true" class="com.sincetimes.businessbible.dao.impl.AbstractDAOHibernate">
    <property name="sessionFactory">
     <ref bean="sessionFactory"/>
    </property>
   </bean>
   <bean id="userDAO" class="com.sincetimes.businessbible.dao.impl.UserDAOHibernate" parent="abstractDAO"/>
   <bean id="flightDAO" class="com.sincetimes.businessbible.dao.impl.FlightDAOHiberrnate" parent="abstractDAO"/>
 经过这样的处理以后,spring在处理这个类的时候,就会先看sessionFactory中是否已经存在了session(是否已经在web tier 中打开),
如果已经打开,则就不用新建一个session,而是利用这个session就可以了。这样可以实现Hibernate’s popular “Open Session in View” pattern

在Service下的Manager类的IMPL 要在applicationContext.xml中定义,例如:
   <bean id="userManager" parent="abstractManager" class="com.sincetimes.businessbible.service.impl.UserManagerImpl">
    <property name="cache">
     <ref local="userManagerCache"/>
    </property>
   </bean>
   <bean id="flightManager" parent="abstractManager" class="com.sincetimes.businessbible.service.impl.FlightManagerImpl">
    <property name="cache">
     <ref local="flightManagerCache"/>
    </property>
    <property name="userManager">
     <ref local="userManager"/>
    </property>
   </bean>

抱歉!评论已关闭.