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

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 :……
      4. Spring Web MVC + DataSource + Hibernate :
        • 方式 - 基本集成:
          • 样例 - 用户管理:
            1. 环境准备:……
            2. 样例编写:
              1. 实体类:……
              2. DAO 层:
                1. Hibernate 功能验证:
                  1. 环境准备:……
                  2. 功能实现:……
                  说明 / 注意事项:
                  • Hibernate 将查询结果转换成实体对象 / 列表的过程中,所有 ID 值为 null 的行在查询中均被转换为 null ,无论表中其他列的值是否为 null 。
                2. DAO 实现类:
                  1. 编写 Hibernate DAO 实现类 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud._01_basic_way.dao.UserDAO ,在功能验证的基础上添加一定程度的参数检查和预处理,并统一进行异常和收尾处理:
                    ……
                    public class UserDAO implements I_UserDAO
                    {
                    
                    	private SessionFactory sf;
                    
                    	……
                    	public UserDAO(SessionFactory sf)
                    	{
                    		this.sf = sf;
                    	}
                    
                    	……
                    	public int create(User user) throws SQLException
                    	{
                    		if (null == user)
                    		{
                    			return -1;
                    		}
                    
                    		Session session = null;
                    
                    		try
                    		{
                    			session = sf.openSession();
                    			Transaction ts = session.beginTransaction();
                    			Serializable srRtn = session.save(user);
                    			ts.commit();
                    
                    			int intJdbcCode = NumberToolE.o2i(srRtn, 0);
                    			return intJdbcCode;
                    		}
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public User findById(Integer itgUserId) throws SQLException
                    	{
                    		SqlSession session = null;
                    
                    		try
                    		{
                    			session = sf.openSession();
                    			User user = (User) session.get(User.class, itgUserId);
                    			return user;
                    		}
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public List<User> listByName(String strUserName) throws SQLException
                    	{
                    		Session session = null;
                    
                    		try
                    		{
                    			String strHql =
                    					"from User where name="
                    							+ (null == strUserName ? null
                    									: "'" + strUserName + "'")
                    							+ " order by name, id";
                    			System.out.println("HQL:\n\t" + strHql);
                    
                    			session = session = sf.openSession();
                    			Query query = session.createQuery(strHql);
                    			@SuppressWarnings("unchecked")
                    			List<User> lstUsers = query.list();
                    
                    			return lstUsers;
                    		}
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public List<User> listAll() throws SQLException
                    	{
                    		Session session = null;
                    
                    		try
                    		{
                    			session = sf.openSession();
                    			Query query = session.getNamedQuery(
                    					"tfw.dash_01._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll");
                    			@SuppressWarnings("unchecked")
                    			List<User> lstUsers = query.list();
                    
                    			return lstUsers;
                    		}
                    		catch (Exception e)
                    		{
                    			throw toSQLException(e);
                    		}
                    		finally
                    		{
                    			finallyOperation(session);
                    		}
                    	}
                    
                    	……
                    	public List<User> listByRange(int intStartRowIdx, int intSelectRowCount)
                    			throws SQLException
                    	{
                    		Session session = null;
                    
                    		try
                    		{
                    			session = session = sf.openSession();
                    			Query query = session.getNamedQuery(
                    					"tfw.dash_01._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll2");
                    
                    			query.setFirstResult(intStartRowIdx);
                    			query.setMaxResults(intSelectRowCount);
                    
                    			@SuppressWarnings("unchecked")
                    			List<User> lstUsers = query.list();
                    
                    			return lstUsers;
                    		}
                    		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();
                    			}
                    		}
                    	}
                    
                    }
                    

                    说明 / 注意事项:
                    • toSQLException(……);”、“finallyOperation(……);”:
                      各数据操作方法采用统一的异常处理收尾处理
                  2. 编写测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._02_HibernateDAO ,测试 DAO 类中的各方法:
                    ……
                    public class _02_HibernateDAO extends A_UserMgrTestCaseBase
                    {
                    
                    	private String strClasspathConfFile =
                    			"tfw/integration_demo/_04_spring_web_mvc/_02_crud/_04_hibernate_crud/hibernate-config.xml";
                    
                    	private I_UserDAO userDAO;
                    
                    	@Test
                    	public void testObjects() throws IOException
                    	{
                    		Configuration conf = new Configuration();
                    		System.out.println("Configuration:\n\t" + conf);
                    
                    		System.out.println("Configuration File:\n\t" + strClasspathConfFile);
                    		conf.configure(strClasspathConfFile);
                    		System.out.println("Configuration:\n\t" + conf);
                    
                    		SessionFactory sf = conf.buildSessionFactory();
                    		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);
                    
                    		I_UserDAO userDAO = new UserDAO(sf);
                    		System.out.println("DAO:\n\t" + userDAO);
                    	}
                    
                    	@Before
                    	public void init() throws IOException
                    	{
                    		……
                    
                    		SessionFactory sf = conf.buildSessionFactory();
                    		System.out.println("SessionFactory:\n\t" + sf);
                    
                    		userDAO = new UserDAO(sf);
                    		System.out.println("DAO:\n\t" + userDAO);
                    	}
                    
                    	@Test
                    	public void testUserDAO_create() throws SQLException
                    	{
                    		……
                    	}
                    
                    	……
                    
                    }
                    

                    说明 / 注意事项:
                    • 此样例目前尚未和 Spring 发生关系,所以不涉及 Spring 的 XML 文件Spring 注解
                      目前所有依赖都以写死或者 new 的方式自行解决。
              3. 业务层:
                沿用“基本 JDBC CRUD”样例既有的业务层类
                将测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._02_HibernateDAO 改名为 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._02_HibernateDAO_and_Service ,修改、添加测试方法,测试业务层类。
                ……
                public class _02_HibernateDAO_and_Service extends A_UserMgrTestCaseBase
                {
                
                	……
                
                	private I_UserDAO userDAO;
                
                	private _01_UserMgrService userMgrSvc;
                
                	@Test
                	public void testObjects() throws IOException
                	{
                		……
                
                		I_UserDAO userDAO = new UserDAO(sf);
                		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(sf);
                		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
                	{
                		……
                	}
                
                	……
                
                }
                

              4. ……