日期:2014-05-16 浏览次数:20564 次
Spring JDBC是Spring所提供的持久层技术。他的目的是降低JDBC API门槛,以一种更直接、更简洁的方式使用JDBC API。
JdbcTemplate小试牛刀
package com.smart.dao; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.CallableStatementCallback; import org.springframework.jdbc.core.CallableStatementCreator; import org.springframework.jdbc.core.CallableStatementCreatorFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlOutParameter; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Repository; import com.smart.domain.User; @Repository public class ViewSpaceDao { @Autowired private JdbcTemplate jdbcTemplate; public void initDb() { String sql="create table t_user_2(user_id int primary key,user_name varchar(60))"; jdbcTemplate.execute(sql); } public void updateJdbc1() { final String sql="INSERT INTO t_user(user_name,password) VALUES(?,?)"; Object[] params=new Object[]{"MonkeyKing","孫悟空"}; jdbcTemplate.update(sql,params); } public void updateJdbc2() { final String sql="INSERT INTO t_user(user_name,password) VALUES(?,?)"; Object[] params=new Object[]{"MonkeyKing","孫悟空"}; jdbcTemplate.update(sql,params,new int[]{Types.VARCHAR,Types.VARCHAR}); } //獲得主鍵的更新 public void updateJdbc3() { final String sql="INSERT INTO t_user(user_name,password) VALUES(?,?)"; KeyHolder keyHolder=new GeneratedKeyHolder();//創建一個主鍵執有 jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection arg0) throws SQLException { // TODO Auto-generated method stub PreparedStatement ps=arg0.prepareStatement(sql); ps.setString(1, "MonkeyKing"); ps.setString(2, "孫悟空"); return ps; } },keyHolder); //獲得主鍵并打印出主鍵 System.out.println(keyHolder.getKey().intValue()); //jdbcTemplate. } //批量更改數據 public void batchUpdateSql(final List<User> users) { final String sql="INSERT INTO t_user(user_name,password) VALUES(?,?)"; jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement arg0, int arg1) throws SQLException { // TODO Auto-generated method stub User user=users.get(arg1); arg0.setString(1,user.getUserName()); arg0.setString(2, user.getPassword()); } @Override public int getBatchSize() { // TODO Auto-generated method stub return users.size(); } }); } //查詢單條數據集,多條只需修改返回類型和一些定義數據類型 public User getDataSql(final int userId) { String sql="SELECT user_name,password from t_user where user_id=?"; final User user=new User(); jdbcTemplate.query(sql, new Object[]{userId},new RowCallbackHandler(){ @Override public void processRow(ResultSet rs) throws SQLException { // TODO Auto-generated method stub user.setUserId(userId); user.setUserName(rs.getString("user_name")); user.setPassword(rs.