Spring对JDBC的DAO支持

    Spring提供JdbcDaoSupport来实现对JDBC的DAO支持。在JdbcDaoSupport类中提供了两个方法,分别是:

        void setDataSource(DataSource dataSource) :采用setter依赖注入来设置数据源

        getJdbcTemplate():获得JdbcTemplate来执行CRUD操作。

    下面举例来详细说明:

    假设有一个名为dao的数据库,数据库中有一个student表,表中的字段为id,name,sex

    1、首先创建一个JavaBean来作为数据传输对象:

    StudentBean.java

  1. package bean;
  2. public class StudentBean {
  3.     
  4.     private int id;
  5.     
  6.     private String name;
  7.     
  8.     private String sex;
  9.     
  10.     public StudentBean() {
  11.         
  12.     }
  13.     public StudentBean(int id, String name, String sex) {
  14.         super();
  15.         this.id = id;
  16.         this.name = name;
  17.         this.sex = sex;
  18.     }
  19.     public int getId() {
  20.         return id;
  21.     }
  22.     public void setId(int id) {
  23.         this.id = id;
  24.     }
  25.     public String getName() {
  26.         return name;
  27.     }
  28.     public void setName(String name) {
  29.         this.name = name;
  30.     }
  31.     public String getSex() {
  32.         return sex;
  33.     }
  34.     public void setSex(String sex) {
  35.         this.sex = sex;
  36.     }
  37. }

    2、编写DAO接口

    StudentDao.java

  1. package dao;
  2. import bean.StudentBean;
  3. public interface StudentDao {
  4.     
  5.     /**
  6.      * 根据传入的StudentBean实例插入记录
  7.      * @param s 需要保存的StudentBean实例
  8.      */
  9.     void createStudent(StudentBean s) throws Exception;
  10.     
  11.     /**
  12.      * 根据传入的id属性,查找匹配名字字符串的StudentBean实例
  13.      * @param id 需要查找StudentBean的id
  14.      * @return 匹配名字字符串的StudentBean实例
  15.      */
  16.     StudentBean getStudent(int id) throws Exception;
  17.     
  18.     /**
  19.      * 根据传入的name属性,删除记录
  20.      * @param name 需要删除的StudentBean的name
  21.      */
  22.     void deleteStudent(String name) throws Exception;
  23.     
  24.     /**
  25.      * 根据传入的StudentBean实例更新对应的记录
  26.      * @param s 需要更新的StudentBean实例
  27.      */
  28.     void updateStudent(StudentBean s) throws Exception;
  29. }

    3、编写DAO接口的实现类,继承了JdbcDaoSupport

    StudentDaoJdbc.java

 

  1. package dao;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import org.springframework.jdbc.core.RowMapper;
  5. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  6. import bean.StudentBean;
  7. public class StudentDaoJdbc extends JdbcDaoSupport implements StudentDao {
  8.     public void createStudent(StudentBean s) throws Exception {
  9.         Object[] args = {new Integer(s.getId()),s.getName(),s.getSex()};
  10.         String sql = "insert into student(id,name,sex) values(?,?,?)";
  11.         //获得JdbcTemplate对象,并调用update方法对数据库进行操作
  12.         this.getJdbcTemplate().update(sql,args);
  13.     }
  14.     public void deleteStudent(String name) throws Exception {
  15.         Object[] args = {name};
  16.         String sql = "delete from student where name = ?";
  17.         this.getJdbcTemplate().update(sql, args);
  18.     }
  19.     public StudentBean getStudent(int id) throws Exception {
  20.         Object[] args = {new Integer(id)};
  21.         String sql = "select id,name,sex from student where id = ?";
  22.         
  23.         //将查询的ResultSet包装成对象后返回
  24.         return (StudentBean)getJdbcTemplate().queryForObject(sql,args,new StudentRowMapper());
  25.     }
  26.     public void updateStudent(StudentBean s) throws Exception {
  27.         Object[] args = {s.getName(),s.getSex(),new Integer(s.getId())};
  28.         String sql = "update student set name = ?,sex = ? where id = ?";
  29.         this.getJdbcTemplate().update(sql, args);
  30.     }
  31.     
  32.     /**
  33.      * @author Administrator
  34.      * 私有内部类
  35.      * 继承了RowMapper接口
  36.      * 用于将ResultSet包装成相应的值对象后返回
  37.      */
  38.     private class StudentRowMapper implements RowMapper {
  39.         public Object mapRow(ResultSet rs,int rowNumber) throws SQLException {
  40.             StudentBean sb = new StudentBean(rs.getInt("id"),rs.getString("name"),rs.getString("sex"));
  41.             return sb;
  42.         }
  43.     }
  44. }

    4、最后描述配置文件(包括对数据源的配置,并将数据源对象注入到StudentDaoJdbc实例中)

    applicationContext.xml

 

  1. <?xml version="1.0" encoding="GB2312"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xmlns:aop="http://www.springframework.org/schema/aop"
  5.          xmlns:tx="http://www.springframework.org/schema/tx"
  6.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9.     <!-- 配置dbcp数据源 -->
  10.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  11.         <property name="driverClassName">
  12.             <value>com.mysql.jdbc.Driver</value>
  13.         </property>
  14.         <property name="url">
  15.             <value>jdbc:mysql://localhost:3306/dao</value>
  16.         </property>
  17.         <property name="username">
  18.             <value>root</value>
  19.         </property>
  20.         <property name="password">
  21.             <value>4231992</value>
  22.         </property>
  23.     </bean>
  24.     <!-- 配置用户的DAO bean -->
  25.     <bean id="studentDao" class="dao.StudentDaoJdbc">
  26.         <!-- 为DAO实例注入所需的DataSource实例,采用setter注入,setter方法位于StudentDaoJdbc的父类JdbcDaoSupport中 -->
  27.         <property name="dataSource">
  28.             <ref local="dataSource"/>
  29.         </property>
  30.     </bean>
  31. </beans>

    通过测试,我们可以看到通过Spring实现的DAO,代码简洁,不需要频繁地进行数据库连接和捕获异常,也不需要创建DAO工厂,而是由Spring自动地管理DAO实例的创建。         

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值