SimpleJdbcTemplate `query()`メソッドを使用してデータベースからデータを照会または抽出する方法を示すいくつかの例を以下に示します。 JdbcTemplate `query()`では、返された結果をオブジェクト型に手動でキャストし、Object配列をパラメータとして渡す必要があります。 SimpleJdbcTemplateでは、ユーザーフレンドリーでシンプルです。

1.単一行のクエリ

データベースから単一の行をクエリまたは抽出してモデルクラスに変換する方法を示す2つの方法があります。

1.1カスタムRowMapper

通常、RowMapperインタフェースを実装して、ニーズに合わせてカスタムRowMapperを作成することをお勧めします。

package com.mkyong.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 List<Customer> findAll(){

    String sql = "SELECT **  FROM CUSTOMER";

    List<Customer> customers =
        getSimpleJdbcTemplate().query(sql,
           ParameterizedBeanPropertyRowMapper.newInstance(Customer.class));

    return customers;
}

3.単一の値を照会する

データベースから単一の列の値を照会または抽出します。

3.1単一の列名

これは、単一の列名をStringとして照会する方法を示しています。

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.mkyong.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.dao.CustomerDAO;
import com.mkyong.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-Querying-Example.zip

(15 KB)