vue的template用法 template标签是什么意思( 二 )


//自定义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支持的回调类预编译语句及存储过程创建回调:用于根据JdbcTemplate提供的连接创建相应的语句PreparedStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的PreparedStatement;CallableStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的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) // 关联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创建一个预编译语句,其次由JdbcTemplate通过PreparedStatementCallback回调传回,由用户决定如何执行该PreparedStatement 。此处我们使用的是execute方法 。
预编译语句设值回调:用于给预编译语句相应参数设值PreparedStatementSetter:通过回调获取JdbcTemplate提供的PreparedStatement,由用户来对相应的预编译语句相应参数设值;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) // 关联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);}}通过JdbcTemplate的int update(String sql, PreparedStatementSetter pss)执行预编译sql,其中sql参数为“insert into user(user_name) values (?) ”,该sql有一个占位符需要在执行前设值,PreparedStatementSetter实现就是为了设值,使用setValues(PreparedStatement pstmt)回调方法设值相应的占位符位置的值 。JdbcTemplate也提供一种更简单的方式“update(String sql, Object… args)”来实现设值,所以只要当使用该种方式不满足需求时才应使用PreparedStatementSetter 。


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: