Valid XHTML 1.0 Transitional集成演练路线图 (P20)

Page: [index.html] (v2017-03-01_21-00)

  1. 项目基础:……
  2. Servlet :……
  3. Spring Web MVC :
    1. 基本 Web 应用:……
    2. CRUD :
      1. Spring Web MVC + DataSource + 基本 JDBC :……
      2. Spring Web MVC + DataSource + Spring JDBC Template :……
      3. Spring Web MVC + DataSource + MyBatis :……
      4. Spring Web MVC + DataSource + Hibernate :
        1. 方式 - 基本集成:……
        2. 方式 - 进阶集成:
          • 样例 - 用户管理:
            1. 环境准备:
              修改 pom.xml ,添加需要的库 (并由 Eclipse 的 Maven 插件自动下载、导入) ,以使用 org.mybatis.spring.SqlSessionFactoryBean 类:
              <project ……>
              	……
              	<dependencies>
              		……
              		<dependency>
              			<!-- [Spring 3] and [Hibernate 3] advanced integration. -->
              			<groupId>org.springframework</groupId>
              			<artifactId>spring-hibernate</artifactId>
              			<version>1.2.8</version>
              		</dependency>
              	</dependencies>
              </project>
              

              说明 / 注意事项:
              • 目前通过 Maven 引入的库有:
                antlr-2.7.6.jar
                aopalliance-1.0.jar
                commons-codec-1.10.0.jar
                commons-codec-1.10.jar
                commons-collections-3.1.jar
                commons-dbcp-1.4.jar
                commons-lang-2.1.jar
                commons-logging-1.1.3.jar
                commons-pool-1.5.4.jar
                dom4j-1.6.1.jar
                hamcrest-core-1.3.jar
                hibernate-commons-annotations-3.2.0.Final.jar
                hibernate-core-3.6.8.Final.jar
                hibernate-jpa-2.0-api-1.0.1.Final.jar
                javassist-3.11.0.GA.jar
                jta-1.1.jar
                junit-4.12.jar
                mybatis-3.2.8.jar
                mybatis-spring-1.2.2.jar
                ojdbc5-11.2.0.4.jar
                slf4j-api-1.6.1.jar
                spring-aop-3.2.8.RELEASE.jar
                spring-beans-3.2.8.RELEASE.jar
                spring-context-3.2.8.RELEASE.jar
                spring-core-3.2.8.RELEASE.jar
                spring-expression-3.2.8.RELEASE.jar
                spring-hibernate-1.2.8.jar
                spring-jdbc-3.2.8.RELEASE.jar
                spring-orm-1.2.8.jar
                spring-tx-3.2.8.RELEASE.jar
                spring-web-3.2.8.RELEASE.jar
                spring-webmvc-3.2.8.RELEASE.jar
            2. 样例编写:
              1. 实体类:
                沿用“基本 JDBC CRUD”样例既有的实体类 tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User
              2. DAO 层:
                1. Spring-Hibernate 集成组件介入:
                  Spring-Hibernate 集成组件提供了更强大的 SessionFactory ,无需在 ApplicationContext 中注册过多初级对象,且可直接使用 ApplicationContext 中已注册的数据源,在 Spring 环境中使用更方便
                  1. 修改 ApplicationContext 分项配置文件 src/main/resources/tfw/integration_demo/_04_spring_web_mvc/sub_files/05_hibernate_crud.xml ,注册新型 SessionFactory 对象并指定 ID
                    <?xml version="1.0" encoding="UTF-8"?>
                    <beans ……>
                    	……
                    
                    	<bean id="crud.hibernate.urlMapping"
                    			……>
                    		……
                    	</bean>
                    
                    	<!-- [########################################] -->
                    
                    	<bean id="crud.hibernate.advanced.sf"
                    			class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                    		<property name="dataSource" ref="ds"/>
                    		<property name="hibernateProperties">
                    			<props>
                    				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                    				<prop key="hibernate.show_sql">true</prop>
                    				<prop key="hibernate.formate_sql">true</prop>
                    			</props>
                    		</property>
                    		<property name="mappingResources">
                    			<list>
                    				<value>tfw/integration_demo/_04_spring_web_mvc/_02_crud/_04_hibernate_crud/dao/impl/oracle/User.hibernate_mapping.xml</value>
                    			</list>
                    		</property>
                    	</bean>
                    </beans>
                    

                  2. 编写测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._02_advanced_way._01_HibernateDAO_with_Spring ,确认新型 SessionFactory 对象配置正确:
                    ID
                    public class _01_HibernateDAO_with_Spring
                    {
                    
                    	private String strSpringClassPathConfFile =
                    			"tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml";
                    
                    	@Test
                    	public void testObjects2() throws IOException
                    	{
                    		System.out.println(
                    				"Spring Configuration File:\n\t" + strSpringClassPathConfFile);
                    
                    		AbstractApplicationContext actxt =
                    				new ClassPathXmlApplicationContext(strSpringClassPathConfFile);
                    		System.out.println("Application Context:\n\t" + actxt);
                    
                    		SessionFactory sf = actxt.getBean("crud.hibernate.advanced.sf",
                    				SessionFactory.class);
                    		System.out.println("SessionFactory:\n\t" + sf);
                    
                    		Session session = sf.openSession();
                    		System.out.println("Session:\n\t" + session);
                    
                    		Transaction ts = session.beginTransaction();
                    		System.out.println("Transaction:\n\t" + ts);
                    
                    		session.close();
                    		System.out.println("Session Closed:\n\t" + session);
                    
                    		actxt.close();
                    		System.out.println("Application Context Closed:\n\t" + actxt);
                    	}
                    
                    }
                    

                2. DAO 实现类:
                  1. 沿用“基本 Hibernate 集成”样例既有的 DAO 类 tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User
                    修改 ApplicationContext 分项配置文件,用新的 ID 注册 DAO ,并向其注入新型 SessionFactory 对象
                    <?xml version="1.0" encoding="UTF-8"?>
                    <beans ……>
                    	……
                    
                    	<!-- [########################################] -->
                    
                    	<bean id="crud.hibernate.advanced.sf"
                    			……>
                    		……
                    	</bean>
                    
                    	<bean id="crud.hibernate.advanced.userDAO"
                    			class="tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud._01_basic_way.dao.UserDAO">
                    		<constructor-arg ref="crud.hibernate.advanced.sf"/>
                    	</bean>
                    </beans>
                    

                  2. 改造测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._02_advanced_way._01_HibernateDAO_with_Spring ,修改、添加方法,测试 DAO 类:
                    ……
                    public class _01_HibernateDAO_with_Spring extends A_UserMgrTestCaseBase
                    {
                    
                    	private String strSpringClassPathConfFile =
                    			"tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml";
                    
                    	private AbstractApplicationContext actxt;
                    
                    	private I_UserDAO userDAO;
                    
                    	@Test
                    	public void testObjects2() throws IOException
                    	{
                    		……
                    
                    		session.close();
                    		System.out.println("Session Closed:\n\t" + session);
                    
                    		I_UserDAO userDAO =
                    				actxt.getBean("crud.hibernate.advanced.userDAO", I_UserDAO.class);
                    		System.out.println("DAO:\n\t" + userDAO);
                    
                    		actxt.close();
                    		System.out.println("Application Context Closed:\n\t" + actxt);
                    	}
                    
                    	@Before
                    	public void init() throws IOException
                    	{
                    		System.out.println(
                    				"Spring Configuration File:\n\t" + strSpringClassPathConfFile);
                    
                    		actxt = new ClassPathXmlApplicationContext(strSpringClassPathConfFile);
                    		System.out.println("Application Context:\n\t" + actxt);
                    
                    		userDAO = actxt.getBean("crud.hibernate.advanced.userDAO",
                    				I_UserDAO.class);
                    		System.out.println("DAO:\n\t" + userDAO);
                    	}
                    
                    	@After
                    	public void close()
                    	{
                    		actxt.close();
                    		System.out.println("Application Context Closed:\n\t" + actxt);
                    	}
                    
                    	@Test
                    	public void testUserDAO_create() throws SQLException
                    	{
                    		……
                    	}
                    
                    	……
                    
                    }
                    

              3. 业务层:
                1. 沿用“基本 JDBC CRUD”样例既有的业务层类
                  修改 ApplicationContext 分项配置文件 src/main/resources/tfw/integration_demo/_04_spring_web_mvc/sub_files/05_hibernate_crud.xml ,以新的 ID 注册业务层对象,并向其注入 DAO
                  <?xml version="1.0" encoding="UTF-8"?>
                  <beans ……>
                  	……
                  
                  	<bean id="crud.hibernate.advanced.userDAO"
                  			……>
                  		……
                  	</bean>
                  
                  	<bean id="crud.hibernate.advanced.userMgrSvc"
                  			class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.service._01_UserMgrService">
                  		<property name="userDAO" ref="crud.hibernate.advanced.userDAO"/>
                  	</bean>
                  </beans>
                  

                2. 将测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._02_advanced_way._01_HibernateDAO_with_Spring 改名为 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._02_advanced_way._01_HibernateDAO_and_Service_with_Spring ,修改、添加方法,测试业务层类:
                  ……
                  public class _01_HibernateDAO_and_Service_with_Spring
                  		extends A_UserMgrTestCaseBase
                  {
                  
                  	……
                  
                  	private I_UserDAO userDAO;
                  
                  	private _01_UserMgrService userMgrSvc;
                  
                  	@Test
                  	public void testObjects2() throws IOException
                  	{
                  		……
                  
                  		I_UserDAO userDAO =
                  				actxt.getBean("crud.hibernate.advanced.userDAO", I_UserDAO.class);
                  		System.out.println("DAO:\n\t" + userDAO);
                  
                  		_01_UserMgrService userMgrSvc = actxt.getBean(
                  				"crud.hibernate.advanced.userMgrSvc", _01_UserMgrService.class);
                  		System.out.println("Service:\n\t" + userMgrSvc);
                  
                  		actxt.close();
                  		System.out.println("Application Context Closed:\n\t" + actxt);
                  	}
                  
                  	@Before
                  	public void init() throws IOException
                  	{
                  		……
                  
                  		userDAO = actxt.getBean("crud.hibernate.advanced.userDAO",
                  				I_UserDAO.class);
                  		System.out.println("DAO:\n\t" + userDAO);
                  
                  		userMgrSvc = actxt.getBean("crud.hibernate.advanced.userMgrSvc",
                  				_01_UserMgrService.class);
                  		System.out.println("Service:\n\t" + userMgrSvc);
                  	}
                  
                  	@After
                  	public void close()
                  	{
                  		……
                  	}
                  
                  	……
                  
                  	@Test
                  	public void testUserDAO_discardById() throws SQLException
                  	{
                  		……
                  	}
                  
                  	@Test
                  	public void testUserMgrSvc_createUser() throws SQLException
                  	{
                  		……
                  	}
                  
                  	……
                  
                  }
                  

              4. 表示层:
                1. 控制器逻辑同“Spring JDBC Template CRUD”样例,直接沿用既有的“控制器”类;
                  修改 ApplicationContext 分项配置文件 src/main/resources/tfw/integration_demo/_04_spring_web_mvc/sub_files/05_hibernate_crud.xml ,注册此“控制器”对象、指定 ID ,并向其注入业务层对象、页面标题和跳转目标:
                  <?xml version="1.0" encoding="UTF-8"?>
                  <beans ……>
                  	……
                  
                  	<bean id="crud.hibernate.advanced.userMgrSvc"
                  			……>
                  		……
                  	</bean>
                  
                  	<bean id="crud.hibernate.advanced.userMgrCtrl"
                  			class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.controller._01_UserMgrController">
                  		<property name="userMgrSvc" ref="crud.hibernate.advanced.userMgrSvc"/>
                  		<property name="pageTitle"
                  				value="Integration&amp;nbsp;Demo / 04&amp;nbsp;Spring&amp;nbsp;Web&amp;nbsp;MVC / 02&amp;nbsp;Create,&amp;nbsp;Read,&amp;nbsp;Update&amp;nbsp;&amp;amp;&amp;nbsp;Discard / 04&amp;nbsp;Hibernate&amp;nbsp;CRUD / 02&amp;nbsp;Advanced&amp;nbsp;Integration / 01&amp;nbsp;User&amp;nbsp;Management - "/>
                  		<property name="jumpTarget">
                  			<props>
                  				<prop key="result">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/result.jsp</prop>
                  				<prop key="detail">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/detail.jsp</prop>
                  				<prop key="login">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/04_hibernate_crud/02_advanced_way/01_user_mgr/login.html</prop>
                  				<prop key="list">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/list.jsp</prop>
                  			</props>
                  		</property>
                  	</bean>
                  </beans>
                  

                2. 修改 ApplicationContext 分项配置文件 src/main/resources/tfw/integration_demo/_04_spring_web_mvc/sub_files/05_hibernate_crud.xml ,将 URI /tfw/integration_demo/04_spring_web_mvc/02_crud/04_hibernate_crud/02_advanced_way/01_user_mgr/action.spr 映射至此“控制器”对象
                  <?xml version="1.0" encoding="UTF-8"?>
                  <beans ……>
                  	……
                  
                  	<bean id="crud.hibernate.basic.userMgrCtrl"
                  			……>
                  		……
                  	</bean>
                  
                  	<bean id="crud.hibernate.urlMapping"
                  			class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                  		<property name="mappings">
                  			<props>
                  				<prop key="/tfw/integration_demo/04_spring_web_mvc/02_crud/04_hibernate_crud/01_basic_way/01_user_mgr/action.spr">crud.hibernate.basic.userMgrCtrl</prop>
                  				<prop key="/tfw/integration_demo/04_spring_web_mvc/02_crud/04_hibernate_crud/02_advanced_way/01_user_mgr/action.spr">crud.hibernate.advanced.userMgrCtrl</prop>
                  			</props>
                  		</property>
                  	</bean>
                  
                  	<!-- [########################################] -->
                  
                  	<bean id="crud.hibernate.advanced.sf"
                  			……>
                  		……
                  	</bean>
                  
                  	……
                  </beans>
                  

                3. 修改 或/及 新增相关的索引页,添加指向此控制器的 URI ,确认控制器配置正确。
                4. 编写本样例的专用页面 src/main/webapp/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/04_hibernate_crud/02_advanced_way/01_user_mgr/login.html
                5. 测试本样例的“登录”和增删改查功能。
  4. ……