这里有几个例子来说明如何使用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

Spring SimpleJdbcTemplate查询示例相关推荐

  1. Spring整合Mybatis之关联查询示例

    Spring整合Mybatis之关联查询示例 目录结构: Dept表: employee表: pojo包 Dept.java(实体类): package com.xmm.springboot_lab. ...

  2. Spring Boot AJAX 示例

    本文以spring boot框架.thymeleaf引擎为基础,利用jquery.ajax提交HTML表单请求到后台(spring rest api),后台返回一个JSON格式的数据为例进行说明. 开 ...

  3. Spring Boot的Spring Data JPA示例

    1.简介 在本文中,我们将演示如何利用功能强大的Spring Data JPA API与本课程中的数据库(内存中的H2数据库)进行交互. Spring Data JPA提供了一组非常强大且高度抽象的接 ...

  4. Spring事务管理示例JDBC

    Spring事务管理示例JDBC Spring Transaction Management是Spring框架中使用最广泛且最重要的特性之一.事务管理在任何企业应用程序中都是一项微不足道的任务.我们已 ...

  5. Spring Data MongoDB示例

    Spring Data MongoDB示例 欢迎使用Spring Data MongoDB示例.Spring Data MongoDB是将Spring Framework与最广泛使用的NoSQL数据库 ...

  6. Spring Boot中使用Spring Data JPA示例

    JPA是Java Persistence API的简称,是sun公司早期推出的Java持久层规范,目前实现JPA规范的主流框架有Hibernate.OpenJPA等.Hibernate框架是当前较为流 ...

  7. sql子查询示例_SQL更新查询示例说明

    sql子查询示例 In this article, we're going to learn how to use the SQL update statement - what it is, wha ...

  8. 5,6,7_InfluxDB数据保留策略,InfluxDB的关键概念,带有时区进行查询示例

    5.InfluxDB学习之InfluxDB数据保留策略(Retention Policies) 5.1.InfluxDB数据保留策略说明 5.2.InfluxDB数据保留策略目的 5.3.Influx ...

  9. 08_clickhouse主键/索引的工作机制(MergeTree的稀疏索引、索引的生成过程、索引的查询),数据标记的工作机制(数据存储、数据标记、数据查询、数据查询示例)(学习笔记)

    5.主键/索引的工作机制 5.1.MergeTree的稀疏索引 5.2.索引的生成过程 5.3.索引的查询 6.数据标记的工作机制 6.1.数据存储 6.2.数据标记 6.3.数据查询 6.4.数据查 ...

最新文章

  1. 生态伙伴 | Worktile入驻飞书,助力企业轻松实现敏捷开发与协作
  2. 服务器mac地址查询修改,服务器mac地址查询修改
  3. java报表工具FineReport使用中遇到的常见报错及解决办法(三)
  4. Element el-upload上传组件详解
  5. JSP和Servlet里的Cookie处理
  6. 视觉感知_产品设计中的视觉感知
  7. 学校计算机数据采集处理系统,中学化学计算机数据采集处理系统实验室装备
  8. linux io阻塞问题
  9. Clojure学习02:语法
  10. 2016-03-12 Leanning Plan
  11. 2019 全国大学生电子设计竞赛题目
  12. 二阶矩阵转置怎么求_矩阵的转置怎么求 详情介绍
  13. 写JAVA的,码农,程序员,工程师有啥不同
  14. total variation、global variation、local variation
  15. source insight中文乱码
  16. 与MySQL的纠缠(卸载与安装)
  17. mysql源码包多大_MySQL源码包安装
  18. 爱奇艺校招笔试题 数字游戏
  19. 有道翻译爬虫+JS逆向
  20. 移动App多渠道推广统计

热门文章

  1. C语言gauss elimination高斯消元法算法(附完整源码)
  2. C语言实现镜子mirror算法(附完整源码)
  3. python内置函数表_python学习系列--python内置函数(一)
  4. influxdb介绍,安装,使用等(转载:http://www.jianshu.com/p/d2935e99006e)
  5. XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
  6. overridePendingTransition的简介
  7. 数据表从一个表空间中移动到另一个表空间中
  8. composer搭建php框架,用 Composer构建自己的 PHP 框架之基础准备
  9. pg高性能服务器,如何充分利用单台服务器的性能将10亿级的json数据尽可能高效的插入postgresql?...
  10. java中将date插入mysql中date_JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法[转]...