前言
在沒有任何框架的幫助下我們操作數據庫都是用jdbc,耗時耗力,那么有了Spring,我們則不用重復造輪子了,先來試試Spring JDBC增刪改查,其中關鍵就是構造JdbcTemplate類。
其中jdbcTemplate已經實現了queryForList(),但是經過測試后發現之能返回簡單數據類型String、Integer之類。 如果需要返回List<T>則使用query()並且讓model實現RowMappper接口。
開發環境
idea2016、jdk1.8、maven3.3、
spring-jdbc 4.3.6
Spring Jdbc初體驗之增刪改查
1、創建一個maven項目,導入所依賴的jar,spring jdbc主要依賴spring-jdbc。
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-jdbc</artifactId> 5 <version>4.3.6.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>junit</groupId> 9 <artifactId>junit</artifactId> 10 <version>4.10</version> 11 </dependency> 12 <dependency> 13 <groupId>com.microsoft.sqlserver</groupId> 14 <artifactId>sqljdbc4</artifactId> 15 <version>4.0</version> 16 </dependency> 17 </dependencies>
2、創建DriverManagerDataSource、創建jdbcTemplate。主要調用query()、queryForObject()、queryForList()、queryForMap、
package com.autohome.dao;
import com.autohome.model.User;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class UserDaoTest {
private static JdbcTemplate jdbcTemplate;
@BeforeClass
public static void setUpClass(){
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setUrl("jdbc:sqlserver://127.0.0.1:1433;databaseName=test");
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUsername("sa");
dataSource.setPassword("");
jdbcTemplate=new JdbcTemplate(dataSource);
}
@Test
public void selectAll(){
jdbcTemplate.query("select * from t_student", new RowCallbackHandler() {
public void processRow(ResultSet resultSet) throws SQLException {
System.out.println("====id:"+resultSet.getInt("id")+"===name:"+resultSet.getString("name"));
}
});
}
@Test
public void insert(){
//SQL
//int result = jdbcTemplate.update("insert into t_student (name,age) VALUES (?,?)",new Object[]{"Lin",30});
//避免sql參數注入
int result = jdbcTemplate.update("insert into t_student (name,age) values(?,?)",
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1,"zhangsan");
ps.setInt(2,35);
}
});
if(result>0){
System.out.println("insert success...");
}
}
@Test
public void update(){
int result = jdbcTemplate.update("update t_student set Name=? ,Age=? where Id=?", new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1,"Linshuhao");
ps.setInt(2,40);
ps.setInt(3,3);
}
});
if(result>0){
System.out.println("update success...");
}
}
@Test
public void delete(){
int result = jdbcTemplate.update("delete from t_student where id=?",new Object[]{1},new int[]{Types.INTEGER});
if(result>0){
System.out.println("delete success...");
}
}
@Test
public void listAll(){
List<User> allUser = jdbcTemplate.query("SELECT * FROM t_student",new User());
for(User user:allUser){
System.out.println("===id:"+user.getId()+",name:"+user.getName()+",age:"+user.getAge());
}
}
@Test
public void selectUser(){
String sql="SELECT * FROM t_student where id=?";
User user = (User) jdbcTemplate.queryForObject(sql,new Object[]{2},new User());
System.out.println("===id:"+user.getId()+",name:"+user.getName()+",age:"+user.getAge());
}
}
User.java 主要是實現RowMapper接口,要不然jdbc不知道你的實體類和sql字段怎么映射,僅此而已
package com.autohome.model;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class User implements RowMapper {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Object mapRow(ResultSet rs, int i) throws SQLException {
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
return user;
}
}
參考資料
http://www.cnblogs.com/doudouxiaoye/p/5782003.html
http://1358440610-qq-com.iteye.com/blog/1826816
