ITeye博客

Spring对JDBC的支持

ustczhuli · 2013-01-16 13:57 · JDBC · Spring · 编程语言 · Spring · JDBC · Markdown

从来没使用过,感觉还是很有意思的,有工夫可以玩完,呵呵。

 

spring提供了类 org.springframework.jdbc.core.JdbcTemplate,它借助配置文件获取数据库信息,实现了对JDBC开发过程中的 加载驱动,创建连接,执行sql语句,对事务进行处理以及一些数据类型转化等操作的封装。只需要程序员对其传入sql语句和必要的参数即可轻松进行 JDBC编程。

 

Dao类:

public class StudentDaoJDBCImpl implements StudentDao {
  private JdbcTemplate template;
        //template的setter.getter方法
  @Override
  public void delete(Student s) {
    deleteById(s.getId());
  }

  @Override
  public void deleteById(Integer id) {
    String sql = "delete from spring_student where id=?";
    Object[] args = { id };// 数组元素依次对应sql语句中的?
    template.update(sql, args);// 只需传sql语句和参数数组,template会自动加载驱动,创建连结,执行sql语句,并且进行事务处理
  }

  @Override
  public void insert(Student s) {
    String sql = "insert into spring_student(id,name,birthday) values(spring_stu_seq.nextval,?,?)";
    Object[] args = { s.getName(), s.getBirthday() };//template会对Date类型数据进行转化
    template.update(sql, args);
  }

  @Override
  public Student queryById(Integer id) {
    String sql = "select id,name,birthday from spring_student where id=?";
    Object[] args = { id };
    final Student s = new Student();
    template.query(sql, args, new RowCallbackHandler() {
      //它要求程序员自己在RowCallbackHandler类型对象的processRow方法中对结果集进行处理
      @Override
      public void processRow(ResultSet rs) throws SQLException {
        if (!(rs.isAfterLast())) {// 把查询结果封装到一个对象中
          s.setId(rs.getInt(1));
          s.setName(rs.getString(2));
          s.setBirthday(rs.getDate(3));
        }
      }
    });
    if (s.getId() == null) {
      return null;
    } else {
      return s;
    }
  }

  @Override
  public List<Student> queryAll() {
    String sql="select id,name,birthday from spring_student";
    List<Student> ret=template.query(sql, new RowMapper(){
      //它要求程序员自己在RowMapper类型对象的mapRow方法中说明结果集中每条记录如何封装成一个对象,
            //template.query方法会自动把对象加入到集合中。
      @Override
      public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        //rs:结果集
        //rowNum:迭代次数
        Student s=new Student();
        s.setId(rs.getInt(1));
        s.setName(rs.getString(2));
        s.setBirthday(rs.getDate(3));
        return s;
      }
        
    });
    return ret;
  }
}

 

配置:

<beans>
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
      <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
      <value>jdbc:oracle:thin:@localhost:1521:orcl10</value>
    </property>
    <property name="username">
      <value>scott</value>
    </property>
    <property name="password">
      <value>yf123</value>
    </property>
  </bean>
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property>
  </bean>
  <bean id="dao" class="com.yangfei.spring.jdbc.dao.StudentDaoJDBCImpl">
    <property name="template">
      <ref local="jdbcTemplate"/>
    </property>
  </bean>
</beans>

 

测试:

public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("jdbc.xml");
    StudentDao dao=(StudentDao)ctx.getBean("dao");
    System.out.println(dao.queryById(2));
    
    //dao.deleteById(1);
    Student s=new Student();
    s.setName("xiaozhang");
    s.setBirthday(new Date());
    dao.insert(s);

本文出自 “夜狼” 博客,请务必保留此出处http://yangfei520.blog.51cto.com/1041581/246476

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。