Spring SimpleJdbcTemplate查询示例

以下是一些示例,展示了如何使用SimpleJdbcTemplate query()方法从数据库中查询或提取数据。 在JdbcTemplate query() ,您需要手动将返回的结果强制转换为所需的对象类型,并将Object数组作为参数传递。 在SimpleJdbcTemplate中,它更加用户友好和简单。

jdbctemplate vesus简单
请将此SimpleJdbcTemplate示例与此JdbcTemplate示例进行比较。

1.查询单行

这是两种向您展示如何查询或从数据库中提取一行并将其转换为模型类的方法。

1.1自定义RowMapper

通常,始终建议实现RowMapper接口以创建自定义RowMapper来满足您的需求。

package com.mkyong.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单列名称

它显示了如何查询单个列名作为String。

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.mkyong.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.dao.CustomerDAO;
import com.mkyong.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的友好补充。

下载源代码

下载它– Spring-SimpleJdbcTemplate-Querying-Example.zip (15 KB)

翻译自: https://mkyong.com/spring/spring-simplejdbctemplate-querying-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值