这里有几个例子来说明如何使用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的友好补充它。

Spring SimpleJdbcTemplate查询示例的更多相关文章

  1. spring boot - 整合jpa多对对关系保存和查询示例

    pojo: package com.example.zs.springDataJpa; import org.hibernate.annotations.Proxy; import javax.per ...

  2. spring jdbc 查询结果返回对象、对象列表

    首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...

  3. MyBatis 查询示例

    环境搭建 数据库schema 1)datasource.xml配置 <?xml version="1.0" encoding="UTF-8"?> & ...

  4. Spring松耦合示例(转)& IOC

    Spring松耦合示例 轻松学习Spring<一> IoC容器和Dependency Injection模式 最近公司需要,项目中要用到Spring和Ibatis.趁着过年好好学习学习.I ...

  5. Spring JPA 查询创建

    Spring JPA 查询创建 这是JPA内容的核心部分,可以收藏用作参阅文档. 1. 查询转化和关键字 例:一个JPA查询的转化 public interface UserRepository ex ...

  6. Elasticsearch .Net Client NEST 多条件查询示例

    Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...

  7. 【转】CXF+Spring+Eclipse简明示例

    多系统(异构系统)进行交互时,一种良好的方式便是调用Web Service,本示例基于Apache组织的CXF,为了方便起见特将服务端和客户端写在同一个工程下,实际项目中是不可能的,但是客户端却依赖于 ...

  8. NHibernate查询示例合集

    基本查询   复杂查询示例 /// <summary> /// 获取自定义表单数据中属于部门的部分 /// </summary> /// <param name=&quo ...

  9. Spring JdbcTemplate 查询结果集Map反向生成Java实体(转)

    原文地址:Spring JdbcTemplate 查询结果集Map反向生成Java实体 以前写过一篇文章吐槽过Spring JdbcTemplate的queryForList方法(参见:http:// ...

随机推荐

  1. 安装 Xamarin for Visual Studio

    总得来说,Xamarin 有“联网自动安装”和“手动安装”两种方式. 说明:本文涉及得资源链接都是官网的,同时,在 我的网盘 也有相关备份. 现在,我就以 Windows 为例来大概说明……(-=-我 ...

  2. python使用virtualenv

    virtualenv是python的虚拟环境,可以同时存在多个不同的虚拟环境. #1.安装virtualenv pip install virtualenv #2.创建目录 mkdir myproje ...

  3. centos6.5 使用 rpm 安装 mysql

    从mysql网站下载mysql rpm安装包(包括server.client) 1.安装server rpm -ivh MySQL-server-5.6.19-1.el6.x86_64.rpm 强制安 ...

  4. 网络管理 SNMP基础知识

    SNMP Agent快速开发   网友:SmileWolf 发布于: 2007.08.02 16:06 (共有条评论) 查看评论 | 我要评论                   摘自  http:/ ...

  5. python抓取链家房源信息

    闲着没事就抓取了下链家网的房源信息,抓取的是北京二手房的信息情况,然后通过网址进行分析,有100页,并且每页的url都是类似的 url = 'https://bj.lianjia.com/ershou ...

  6. 常用开放api【长期更新】

    获取时间: 苏宁:http://quan.suning.com/getSysTime.do 淘宝:http://api.m.taobao.com/rest/api3.do?api=mtop.commo ...

  7. CSS3 之媒体查询Media Query

    Media Queries是CSS3有关媒体查询的属性,有了CSS3 之媒体查询Media Queries就可以进行媒体查询,针对每个不同的媒体进行不同的样式编写.传说中的Web响应式布局就可以毫无压 ...

  8. 烈焰遮天 cocos 手游mmo 源码 解析

    引擎: cocos2.x 代码: c++ 混合 lua 游戏类型: mmo 工程结构: game : 游戏启动地方 gamelogic:接sdk相关,登陆支付统计等 libFramework:主要本游 ...

  9. vuex 操作姿势

    Vuex 应用的核心就是 store,它包含着你的应用中大部分的状态 (state) 你不能直接改变 store 中的状态.改变 store 中的状态的唯一途径就是显式地提交 (commit) mut ...

  10. 导出php5.4支持的数组格式,即以[]为标识符而不是以array()标识

    //导出php数组,以[]为标识符而不是以array() if (!function_exists('varExport')) { //导出php数组,以[]为标识符而不是以array() funct ...