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

Page: [index.html] (v2017-03-01_19-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 :
        1. 方式 - 人工编写 DAO 实现类:
          • 样例 - 用户管理:
            1. 环境准备:……
            2. 样例编写:
              1. 实体类:……
              2. DAO 层:
                1. MyBatis 功能验证:……
                2. DAO 实现类:
                  1. 编写 MyBatis DAO 实现类 tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.dao.impl.UserDAO ,在功能验证的基础上添加一定程度的参数检查和预处理,并统一进行异常和收尾处理:
                    ……
                    public class UserDAO implements I_UserDAO
                    {
                    
                    	private SqlSessionFactory ssf;
                    
                    	private final String MYBATIS_NAMESPACE = "crud.mybatis.user";
                    
                    	……
                    	public UserDAO(SqlSessionFactory ssf)
                    	{
                    		this.ssf = ssf;
                    	}
                    
                    	……
                    	public int create(User user) throws SQLException
                    	{
                    		if (null == user)
                    		{
                    			return -1;
                    		}
                    
                    		SqlSession session = null;
                    
                    		try
                    		{
                    			final String MYBATIS_OPERATION_ID = "create";
                    			final String MYBATIS_OPERATION =
                    					MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID;
                    			System.out.println(MYBATIS_OPERATION);
                    
                    			session = ssf.openSession();
                    			String strSql = MyBatisSqlHelper.getNamespaceSql(session,
                    					MYBATIS_OPERATION, user);
                    			System.out.println(strSql);
                    
                    			int intJdbcCode = session.insert(MYBATIS_OPERATION, user);
                    			session.commit();
                    			return intJdbcCode;
                    		}
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public User findById(Integer itgUserId) throws SQLException
                    	{
                    		SqlSession session = null;
                    
                    		try
                    		{
                    			final String MYBATIS_OPERATION = MYBATIS_NAMESPACE + ".findById";
                    			……
                    			return ……;
                    		}
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public List<User> listByName(String strUserName) throws SQLException
                    	{
                    		……
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public List<User> listAll() throws SQLException
                    	{
                    		……
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public List<User> listByRange(int intStartRowIdx, int intSelectRowCount)
                    			throws SQLException
                    	{
                    		……
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public int update(User user) throws SQLException
                    	{
                    		if (null == user)
                    		{
                    			return -1;
                    		}
                    
                    		……
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public int discardById(Integer itgUserId) throws SQLException
                    	{
                    		……
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public String toString()
                    	{
                    		……
                    	}
                    
                    	……
                    	private SQLException toSQLException(Exception e)
                    	{
                    		if (null == e)
                    		{
                    			return new SQLException("Unknown database operation exception!");
                    		}
                    		else if (e instanceof SQLException)
                    		{
                    			return (SQLException) e;
                    		}
                    		else
                    		{
                    			return new SQLException(e);
                    		}
                    	}
                    
                    	……
                    	private void finallyOperation(SqlSession session)
                    	{
                    		if (null != session)
                    		{
                    			try
                    			{
                    				session.close();
                    			}
                    			catch (Exception e)
                    			{
                    				e.printStackTrace();
                    			}
                    		}
                    	}
                    
                    }
                    

                    说明 / 注意事项:
                    1. private final String MYBATIS_NAMESPACE = "crud.mybatis.user";”:
                      DAO 实现类通过命名空间 MyBatis 映射文件中的节点发生关联。
                    2. toSQLException(……);”、“finallyOperation(……);”:
                      各数据操作方法采用统一的异常处理收尾处理
                  2. 编写测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.user_mgr_testcase._02_MyBatisDAO ,测试 DAO 类中的各方法:
                    ……
                    public class _02_MyBatisDAO
                    {
                    
                    	private String strConfFile =
                    			"src/main/resources/tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/mybatis-config.xml";
                    
                    	private I_UserDAO userDAO;
                    
                    	@Test
                    	public void testObjects() throws IOException
                    	{
                    		SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
                    		System.out.println("SqlSessionFactoryBuilder:\n\t" + ssfb);
                    
                    		System.out.println("Configuration File:\n\t" + strConfFile);
                    		InputStream isMyBatisCfg = new FileInputStream(strConfFile);
                    		System.out.println("Configuration InputStream:\n\t" + isMyBatisCfg);
                    
                    		SqlSessionFactory ssf = ssfb.build(isMyBatisCfg);
                    		System.out.println("SqlSessionFactory:\n\t" + ssf);
                    		isMyBatisCfg.close();
                    		System.out.println(
                    				"Configuration InputStream Closed:\n\t" + isMyBatisCfg);
                    
                    		SqlSession session = ssf.openSession();
                    		System.out.println("SqlSession:\n\t" + session);
                    		session.close();
                    		System.out.println("SqlSession Closed:\n\t" + session);
                    
                    		I_UserDAO userDAO = new UserDAO(ssf);
                    		System.out.println("DAO:\n\t" + userDAO);
                    	}
                    
                    	@Before
                    	public void init() throws IOException
                    	{
                    		……
                    
                    		SqlSessionFactory ssf = ssfb.build(isMyBatisCfg);
                    		System.out.println("SqlSessionFactory:\n\t" + ssf);
                    		isMyBatisCfg.close();
                    		System.out.println(
                    				"Configuration InputStream Closed:\n\t" + isMyBatisCfg);
                    
                    		userDAO = new UserDAO(ssf);
                    		System.out.println("DAO:\n\t" + userDAO);
                    	}
                    
                    	@After
                    	public void close()
                    	{
                    		……
                    	}
                    
                    	@Test
                    	public void testUserDAO_create() throws SQLException
                    	{
                    		……
                    	}
                    
                    	……
                    
                    	private void showUsers(List<User> lstUsers)
                    	{
                    		……
                    	}
                    
                    }
                    

                    说明 / 注意事项:
                    1. 此样例目前尚未和 Spring 发生关系,所以不涉及 Spring 的 XML 文件Spring 注解
                      目前所有依赖都以写死或者 new 的方式自行解决。
                    2. @After ……”:
                      本测试用例用到外部资源的“session”,在 DAO 对象工作过程中创建、关闭及销毁,无需再在本测试用例中处理。
              3. 业务层:
                沿用“基本 JDBC CRUD”样例既有的业务层类
                将测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.user_mgr_testcase._02_MyBatisDAO 改名为 tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.user_mgr_testcase._02_MyBatisDAO_and_Service ,修改、添加测试方法,测试业务层类。
                ……
                public class _02_MyBatisDAO_and_Service
                {
                
                	……
                
                	private I_UserDAO userDAO;
                
                	private _01_UserMgrService userMgrSvc;
                
                	@Test
                	public void testObjects() throws IOException
                	{
                		……
                
                		I_UserDAO userDAO = new UserDAO(ssf);
                		System.out.println("DAO:\n\t" + userDAO);
                
                		_01_UserMgrService userMgrSvc = new _01_UserMgrService();
                		userMgrSvc.setUserDAO(userDAO);
                		System.out.println("Service:\n\t" + userMgrSvc);
                	}
                
                	@Before
                	public void init() throws IOException
                	{
                		……
                
                		userDAO = new UserDAO(ssf);
                		System.out.println("DAO:\n\t" + userDAO);
                
                		userMgrSvc = new _01_UserMgrService();
                		userMgrSvc.setUserDAO(userDAO);
                		System.out.println("Service:\n\t" + userMgrSvc);
                	}
                
                	@Test
                	public void testUserDAO_create() throws SQLException
                	{
                		……
                	}
                
                	……
                
                	@Test
                	public void testUserDAO_discardById() throws SQLException
                	{
                		……
                	}
                
                	@Test
                	public void testUserMgrSvc_createUser() throws SQLException
                	{
                		……
                	}
                
                	……
                
                	private void showUsers(List<User> lstUsers)
                	{
                		……
                	}
                
                }
                

                说明 / 注意事项:
                • 此样例目前尚未和 Spring 发生关系,所以不涉及 ApplicationContext 配置文件Spring 注解
                  目前所有依赖都以写死或者 new 的方式自行解决。
              4. ……