DBUtils框架的使用(下)

  • 2020 年 1 月 19 日
  • 筆記

剛才講了使用QueryRunner插入、修改、更新數據,現在來學習一下使用QueryRunner進行資料庫表查詢。 通過QueryRunner類的query()方法即可完成資料庫表的查詢操作,但是在查詢的時候需要實現ResultSetHandler介面來將結果集封裝成對象。可以通過自己實現介面,但很顯然,我們應該使用DBUtils工具包提供的實現類來實現封裝。 在DBUtils框架中,共提供了九個ResultSetHandler的實現類。

  • ArrayHandler:把結果集中的第一行數據轉成對象數組。
  • ArrayListHandler:把結果集中的每一行數據都轉成一個對象數組,再存放到List中。
  • BeanHandler:將結果集中的第一行數據封裝到一個對應的JavaBean實例中。
  • BeanListHandler:將結果集中的每一行數據都封裝到一個對應的JavaBean實例中,存放到List里。
  • ColumnListHandler:將結果集中某一列的數據存放到List中。
  • MapHandler:將結果集中的第一行數據封裝到一個Map里,key是列名,value就是對應的值。
  • MapListHandler:將結果集中的每一行數據都封裝到一個Map里,然後再存放到List
  • KeyedHandler(name):將結果集中的每一行數據都封裝到一個Map里(List),再把這些map再存到一個map里,其key為指定的key。
  • ScalarHandler:將結果集中的列的資訊轉換到一個對象中

分別通過案例感受一下。 新建測試類ResultSetHandlerTest 然後添加成員變數

private ComboPooledDataSource dataSource = new ComboPooledDataSource();

添加ArrayHandler的測試程式碼

	@Test  	public void testArrayHandler() throws SQLException{  		//ArrayHandler	將結果集的第一行數據存入Object數組  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";    		//數組的每一個元素對應第一行數據的每一列  		Object[] objects = queryRunner.query(sql, new ArrayHandler());  		System.out.println(Arrays.toString(objects));  	}

運行程式碼

添加ArrayListHandler測試程式碼

	@Test  	public void testArrayListHandler() throws SQLException{  		//ArrayListHandler	將結果集的每一行數據存入Object數組,然後存入List  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";  		List<Object[]> list = queryRunner.query(sql, new ArrayListHandler());    		for(Object[] objects : list){  			System.out.println(Arrays.toString(objects));  		}  	}

運行程式碼

添加BeanHandler測試程式碼

	@Test  	public void testBeanHandler() throws SQLException{  		//BeanHandler	將結果集的第一行數據封裝到JavaBean對象中  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";    		//傳入Account.class參數是為了在方法中通過反射構造Account對象實例  		Account account = queryRunner.query(sql, new BeanHandler<Account>(Account.class));  		System.out.println(account.getId());  		System.out.println(account.getName());  		System.out.println(account.getMoney());  	}

運行程式碼

注意事項:使用BeanHandler,表列名必須與Bean類的屬性名稱一致。

添加BeanListHandler測試程式碼

	@Test  	public void testBeanListHandler() throws SQLException{  		//BeanListHandler	將結果集每一條數據都封裝到JavaBean對象,再存入List  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";  		List<Account> list = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));    		for(Account account : list){  			System.out.print(account.getId() + "t");  			System.out.print(account.getName() + "t");  			System.out.print(account.getMoney());  			System.out.println();  		}  	}

運行程式碼

添加ColumnListHandler測試程式碼

	@Test  	public void testColumnListHandler() throws SQLException{  		//ColumnListHandler		獲得結果集的某一列  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";    		//泛型為什麼寫Object	因為每列的類型都不一樣  		List<Object> list = queryRunner.query(sql, new ColumnListHandler("name"));  		System.out.println(list);  	}

運行程式碼

添加MapHandler測試程式碼

	@Test  	public void testMapHandler() throws SQLException{  		//MapHandler	將結果集中的第一行數據封裝到Map集合,key是列名,value是數據值  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";  		Map<String, Object> map = queryRunner.query(sql, new MapHandler());  		System.out.println(map);  	}

運行程式碼

添加MapListHandler測試程式碼

	@Test  	public void testMapListHandler() throws SQLException {  		// MapHandler 將結果集中的每一行數據封裝到Map集合,key是列名,value是數據值,再將Map對象存入List  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";  		List<Map<String,Object>> list = queryRunner.query(sql, new MapListHandler());    		for(Map<String,Object> map : list){  			System.out.println(map);  		}  	}

運行程式碼

添加KeyedHandler測試程式碼

	@Test  	public void testKeyedHandler() throws SQLException {  		// KeyedHandler	將結果集中的每一行數據都封裝到Map里,再將Map存入一個Map里,key可以指定為任意列  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select * from account";  		Map<Object, Map<String,Object>> map = queryRunner.query(sql, new KeyedHandler("name"));    		System.out.println(map);  	}

運行程式碼

添加ScalarHandler測試程式碼

	@Test  	public void testScalarHandler() throws SQLException{  		//ScalarHandler		通常保存只有一行一列的結果數據  		QueryRunner queryRunner = new QueryRunner(dataSource);  		String sql = "select count(*) from account";  		long count = (Long) queryRunner.query(sql, new ScalarHandler(1));  		System.out.println(count);  	} 

運行程式碼

到這裡,九個Hanlder就介紹完畢了。 最常用的幾個: BeanHandler、BeanListHandler、ColumnListHandler、ScalarHandler。