这里有几个例子来说明如何使用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. 使用非root用户启动tomcat

    以下操作均为以root用户运行1.添加tomcat用户组 /usr/sbin/groupadd tomcat 2.添加tomcat用户,并限制登录 /usr/sbin/useradd -s /bin/ ...

  2. 查找网页元素对应的js代码

    按F12打开调试窗口,切换到Sources选项卡,最右边的Event Listener Breakpoints里勾选Mouse下的mouseover即可,当鼠标移动到图片上时触发mouseover事件 ...

  3. 9. Swarm mode

  4. 如何去除decimal后面的零?

    如何去除decimal后面的零? 1.260000m.ToString("G29") 不显示科学记数法? decimal.Parse("0.0000001",S ...

  5. 再读《Parallel Programming with Python》并作笔记

    并发编程,在哪个语言里都属于高端应用,一定得会了才好意思说懂了这门语言. 在工作中用得并不是很多,忘了一些内容,就慢慢看,慢慢补上. 今天一天看了近三分之一(我看外文越来越快了??:)), 实践一下多 ...

  6. fastdfs5.x Java客户端简单例子

    下载源码, 使用maven编译并安装 https://github.com/happyfish100/fastdfs-client-java.git 新建maven工程,引入fastdfs-clien ...

  7. “裕同集团&易普优APS项目启动大会”顺利召开

    “裕同集团&易普优APS项目启动大会”顺利召开 ——易普优APS助力裕同集团实现精益生产 2017年7月05日,“裕同集团&易普优APS项目启动大会”顺利召开,裕同集团高级王副总裁.I ...

  8. volatile 的一个经典例子

    volatile 关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被 volatile 修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程 ...

  9. Python函数系列-Random

    import random i = int(input('输入第一个数:')) j = int(input('输入第二个数:')) l = int(input('输入需要产生的个数:')) k=0 w ...

  10. poj 1659 Frogs' Neighborhood( 青蛙的邻居)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9639   Accepted: 40 ...