Spring对jdbc支持

时间:2022-07-21 11:56:06

4. Spring对jdbc支持

spring对jdbc提供了很好的支持

  体现在:

    1.Spring对C3P0连接池的支持很完善

    2.Spring对jdbc提供了jdbcTemplate来简化jdbc的操作

      jdbcTemplate模板工具类,类似于DbUtils工具类

  使用步骤:

1)引入jar文件

      spring-jdbc-3.2.5.RELEASE.jar   jdbc支持包

      spring-tx-3.2.5.RELEASE.jar    事务包,必须引入,否则会报错。

2) 优化

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;
    }

}
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 {

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

    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<Dept> list = jdbcTemplate.query(sql,new MyResult(), id);//queryForList,queryForMap
        ) ? list.) : null;
    }

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

    class MyResult implements RowMapper<Dept>{

        // 告知框架如何封装一行记录
        @Override
        public Dept mapRow(ResultSet rs, int index) throws SQLException {  //index是当前行的索引
            Dept dept = new Dept();
            dept.setDeptId(rs.getInt("deptId"));
            dept.setDeptName(rs.getString("deptName"));
            return dept;
        }

    }
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- 1. 数据源对象: C3P0连接池 -->
    <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>

    <!-- 2. 创建JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- dao 实例 -->
    <bean id="userDao" class="cn.itcast.h_jdbc.UserDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
</beans>       
package cn.itcast.h_jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    // 容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/h_jdbc/bean.xml");

    @Test
    public void testApp() throws Exception {
        UserDao ud = (UserDao) ac.getBean("userDao");
//        ud.save();
        System.));
        System.out.println(ud.getAll());
    }
}