Spring 专题
专题目录
您的位置:java > Spring专题 > Spring SimpleJdbcTemplate查询示例
Spring SimpleJdbcTemplate查询示例
作者:--    发布时间:2019-11-20
这里有几个例子来说明如何使用simplejdbctemplate query()方法来查询或从数据库中提取数据。在 jdbctemplate query() 方法,需要手动转换返回的结果转换为一个目标对象类型,并传递一个对象数组作为参数。在simplejdbctemplate类,它是更加人性化和简单。
jdbctemplate vs simplejdbctemplate
请比较 jdbctemplate类的示例simplejdbctemplate类的示例。

1.查询单行

这里有向你展示了如何查询或从数据库中提取单行的两种方式,并将其转换成一个模型类。

1.1 自定义rowmapper

在一般情况下,它总是建议来实现 rowmapper 接口来创建自定义的rowmapper,以满足您的需求。
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 = getsimplejdbctemplate().queryforobject(
			sql,  new customerparameterizedrowmapper(), custid);
	
	return customer;
}

1.2 beanpropertyrowmapper

在simplejdbctemplate类,需要使用“parameterizedbeanpropertyrowmapper' 代替 'beanpropertyrowmapper”。
public customer findbycustomerid2(int custid){
		 
	string sql = "select * from customer where cust_id = ?";
 
	customer customer = getsimplejdbctemplate().queryforobject(sql,
          parameterizedbeanpropertyrowmapper.newinstance(customer.class), custid);
	
	return customer;
}

2,查询多行

从数据库查询或提取多行记录,并将其转换成一个列表。

2.1 parameterizedbeanpropertyrowmapper

public list<customer> findall(){
		
	string sql = "select * from customer";
		
	list<customer> customers = 
		getsimplejdbctemplate().query(sql, 
		   parameterizedbeanpropertyrowmapper.newinstance(customer.class));
		
	return customers;
}

3.查询单值

查询或提取数据库中的单个列的值。

3.1单列名

它显示了如何查询单个列名作为字符串。
public string findcustomernamebyid(int custid){
		
	string sql = "select name from customer where cust_id = ?";
		 
	string name = getsimplejdbctemplate().queryforobject(
		sql, string.class, custid);
	
	return name;
		
}

3.2、行总数

它展示了如何从数据库中查询行的总数。
public int findtotalcustomer(){
		
	string sql = "select count(*) from customer";
		 
	int total = getsimplejdbctemplate().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 simplejdbctemplateapp 
{
    public static void main( string[] args )
    {
    	 applicationcontext context = 
    		new classpathxmlapplicationcontext("spring-customer.xml");
    	 
         customerdao customersimpledao = 
                (customerdao) context.getbean("customersimpledao");
		 
         customer customera = customersimpledao.findbycustomerid(1);
         system.out.println("customer a : " + customera);
         
         customer customerb = customersimpledao.findbycustomerid2(1);
         system.out.println("customer b : " + customerb);
         
         list<customer> customeras = customersimpledao.findall();
         for(customer cust: customeras){
         	 system.out.println("customer as : " + customeras);
         }
        
         list<customer> customerbs = customersimpledao.findall2();
         for(customer cust: customerbs){
         	 system.out.println("customer bs : " + customerbs);
         }
         
         string customername = customersimpledao.findcustomernamebyid(1);
         system.out.println("customer name : " + customername);
         
         int total = customersimpledao.findtotalcustomer();
         system.out.println("total : " + total);
         
    }
}

总结

simplejdbctemplate 是不能代替 jdbctemplate 的,它只是一个java5的友好补充它。



网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册