首先建立数据表:
CREATE TABLE `login` (
`username` varchar(10) default NULL,
`passwd` varchar(10) default NULL,
`address` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
配置文件:
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/javaee </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > 1234 </ value >
</ property >
</ bean >
< bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
</ bean >
< bean id ="personDAO" class ="SpringJDBCSupport.ReadData.PersonDAO" >
< property name ="jdbcTemplate" >
< ref local ="jdbcTemplate" />
</ property >
</ bean >
</ beans >
JavaBean
import com.mysql.jdbc.Driver;
public class Person {
private String name;
private String password;
private String address;
public Person() {
}
public Person(String name,String password,String address) {
this .name = name;
this .password = password;
this .address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this .address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
public String toString() {
return this .getName() + " - " + this .getPassword() + " - " + this .getAddress();
}
}
编写自定义RowMapper
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class PersonRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person();
person.setName(rs.getString( " username " ));
person.setPassword(rs.getString( " passwd " ));
person.setAddress(rs.getString( " address " ));
return person;
}
}
测试代码:
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestJDBCTemplate {
public static String filePath = "" ;
public static BeanFactory factory = null ;
public static void main(String[] args) {
filePath = System.getProperty( " user.dir " ) + File.separator + " SpringJDBCSupport " + File.separator + " ReadData " + File.separator + " hello.xml " ;
factory = new XmlBeanFactory( new FileSystemResource(filePath));
PersonDAO personDAO = (PersonDAO)factory.getBean( " personDAO " );
/*
* 准备数据
*/
Person p1 = new Person( " test1 " , " test1 " , " test1 " );
Person p2 = new Person( " test2 " , " test2 " , " test2 " );
Person p3 = new Person( " test3 " , " test3 " , " test3 " );
Person p4 = new Person( " test4 " , " test4 " , " test4 " );
Person p5 = new Person( " test5 " , " test5 " , " test5 " );
List persons = new ArrayList();
persons.add(p3);
persons.add(p4);
persons.add(p5);
// 使用jdbcTemplate.update方式
personDAO.insertPersonUseUpdate(p1);
// 使用jdbcTemplate.execute方式
personDAO.insertPersonUseExecute(p2);
// // 使用jdbcTemplate批处理方式
personDAO.updatePersonUseBatchUpdate(persons);
// 使用RowCallbackHandler执行一次查询,并打印person信息
System.out.println(personDAO.getPersonByRowCallbackHandler( " test1 " ));
List a = personDAO.getPersonsByMapperResultReader();
for (Iterator iter = a.iterator(); iter.hasNext();) {
System.out.println((Person)iter.next());
}
}
}
运行程序:输出为以下结果,红色部分为查询结果集
test1-test1-test1
test1-test1-test1
test2-test2-test2
test3-test3-test3
test4-test4-test4
test5-test5-test5
我们完全可以用这个方法代替RowCallbackHandler,用一个自定义的RowMapper结局单个查询和结果集查询两种方式,返回单个对象是,只要修改
List result=(ArrayList)this.getJdbcTemplate().query(sql,params,new RowMapperResultSetExtractor(new PersonRowMapper()));
return result.get(0);
}
即可
Spring使用JdbcTemplate操作数据库---使用RowMapperResultSetExtractor读数据篇