Spring SimpleJdbcTemplate查询示例
请比较JdbcTemplate类的示例和SimpleJdbcTemplate类的示例。
1.1 自定义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
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.1 ParameterizedBeanPropertyRowMapper
public List<Customer> findAll(){String sql = "SELECT * FROM CUSTOMER";List<Customer> customers = getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Customer.class));return customers;
}
public String findCustomerNameById(int custId){String sql = "SELECT NAME FROM CUSTOMER WHERE CUST_ID = ?";String name = getSimpleJdbcTemplate().queryForObject(sql, String.class, custId);return name;}
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);}
}
总结
Spring SimpleJdbcTemplate查询示例相关推荐
- Spring整合Mybatis之关联查询示例
Spring整合Mybatis之关联查询示例 目录结构: Dept表: employee表: pojo包 Dept.java(实体类): package com.xmm.springboot_lab. ...
- Spring Boot AJAX 示例
本文以spring boot框架.thymeleaf引擎为基础,利用jquery.ajax提交HTML表单请求到后台(spring rest api),后台返回一个JSON格式的数据为例进行说明. 开 ...
- Spring Boot的Spring Data JPA示例
1.简介 在本文中,我们将演示如何利用功能强大的Spring Data JPA API与本课程中的数据库(内存中的H2数据库)进行交互. Spring Data JPA提供了一组非常强大且高度抽象的接 ...
- Spring事务管理示例JDBC
Spring事务管理示例JDBC Spring Transaction Management是Spring框架中使用最广泛且最重要的特性之一.事务管理在任何企业应用程序中都是一项微不足道的任务.我们已 ...
- Spring Data MongoDB示例
Spring Data MongoDB示例 欢迎使用Spring Data MongoDB示例.Spring Data MongoDB是将Spring Framework与最广泛使用的NoSQL数据库 ...
- Spring Boot中使用Spring Data JPA示例
JPA是Java Persistence API的简称,是sun公司早期推出的Java持久层规范,目前实现JPA规范的主流框架有Hibernate.OpenJPA等.Hibernate框架是当前较为流 ...
- sql子查询示例_SQL更新查询示例说明
sql子查询示例 In this article, we're going to learn how to use the SQL update statement - what it is, wha ...
- 5,6,7_InfluxDB数据保留策略,InfluxDB的关键概念,带有时区进行查询示例
5.InfluxDB学习之InfluxDB数据保留策略(Retention Policies) 5.1.InfluxDB数据保留策略说明 5.2.InfluxDB数据保留策略目的 5.3.Influx ...
- 08_clickhouse主键/索引的工作机制(MergeTree的稀疏索引、索引的生成过程、索引的查询),数据标记的工作机制(数据存储、数据标记、数据查询、数据查询示例)(学习笔记)
5.主键/索引的工作机制 5.1.MergeTree的稀疏索引 5.2.索引的生成过程 5.3.索引的查询 6.数据标记的工作机制 6.1.数据存储 6.2.数据标记 6.3.数据查询 6.4.数据查 ...
最新文章
- 生态伙伴 | Worktile入驻飞书,助力企业轻松实现敏捷开发与协作
- 服务器mac地址查询修改,服务器mac地址查询修改
- java报表工具FineReport使用中遇到的常见报错及解决办法(三)
- Element el-upload上传组件详解
- JSP和Servlet里的Cookie处理
- 视觉感知_产品设计中的视觉感知
- 学校计算机数据采集处理系统,中学化学计算机数据采集处理系统实验室装备
- linux io阻塞问题
- Clojure学习02:语法
- 2016-03-12 Leanning Plan
- 2019 全国大学生电子设计竞赛题目
- 二阶矩阵转置怎么求_矩阵的转置怎么求 详情介绍
- 写JAVA的,码农,程序员,工程师有啥不同
- total variation、global variation、local variation
- source insight中文乱码
- 与MySQL的纠缠(卸载与安装)
- mysql源码包多大_MySQL源码包安装
- 爱奇艺校招笔试题 数字游戏
- 有道翻译爬虫+JS逆向
- 移动App多渠道推广统计
热门文章
- C语言gauss elimination高斯消元法算法(附完整源码)
- C语言实现镜子mirror算法(附完整源码)
- python内置函数表_python学习系列--python内置函数(一)
- influxdb介绍,安装,使用等(转载:http://www.jianshu.com/p/d2935e99006e)
- XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
- overridePendingTransition的简介
- 数据表从一个表空间中移动到另一个表空间中
- composer搭建php框架,用 Composer构建自己的 PHP 框架之基础准备
- pg高性能服务器,如何充分利用单台服务器的性能将10亿级的json数据尽可能高效的插入postgresql?...
- java中将date插入mysql中date_JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法[转]...