首页 技术 正文
技术 2022年11月15日
0 收藏 523 点赞 4,592 浏览 3205 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,955
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774