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

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. 功能实现:
                    1. 常规 Session 查询 (按 ID 查询) :……
                    2. HQL 查询:……
                    3. 命名的 HQL 查询:……
                    4. 命名的 SQL 查询:
                      1. 修改 Hibernate 映射文件 src/main/resources/tfw/integration_demo/_04_spring_web_mvc/_02_crud/_04_hibernate_crud/dao/impl/oracle/User.hibernate_mapping.xml ,添加自定义 SQL 查询,并为返回结果指定映射关系:
                        <?xml version="1.0" encoding="utf-8"?>
                        <!DOCTYPE ……>
                        <hibernate-mapping>
                        	<class ……>
                        		……
                        
                        		<!-- Customized queries: -->
                        		<query name="listByName">
                        			……
                        		</query>
                        		<query name="listAll">
                        			……
                        		</query>
                        		<sql-query name="listAll2">
                        			<return class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User">
                        				<return-property name="id" column="id"/>
                        				<return-property name="name" column="name" />
                        				<return-property name="passwd" column="passwd" />
                        				<return-property name="status" column="stat" />
                        				<return-property name="remark" column="remark" />
                        			</return>
                        			select id, name, passwd, stat, remark
                        			from tfw_dash_user
                        			order by name, id
                        		</sql-query>
                        	</class>
                        </hibernate-mapping>
                        

                      2. 改造测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._01_Hibernate_Operation ,添加方法,测试:
                        ……
                        public class _01_Hibernate_Operation
                        {
                        
                        	……
                        
                        	@Test
                        	public void testHibernate_listAll()
                        	{
                        		……
                        	}
                        
                        	@Test
                        	public void testHibernate_listAll2()
                        	{
                        		// Preparing.
                        		Session session = sf.openSession();
                        		System.out.println("Session:\n\t" + session);
                        
                        		// [S] Main.
                        		{
                        			Query query = session.getNamedQuery(
                        					"tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll2");
                        			System.out.println("Query:\n\t");
                        
                        			// [!] User with null ID listed as null row.
                        			@SuppressWarnings("unchecked")
                        			List<User> lstUsers = query.list();
                        
                        			session.close();
                        			System.out.println("Session Closed:\n\t" + session);
                        
                        			showUsers(lstUsers);
                        		}
                        		// [E] Main.
                        	}
                        
                        	private void showUsers(List<User> lstUsers)
                        	{
                        		……
                        	}
                        
                        }
                        

                    5. 分页查询:
                      改造测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._01_Hibernate_Operation ,添加方法,给 Query 对象指定取值范围,测试:
                      ……
                      public class _01_Hibernate_Operation
                      {
                      
                      	……
                      
                      	@Test
                      	public void testHibernate_listAll2()
                      	{
                      		……
                      	}
                      
                      	@Test
                      	public void testHibernate_listByRange()
                      	{
                      		// [S] Preparing.
                      		Session session;
                      		Query query;
                      		{
                      			session = sf.openSession();
                      			System.out.println("Session:\n\t" + session);
                      
                      			// query = session.createQuery("from User order by name, id");
                      			// query = session.getNamedQuery(
                      			// "tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll");
                      			query = session.getNamedQuery(
                      					"tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll2");
                      			System.out.println("Query:\n\t");
                      
                      			// [!] User with null ID listed as null row.
                      			@SuppressWarnings("unchecked")
                      			List<User> lstUsers = query.list();
                      			System.out.println("Session Closed:\n\t" + session);
                      
                      			showUsers(lstUsers);
                      			System.out.println("----------------------------------------");
                      		}
                      		// [S] Preparing.
                      
                      		// [S] Main.
                      		{
                      			query.setFirstResult(15);
                      			query.setMaxResults(10);
                      			System.out.println("Query:\n\t");
                      
                      			// [!] User with null ID listed as null row.
                      			@SuppressWarnings("unchecked")
                      			List<User> lstUsers = query.list();
                      
                      			session.close();
                      			System.out.println("Session Closed:\n\t" + session);
                      
                      			showUsers(lstUsers);
                      		}
                      		// [E] Main.
                      	}
                      
                      	private void showUsers(List<User> lstUsers)
                      	{
                      		……
                      	}
                      
                      }
                      

                      说明 / 注意事项:
                      1. 无论对新建的 Query 对象还是命名的 Query 对象HQL Query 还是 SQL Query ,均适用。
                      2. 观察输出的 SQL ,发现未针对 Oracle 做 rownum 优化。
                    6. 写入:
                      改造测试用例 tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._01_Hibernate_Operation ,添加方法,测试:
                      ……
                      public class _01_Hibernate_Operation
                      {
                      
                      	……
                      
                      	@Before
                      	public void init() throws IOException
                      	{
                      		……
                      	}
                      
                      	@Test
                      	public void testHibernate_create()
                      	{
                      		// [S] Preparing.
                      		User user;
                      		Session session;
                      		Transaction ts;
                      		{
                      			String strDate = DateToolE.simpleFormat(new Date(), "HH-mm-ss.SSS");
                      			String strPasswd = null;
                      			// "Password";
                      			user = // null; // [!][X] Always exception.
                      					new User(null, "Hibernate_N_" + strDate, strPasswd,
                      							new Byte((byte) 0), "Hibernate_R_" + strDate);
                      			System.out.println("User:\n\t" + user);
                      
                      			session = sf.openSession();
                      			System.out.println("Session:\n\t" + session);
                      
                      			ts = session.beginTransaction();
                      			System.out.println("Transaction:\n\t" + ts);
                      		}
                      		// [E] Preparing.
                      
                      		// [S] Main.
                      		{
                      			Serializable srRtn = session.save(user);
                      			System.out.println("Result:\t" + srRtn);
                      
                      			ts.commit();
                      			System.out.println("Transaction Commited:\n\t" + ts);
                      
                      			session.close();
                      			System.out.println("Session Closed:\n\t" + session);
                      
                      			System.out.println("User:\n\t" + user);
                      		}
                      		// [E] Main.
                      	}
                      
                      	@Test
                      	public void testHibernate_findById()
                      	{
                      		……
                      	}
                      
                      	……
                      
                      	@Test
                      	public void testHibernate_listByRange()
                      	{
                      		……
                      	}
                      
                      	@Test
                      	public void testHibernate_update()
                      	{
                      		// [S] Preparing.
                      		Integer itgUserId;
                      		User user;
                      		Session session;
                      		{
                      			itgUserId = // null; // [!] Always exception.
                      					10017;
                      			// 99999;
                      			session = sf.openSession();
                      			System.out.println("Session:\n\t" + session);
                      
                      			user = (User) session.get(User.class, itgUserId);
                      			System.out.println("User (before):\n\t" + user);
                      
                      			session.close();
                      			System.out.println("Session Closed:\n\t" + session);
                      
                      			session = sf.openSession();
                      			System.out.println("Session:\n\t" + session);
                      
                      			String strDate = DateToolE.simpleFormat(new Date(),
                      					"yyyy-MM-ss_HH-mm-ss.SSS");
                      			user = // null; // [!] Always exception.
                      					new User(itgUserId, "Name_" + strDate, "aaa", (byte) 2,
                      							"Remark_" + strDate);
                      			System.out.println("User (new):\n\t" + user);
                      		}
                      		// [E] Preparing.
                      
                      		// [S] Main.
                      		{
                      			Transaction ts = session.beginTransaction();
                      			System.out.println("Transaction:\n\t" + ts);
                      
                      			session.update(user);
                      			System.out.println("Result:\n\tvoid");
                      
                      			ts.commit();
                      			System.out.println("Transaction Commited:\n\t" + ts);
                      		}
                      		// [E] Main.
                      
                      		// [S] Confirmation.
                      		{
                      			user = (User) session.get(User.class, itgUserId);
                      
                      			session.close();
                      			System.out.println("Session Closed:\n\t" + session);
                      
                      			System.out.println("User (updated):\n\t" + user);
                      		}
                      		// [E] Confirmation.
                      	}
                      
                      	@Test
                      	public void testHibernate_discardById()
                      	{
                      		// [S] Preparing.
                      		Integer itgUserId;
                      		User user;
                      		Session session;
                      		{
                      			itgUserId = // null; // [!] Always exception.
                      					10017;
                      			// 99999;
                      			session = sf.openSession();
                      			System.out.println("Session:\n\t" + session);
                      
                      			user = (User) session.get(User.class, itgUserId);
                      			System.out.println("User (before):\n\t" + user);
                      
                      			// session.close();
                      			// System.out.println("Session Closed:\n\t" + session);
                      			//
                      			// session = sf.openSession();
                      			// System.out.println("Session:\n\t" + session);
                      		}
                      		// [E] Preparing.
                      
                      		// [S] Main.
                      		{
                      			Transaction ts = session.beginTransaction();
                      			System.out.println("Transaction:\n\t" + ts);
                      
                      			user.setStatus((byte) -1);
                      			System.out.println("User (new):\n\t" + user);
                      
                      			ts.commit();
                      			System.out.println("Transaction Commited:\n\t" + ts);
                      		}
                      		// [E] Main.
                      
                      		// [S] Confirmation.
                      		{
                      			user = (User) session.get(User.class, itgUserId);
                      
                      			session.close();
                      			System.out.println("Session Closed:\n\t" + session);
                      
                      			System.out.println("User (discarded):\n\t" + user);
                      		}
                      		// [E] Confirmation.
                      	}
                      
                      	private void showUsers(List<User> lstUsers)
                      	{
                      		……
                      	}
                      
                      }
                      

                      说明 / 注意事项:
                      1. 时间有限, Hibernate 的参数验证方式和参数预处理未做研究,交由 DAO 实现类中的代码完成。
                      2. Transaction ts = session.beginTransaction();”、“ts.commit();”:
                        默认情况下,需要用程序代码开启和提交事务,才能令写入操作生效;
                        时间有限,暂不考虑操作失败时回滚。
                      3. user.setStatus(……);”:
                        从 Hibernate Session 中提取的实体对象和 Session 脱离关系之间,对实体对象内容的修改即是对数据库内容的修改;但依然需要提交事务才能生效。
                  ……