自定義功能回調(diào):提供給用戶一個擴展點,用戶可以在指定類型的擴展點執(zhí)行任何數(shù)量需要的操作ConnectionCallback:通過回調(diào)獲取JdbcTemplate提供的Connection,用戶可在該Connection執(zhí)行任何數(shù)量的操作;StatementCallback:通過回調(diào)獲取JdbcTemplate提供的Statement,用戶可以在該Statement執(zhí)行任何數(shù)量的操作;PreparedStatementCallback:通過回調(diào)獲取JdbcTemplate提供的PreparedStatement,用戶可以在該PreparedStatement執(zhí)行任何數(shù)量的操作;CallableStatementCallback:通過回調(diào)獲取JdbcTemplate提供的CallableStatement,用戶可以在該CallableStatement執(zhí)行任何數(shù)量的操作;
結(jié)果集處理回調(diào):通過回調(diào)處理ResultSet或?qū)esultSet轉(zhuǎn)換為需要的形式RowMapper:用于將結(jié)果集每行數(shù)據(jù)轉(zhuǎn)換為需要的類型,用戶需實現(xiàn)方法mapRow(ResultSet rs, int rowNum)來完成將每行數(shù)據(jù)轉(zhuǎn)換為相應(yīng)的類型 。RowCallbackHandler:用于處理ResultSet的每一行結(jié)果,用戶需實現(xiàn)方法processRow(ResultSet rs)來完成處理,在該回調(diào)方法中無需執(zhí)行rs.next(),該操作由JdbcTemplate來執(zhí)行,用戶只需按行獲取數(shù)據(jù)然后處理即可 。ResultSetExtractor:用于結(jié)果集數(shù)據(jù)提取,用戶需實現(xiàn)方法extractData(ResultSet rs)來處理結(jié)果集,用戶必須處理整個結(jié)果集;
@Testpublic void testResultSet1() {jdbcTemplate.update("insert into user(user_name) values('name7')");String listSql = "select * from user where user_name=?";List result = jdbcTemplate.query(listSql,new Object[]{"name7"}, new RowMapper<Map>() {public Map mapRow(ResultSet rs, int rowNum) throws SQLException {Map row = new HashMap();row.put(rs.getInt("user_id"), rs.getString("user_name"));return row;}});Assert.assertEquals(1, result.size());//查詢結(jié)果數(shù)量為1才向下執(zhí)行jdbcTemplate.update("delete from user where user_name='name7'");}RowMapper接口提供mapRow(ResultSet rs, int rowNum)方法將結(jié)果集的每一行轉(zhuǎn)換為一個Map,當然可以轉(zhuǎn)換為其他類 。
@Testpublic void testResultSet2() {jdbcTemplate.update("insert into user(user_name) values('name5')");String listSql = "select * from user";final List result = new ArrayList();jdbcTemplate.query(listSql, new RowCallbackHandler() {public void processRow(ResultSet rs) throws SQLException {Map row = new HashMap();row.put(rs.getInt("user_id"), rs.getString("user_name"));result.add(row);}});Assert.assertEquals(1, result.size());jdbcTemplate.update("delete from user where user_name='name5'");}RowCallbackHandler接口也提供方法processRow(ResultSet rs),能將結(jié)果集的行轉(zhuǎn)換為需要的形式 。
@Testpublic void testResultSet3() {jdbcTemplate.update("insert into test(name) values('name5')");String listSql = "select * from test";List result = jdbcTemplate.query(listSql, new ResultSetExtractor<List>() {public List extractData(ResultSet rs) throws SQLException, DataAccessException {List result = new ArrayList();while (rs.next()) {Map row = new HashMap();row.put(rs.getInt("id"), rs.getString("name"));result.add(row);}return result;}});Assert.assertEquals(0, result.size());jdbcTemplate.update("delete from test where name='name5'");}ResultSetExtractor使用回調(diào)方法extractData(ResultSet rs)提供給用戶整個結(jié)果集,讓用戶決定如何處理該結(jié)果集 。
當然JdbcTemplate提供更簡單的queryForXXX方法,來簡化開發(fā):
//1.查詢一行數(shù)據(jù)并返回int型結(jié)果jdbcTemplate.queryForInt("select count(*) from test");//2. 查詢一行數(shù)據(jù)并將該行數(shù)據(jù)轉(zhuǎn)換為Map返回jdbcTemplate.queryForMap("select * from test where name='name5'");//3.查詢一行任何類型的數(shù)據(jù),最后一個參數(shù)指定返回結(jié)果類型jdbcTemplate.queryForObject("select count(*) from test", Integer.class);//4.查詢一批數(shù)據(jù),默認將每行數(shù)據(jù)轉(zhuǎn)換為MapjdbcTemplate.queryForList("select * from test");//5.只查詢一列數(shù)據(jù)列表,列類型是String類型,列名字是namejdbcTemplate.queryForList("select name from test where name=?", new Object[]{"name5"}, String.class);//6.查詢一批數(shù)據(jù),返回為SqlRowSet,類似于ResultSet,但不再綁定到連接上SqlRowSet rs = jdbcTemplate.queryForRowSet("select * from test");返回新增的自增ID@Testpublic void test(){String sql = "insert into sp_user(name) values (?)";KeyHolder keyHolder = new GeneratedKeyHolder();jdbcTemplate.update(new PreparedStatementCreator() {@Overridepublic PreparedStatement createPreparedStatement(Connection connection) throws SQLException {// 設(shè)置返回的主鍵字段名PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);ps.setString(1, "harvey");return ps;}}, keyHolder);// 獲取到插入數(shù)據(jù)生成的IDint num = keyHolder.getKey().intValue();System.out.println(num);}二、NamedParameterJdbcTemplate什么是具名參數(shù)在經(jīng)典的 JDBC 用法中, SQL 參數(shù)是用占位符 ? 表示,并且受到位置的限制 。定位參數(shù)的問題在于, 一旦參數(shù)的順序發(fā)生變化, 就必須改變參數(shù)綁定 。在 Spring JDBC 框架中, 綁定 SQL 參數(shù)的另一種選擇是使用具名參數(shù)(named parameter) 。
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- 一升是多少斤水
- 解析復(fù)選框的checked屬性 js如何判斷checkbox是否選中
- xo怎么喝才好喝
- 2021年省錢的返利app排名 返利商品怎么入賬
- 好用的照相軟件排名 什么相機軟件好用有實惠
- 一個完整的網(wǎng)上商城的源碼 php商城系統(tǒng)技術(shù)難點
- 神話中鳥的名字除了精衛(wèi)還有哪些
- 買來的螃蟹怎么保存
- 這3種人,一定會得到佛菩薩的加持 身上有菩薩跟著的人
- 《長津湖之水門橋》每個人的結(jié)局是什么
