Spring提供JdbcDaoSupport来实现对JDBC的DAO支持。在JdbcDaoSupport类中提供了两个方法,分别是:
void setDataSource(DataSource dataSource) :采用setter依赖注入来设置数据源
getJdbcTemplate():获得JdbcTemplate来执行CRUD操作。
下面举例来详细说明:
假设有一个名为dao的数据库,数据库中有一个student表,表中的字段为id,name,sex
1、首先创建一个JavaBean来作为数据传输对象:
StudentBean.java
- package bean;
- public class StudentBean {
- private int id;
- private String name;
- private String sex;
- public StudentBean() {
- }
- public StudentBean(int id, String name, String sex) {
- super();
- this.id = id;
- this.name = name;
- this.sex = sex;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
2、编写DAO接口
StudentDao.java
- package dao;
- import bean.StudentBean;
- public interface StudentDao {
- /**
- * 根据传入的StudentBean实例插入记录
- * @param s 需要保存的StudentBean实例
- */
- void createStudent(StudentBean s) throws Exception;
- /**
- * 根据传入的id属性,查找匹配名字字符串的StudentBean实例
- * @param id 需要查找StudentBean的id
- * @return 匹配名字字符串的StudentBean实例
- */
- StudentBean getStudent(int id) throws Exception;
- /**
- * 根据传入的name属性,删除记录
- * @param name 需要删除的StudentBean的name
- */
- void deleteStudent(String name) throws Exception;
- /**
- * 根据传入的StudentBean实例更新对应的记录
- * @param s 需要更新的StudentBean实例
- */
- void updateStudent(StudentBean s) throws Exception;
- }
3、编写DAO接口的实现类,继承了JdbcDaoSupport
StudentDaoJdbc.java
- package dao;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.springframework.jdbc.core.RowMapper;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- import bean.StudentBean;
- public class StudentDaoJdbc extends JdbcDaoSupport implements StudentDao {
- public void createStudent(StudentBean s) throws Exception {
- Object[] args = {new Integer(s.getId()),s.getName(),s.getSex()};
- String sql = "insert into student(id,name,sex) values(?,?,?)";
- //获得JdbcTemplate对象,并调用update方法对数据库进行操作
- this.getJdbcTemplate().update(sql,args);
- }
- public void deleteStudent(String name) throws Exception {
- Object[] args = {name};
- String sql = "delete from student where name = ?";
- this.getJdbcTemplate().update(sql, args);
- }
- public StudentBean getStudent(int id) throws Exception {
- Object[] args = {new Integer(id)};
- String sql = "select id,name,sex from student where id = ?";
- //将查询的ResultSet包装成对象后返回
- return (StudentBean)getJdbcTemplate().queryForObject(sql,args,new StudentRowMapper());
- }
- public void updateStudent(StudentBean s) throws Exception {
- Object[] args = {s.getName(),s.getSex(),new Integer(s.getId())};
- String sql = "update student set name = ?,sex = ? where id = ?";
- this.getJdbcTemplate().update(sql, args);
- }
- /**
- * @author Administrator
- * 私有内部类
- * 继承了RowMapper接口
- * 用于将ResultSet包装成相应的值对象后返回
- */
- private class StudentRowMapper implements RowMapper {
- public Object mapRow(ResultSet rs,int rowNumber) throws SQLException {
- StudentBean sb = new StudentBean(rs.getInt("id"),rs.getString("name"),rs.getString("sex"));
- return sb;
- }
- }
- }
4、最后描述配置文件(包括对数据源的配置,并将数据源对象注入到StudentDaoJdbc实例中)
applicationContext.xml
- <?xml version="1.0" encoding="GB2312"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!-- 配置dbcp数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/dao</value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>4231992</value>
- </property>
- </bean>
- <!-- 配置用户的DAO bean -->
- <bean id="studentDao" class="dao.StudentDaoJdbc">
- <!-- 为DAO实例注入所需的DataSource实例,采用setter注入,setter方法位于StudentDaoJdbc的父类JdbcDaoSupport中 -->
- <property name="dataSource">
- <ref local="dataSource"/>
- </property>
- </bean>
- </beans>
通过测试,我们可以看到通过Spring实现的DAO,代码简洁,不需要频繁地进行数据库连接和捕获异常,也不需要创建DAO工厂,而是由Spring自动地管理DAO实例的创建。