博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring SimpleJdbcTemplate查询示例
阅读量:4317 次
发布时间:2019-06-06

本文共 3787 字,大约阅读时间需要 12 分钟。

这里有几个例子来说明如何使用SimpleJdbcTemplate query()方法来查询或从数据库中提取数据。在 JdbcTemplate query() 方法,需要手动转换返回的结果转换为一个目标对象类型,并传递一个对象数组作为参数。在SimpleJdbcTemplate类,它是更加人性化和简单。
jdbctemplate VS 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
findAll(){ String sql = "SELECT * FROM CUSTOMER"; List
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
customerAs = customerSimpleDAO.findAll(); for(Customer cust: customerAs){ System.out.println("Customer As : " + customerAs); } List
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的友好补充它。

 

下载代码 – 

转载于:https://www.cnblogs.com/soundcode/p/6367570.html

你可能感兴趣的文章
Stone Crusher Used in High-speed Railway Construction
查看>>
Powerful Function of Hammer Crushers Two
查看>>
an error occurred during the file system check错误的解决
查看>>
logstash json和rubydebug 第次重启logstash都会把所有的日志读完 而不是只读入新输入的内容...
查看>>
实验吧-web-Guess Next Session(session简介)
查看>>
C语言i++和++i的区别和指针*(a++)和*(++a)的区别
查看>>
在一个CommandField中为删除按钮设置OnClientClick属性
查看>>
Linux常用命令-1
查看>>
(一)Java工程化--Maven基础
查看>>
Linux磁盘空间爆满,MySQL无法启动
查看>>
Inception搭建
查看>>
设置联想键盘恢复F1~F12默认按键的操作办法
查看>>
Python编程之sqlite3数据库
查看>>
CROS+node-basis+ajax
查看>>
SetWindowLong函数
查看>>
What's New in Core Data in iOS 7
查看>>
个人编程作业2
查看>>
magento首页标题的修改
查看>>
刚刚拿到驾照?交警告诉你新手必知的10个行车好习惯
查看>>
linux 操作 mysql 指定端口登录 以及启动 停止
查看>>