Spring对jdbc支持

本文介绍如何使用Spring框架整合JDBC进行数据库操作,包括配置数据源、创建JdbcTemplate对象及实现增删改查等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用步骤:
    1)引入jar文件
        spring-jdbc-3.2.5.RELEASE.jar
        spring-tx-3.2.5.RELEASE.jar
    2) 优化

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>        

Dept.java

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

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);
        return (list!=null && list.size()>0) ? list.get(0) : 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 {
            Dept dept = new Dept();
            dept.setDeptId(rs.getInt("deptId"));
            dept.setDeptName(rs.getString("deptName"));
            return dept;
        }

    }
}

App.java

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.out.println(ud.findById(9));
        System.out.println(ud.getAll());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值