package com.h3.customer.model;
import java.sql.resultset;
import java.sql.sqlexception;
import org.springframework.jdbc.core.rowmapper;
public class customerrowmapper implements rowmapper
{
public object maprow(resultset rs, int rownum) throws sqlexception {
customer customer = new customer();
customer.setcustid(rs.getint("cust_id"));
customer.setname(rs.getstring("name"));
customer.setage(rs.getint("age"));
return customer;
}
}
public customer findbycustomerid(int custid){
string sql = "select * from customer where cust_id = ?";
customer customer = (customer)getjdbctemplate().queryforobject(
sql, new object[] { custid }, new customerrowmapper());
return customer;
}
在spring2.5中,带有一个方便 rowmapper 实现所谓“beanpropertyrowmapper”,它可以通过匹配行的名字的列值映射到一个属性。只要确保这两个属性和列具有相同的名称,如属性“custid'将匹配到列名为:”custid'或下划线“cust_id”。
public customer findbycustomerid2(int custid){
string sql = "select * from customer where cust_id = ?";
customer customer = (customer)getjdbctemplate().queryforobject(
sql, new object[] { custid },
new beanpropertyrowmapper(customer.class));
return customer;
}
public list<customer> findall(){
string sql = "select * from customer";
list<customer> customers = new arraylist<customer>();
list<map> rows = getjdbctemplate().queryforlist(sql);
for (map row : rows) {
customer customer = new customer();
customer.setcustid((long)(row.get("cust_id")));
customer.setname((string)row.get("name"));
customer.setage((integer)row.get("age"));
customers.add(customer);
}
return customers;
}
public list<customer> findall(){
string sql = "select * from customer";
list<customer> customers = getjdbctemplate().query(sql,
new beanpropertyrowmapper(customer.class));
return customers;
}
public string findcustomernamebyid(int custid){
string sql = "select name from customer where cust_id = ?";
string name = (string)getjdbctemplate().queryforobject(
sql, new object[] { custid }, string.class);
return name;
}
public int findtotalcustomer(){
string sql = "select count(*) from customer";
int total = getjdbctemplate().queryforint(sql);
return total;
}
运行它
package com.h3.common;
import java.util.arraylist;
import java.util.list;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.h3.customer.dao.customerdao;
import com.h3.customer.model.customer;
public class jdbctemplateapp
{
public static void main( string[] args )
{
applicationcontext context =
new classpathxmlapplicationcontext("spring-customer.xml");
customerdao customerdao = (customerdao) context.getbean("customerdao");
customer customera = customerdao.findbycustomerid(1);
system.out.println("customer a : " + customera);
customer customerb = customerdao.findbycustomerid2(1);
system.out.println("customer b : " + customerb);
list<customer> customeras = customerdao.findall();
for(customer cust: customeras){
system.out.println("customer as : " + customeras);
}
list<customer> customerbs = customerdao.findall2();
for(customer cust: customerbs){
system.out.println("customer bs : " + customerbs);
}
string customername = customerdao.findcustomernamebyid(1);
system.out.println("customer name : " + customername);
int total = customerdao.findtotalcustomer();
system.out.println("total : " + total);
}
}