亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

vue的template用法 template標(biāo)簽是什么意思( 二 )


//自定義public class StudentRowMapper implements RowMapper<Student> {@Overridepublic Student mapRow(ResultSet rs, int i) throws SQLException {Student student = new Student();student.setName(rs.getString("name"));student.setGender(rs.getString("gender"));student.setEmail(rs.getString("email"));return student;}}//使用String sql = "select name, gender, email from test_student where name = ?";jdbcTemplate.queryForObject(sql, new Object[]{name}, new StudentRowMapper());批量修改public int[] batchInsert(List<Map<String, Object>> dataList) {String sql = "INSERT INTO test_info(name, create_time, age) VALUES(?, NOW(), ?)";List<Object[]> paramArray = new ArrayList<>();for (Map<String, Object> dataInfo : dataList) {Object[] obj = new Object[2];obj[0] = dataInfo.get("name");obj[1] = dataInfo.get("age");paramArray.add(obj);}if (CollectionUtils.isNotEmpty(paramArray)) {return jdbcTemplate.batchUpdate(sql, paramArray);}return new int[0];}JdbcTemplate支持的回調(diào)類預(yù)編譯語(yǔ)句及存儲(chǔ)過(guò)程創(chuàng)建回調(diào):用于根據(jù)JdbcTemplate提供的連接創(chuàng)建相應(yīng)的語(yǔ)句PreparedStatementCreator:通過(guò)回調(diào)獲取JdbcTemplate提供的Connection,由用戶使用該Conncetion創(chuàng)建相關(guān)的PreparedStatement;CallableStatementCreator:通過(guò)回調(diào)獲取JdbcTemplate提供的Connection,由用戶使用該Conncetion創(chuàng)建相關(guān)的CallableStatement;
import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementCallback;import org.springframework.jdbc.core.PreparedStatementCreator;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) // 關(guān)聯(lián)Spring與Junit@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) // 加載配置spring配置文件public class AppTest {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testPpreparedStatement1() {int count = jdbcTemplate.execute(new PreparedStatementCreator() {public java.sql.PreparedStatement createPreparedStatement(Connection conn) throws SQLException {return conn.prepareStatement("select count(*) from user");}}, new PreparedStatementCallback<Integer>() {public Integer doInPreparedStatement(java.sql.PreparedStatement pstmt)throws SQLException, DataAccessException {pstmt.execute();ResultSet rs = pstmt.getResultSet();rs.next();return rs.getInt(1);}});System.out.println(count);}}首先使用PreparedStatementCreator創(chuàng)建一個(gè)預(yù)編譯語(yǔ)句,其次由JdbcTemplate通過(guò)PreparedStatementCallback回調(diào)傳回,由用戶決定如何執(zhí)行該P(yáng)reparedStatement 。此處我們使用的是execute方法 。
預(yù)編譯語(yǔ)句設(shè)值回調(diào):用于給預(yù)編譯語(yǔ)句相應(yīng)參數(shù)設(shè)值PreparedStatementSetter:通過(guò)回調(diào)獲取JdbcTemplate提供的PreparedStatement,由用戶來(lái)對(duì)相應(yīng)的預(yù)編譯語(yǔ)句相應(yīng)參數(shù)設(shè)值;BatchPreparedStatementSetter:;類似于PreparedStatementSetter,但用于批處理,需要指定批處理大??;
import java.sql.PreparedStatement;import java.sql.SQLException;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementSetter;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) // 關(guān)聯(lián)Spring與Junit@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) // 加載配置spring配置文件public class AppTest {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testPreparedStatement2() {String insertSql = "insert into user(user_name) values (?)";int count = jdbcTemplate.update(insertSql, new PreparedStatementSetter() {public void setValues(PreparedStatement pstmt) throws SQLException {pstmt.setObject(1, "mmNN");}});Assert.assertEquals(1, count);String deleteSql = "delete from user where user_name=?";count = jdbcTemplate.update(deleteSql, new Object[] { "mmNN" });Assert.assertEquals(1, count);}}通過(guò)JdbcTemplate的int update(String sql, PreparedStatementSetter pss)執(zhí)行預(yù)編譯sql,其中sql參數(shù)為“insert into user(user_name) values (?) ”,該sql有一個(gè)占位符需要在執(zhí)行前設(shè)值,PreparedStatementSetter實(shí)現(xiàn)就是為了設(shè)值,使用setValues(PreparedStatement pstmt)回調(diào)方法設(shè)值相應(yīng)的占位符位置的值 。JdbcTemplate也提供一種更簡(jiǎn)單的方式“update(String sql, Object… args)”來(lái)實(shí)現(xiàn)設(shè)值,所以只要當(dāng)使用該種方式不滿足需求時(shí)才應(yīng)使用PreparedStatementSetter 。


以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問(wèn)題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!

「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助: