Spring SimpleJdbcTemplate查询示例

简介:
这里有几个例子来说明如何使用SimpleJdbcTemplate query()方法来查询或从数据库中提取数据。在 JdbcTemplate query() 方法,需要手动转换返回的结果转换为一个目标对象类型,并传递一个对象数组作为参数。在SimpleJdbcTemplate类,它是更加人性化和简单。
jdbctemplate VS simplejdbctemplate
请比较  JdbcTemplate类的示例SimpleJdbcTemplate类的示例。
1.查询单行
这里有向你展示了如何查询或从数据库中提取单行的两种方式,并将其转换成一个模型类。

1.1 自定义RowMapper

在一般情况下,它总是建议来实现 RowMapper 接口来创建自定义的RowMapper,以满足您的需求。
package com.yiibai.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.yiibai.common;

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

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

 

下载代码 –  http://pan.baidu.com/s/1eRisz6M

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6367570.html,如需转载请自行联系原作者
相关文章
|
8月前
|
缓存 Java Go
解决Spring Data JPA查询存在缓存问题及解决方案
解决Spring Data JPA查询存在缓存问题及解决方案
422 0
|
9月前
|
自然语言处理 数据可视化 Java
Spring Data Elasticsearch 聚合查询
如需要统计某件商品的数量,最高价格,最低价格等就用到了聚合查询,就像数据库中的group by
147 0
|
6天前
|
安全 Java 测试技术
Spring Security应用中的部分代码示例2
【6月更文挑战第12天】Spring Security应用中的部分代码示例2
14 5
|
6天前
|
安全 Java 数据库
Spring Security应用代码示例
【6月更文挑战第12天】Spring Security应用代码示例
15 3
|
8月前
|
缓存 监控 安全
Spring AOP 详细深入讲解+代码示例
Spring AOP(Aspect-Oriented Programming)是Spring框架提供的一种面向切面编程的技术。它通过将横切关注点(例如日志记录、事务管理、安全性检查等)从主业务逻辑代码中分离出来,以模块化的方式实现对这些关注点的管理和重用。 在Spring AOP中,切面(Aspect)是一个模块化的关注点,它可以跨越多个对象,例如日志记录、事务管理等。切面通过定义切点(Pointcut)和增强(Advice)来介入目标对象的方法执行过程。 切点是一个表达式,用于匹配目标对象的一组方法,在这些方法执行时切面会被触发。增强则定义了切面在目标对象方法执行前、执行后或抛出异常时所
3610 0
|
1月前
|
前端开发 Java Spring
Java 新手如何使用Spring MVC 中的查询字符串和查询参数
Java 新手如何使用Spring MVC 中的查询字符串和查询参数
|
1月前
|
存储 Java 关系型数据库
Spring Batch学习记录及示例项目代码
Spring Batch学习记录及示例项目代码
|
10月前
|
XML 缓存 Java
2021-08-07mybaties结尾:查询缓存,dao开发分页+项目改造+spring框架
2021-08-07mybaties结尾:查询缓存,dao开发分页+项目改造+spring框架
198 0
|
1月前
Spring-AOP通知类型示例
Spring-AOP通知类型示例
40 0
|
1月前
|
Java Spring 容器
Spring-依赖注入(DI)的两种方式示例
Spring-依赖注入(DI)的两种方式示例
36 0