Spring+JDBCインスタンス

4427 ワード

  • Customer表
  • この例では、MySQLデータベースを使用します.CREATE TABLE customer ( CUST_ID int(10) unsigned NOT NULL AUTO_INCREMENT, NAME varchar(100) NOT NULL, AGE int(10) unsigned NOT NULL, PRIMARY KEY ( CUST_ID ) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  • Customerモデル
  • ユーザーのデータを格納するための顧客モデルを追加します.package com.yiibai.customer.model;
    import java.sql.Timestamp;
    public class Customer { int custId; String name; int age;//getter and setter methods
    }
  • データアクセスオブジェクト(DAO)モード
  • Customer Daoインタフェース
    package com.yiibai.customer.dao;
    import com.yiibai.customer.model.Customer;
    public interface CustomerDAO{public void insert(Customer customer);public Customer findByCustomerId(int custId);}お客様のDAO実装は、JDBCを使用して簡単なinsert文とselect SQL文を発行します.package com.yiibai.customer.dao.impl;
    import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import com.yiibai.customer.dao.CustomerDAO; import com.yiibai.customer.model.Customer;
    public class JdbcCustomerDAO implements CustomerDAO { private DataSource dataSource;
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    
    public void insert(Customer customer){
        
        String sql = "INSERT INTO CUSTOMER " +
                "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
        Connection conn = null;
        
        try {
            conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, customer.getCustId());
            ps.setString(2, customer.getName());
            ps.setInt(3, customer.getAge());
            ps.executeUpdate();
            ps.close();
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
            
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {}
            }
        }
    }
    
    public Customer findByCustomerId(int custId){
        
        String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
        
        Connection conn = null;
        
        try {
            conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, custId);
            Customer customer = null;
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                customer = new Customer(
                    rs.getInt("CUST_ID"),
                    rs.getString("NAME"), 
                    rs.getInt("Age")
                );
            }
            rs.close();
            ps.close();
            return customer;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                conn.close();
                } catch (SQLException e) {}
            }
        }
    }
    

    }
  • Spring bean構成
  • Spring beanプロファイルにcustomerDAOとデータソースを作成します.File : Spring-Customer.xml
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        
    
    

    File : Spring-Datasource.xml
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    
        
        
        
        
    
    

    File : Spring-Module.xml
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">