Spring SimpleJdbcTemplateクエリの例
SimpleJdbcTemplatequery()メソッドを使用してデータベースにデータをクエリまたは抽出する方法を示すいくつかの例を次に示します。 JdbcTemplatequery()では、返された結果を目的のオブジェクトタイプに手動でキャストし、オブジェクト配列をパラメーターとして渡す必要があります。 SimpleJdbcTemplateでは、よりユーザーフレンドリーでシンプルです。
jdbctemplate vesus simplejdbctemplate
このSimpleJdbcTemplate exampleをこのJdbcTemplate exampleと比較してください。
1. 単一行のクエリ
データベースから単一の行をクエリまたは抽出し、それをモデルクラスに変換する方法を示す2つの方法を次に示します。
1.1 Custom RowMapper
通常、ニーズに合わせてカスタムRowMapperを作成するには、RowMapperインターフェイスを実装することを常にお勧めします。
package com.example.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では、「BeanPropertyRowMapper」の代わりに「ParameterizedBeanPropertyRowMapper」を使用する必要があります。
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 ListfindAll(){ String sql = "SELECT * FROM CUSTOMER"; List customers = getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Customer.class)); return customers; }
3. 単一の値のクエリ
データベースから単一の列値を照会または抽出します。
3.1 Single column name
単一の列名を文字列としてクエリする方法を示します。
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 Total number of rows
データベースから行の総数を照会する方法を示しています。
public int findTotalCustomer(){
String sql = "SELECT COUNT(*) FROM CUSTOMER";
int total = getSimpleJdbcTemplate().queryForInt(sql);
return total;
}
それを実行します
package com.example.common;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.customer.dao.CustomerDAO;
import com.example.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に対応した単なる補足です。
ソースコードをダウンロード
ダウンロード–Spring-SimpleJdbcTemplate-Querying-Example.zip(15 KB)