初識spring(十一)

14073 ワード

Springのjdbcへのサポート
Springはc 3 p 0でデータベースをサポートできます.
Dept.java
package cn.itcast.h_jdbc;

public class Dept {

    private int deptId;
    private String deptName;
    public int getDeptId() {
        return deptId;
    }
    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

}

UserDao.java
package cn.itcast.h_jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;

public class UserDao {


    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public void save() {
        String sql = "insert into t_dept(deptName) values('test');";
        jdbcTemplate.update(sql);
    }

    public Dept findById(int id) {
        String sql = "select * from t_dept where deptId=?";
        List list = jdbcTemplate.query(sql,new MyResult(), id);
        return (list!=null && list.size()>0) ? list.get(0) : null;
    }

    public List getAll() {
        String sql = "select * from t_dept";
        List list = jdbcTemplate.query(sql, new MyResult());
        return list;
    }




    class MyResult implements RowMapper{

        //         
        @Override
        public Dept mapRow(ResultSet rs, int index) throws SQLException {
            Dept dept = new Dept();
            dept.setDeptId(rs.getInt("deptId"));
            dept.setDeptName(rs.getString("deptName"));
            return dept;
        }

    }
}

UserDao1.JAvaこれは伝統的なjdbcです
package cn.itcast.h_jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class UserDao01 {


    /*
     *      
     *         :
     *      1.     
     *      2. jdbc        
     */
    public void save() {
        try {
            String sql = "insert into t_dept(deptName) values('test');";
            Connection con = null;
            Statement stmt = null;
            Class.forName("com.mysql.jdbc.Driver");
            //     
            con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root");
            //       
            stmt =  con.createStatement();
            //   
            stmt.execute(sql);

            //   
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

UserDao2.JAvaはdatasourceを使用してデータベース操作を行います.
package cn.itcast.h_jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.sql.DataSource;

public class UserDao02 {

    // IOC    
    private DataSource dataSource;
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    public void save() {
        try {
            String sql = "insert into t_dept(deptName) values('test');";
            Connection con = null;
            Statement stmt = null;
            //     
            con = dataSource.getConnection();
            //       
            stmt =  con.createStatement();
            //   
            stmt.execute(sql);

            //   
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo">property>
        <property name="user" value="root">property>
        <property name="password" value="root">property>
        <property name="initialPoolSize" value="3">property>
        <property name="maxPoolSize" value="10">property>
        <property name="maxStatements" value="100">property>
        <property name="acquireIncrement" value="2">property>
    bean>

    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <bean id="userDao" class="cn.itcast.h_jdbc.UserDao">
        <property name="jdbcTemplate" ref="jdbcTemplate">property>
    bean>
beans>