DAO应该是现在比较流行的一种开发模式.不管是用什么更新的技术,hibernate或者其它的.DAO模式是应该遵循的.
我写DAO大概是这样的:一个DAO接口,定义好要实现的方法名称;一个Impl实现类,主要控制连接及关闭数据库;
接下来会有一个写具体方法的数据库实现类;还有一个工厂类,可以更灵活的取得不同的DAO.
上面的写法感觉上不错,但是写起来比较麻烦,而且如果使用数据库连接池的活,利用JUnit做测试的时候会比较麻烦.
下面我们利用Spring的JDBC模板写一个DAO测试实例,你会发现它是如此的简单且实用.
使用的了DBCP的连接池.
下面是要引入的JAR包
<classpathentry kind="lib" path="D:/classes/spring.jar"/>
<classpathentry kind="lib" path="D:/classes/junit-3.8.1.jar"/>
<classpathentry kind="lib" path="D:/classes/spring-dao.jar"/>
<classpathentry kind="lib" path="D:/classes/mysql-connector-java-3.0.9-stable-bin.jar"/>
<classpathentry kind="lib" path="D:/classes/commons-dbcp-1.1.jar"/>
<classpathentry kind="lib" path="D:/classes/commons-pool-1.1.jar"/>
<classpathentry kind="lib" path="D:/classes/commons-collections.jar"/>
<classpathentry kind="lib" path="D:/classes/commons-beanutils.jar"/>
<classpathentry kind="lib" path="D:/classes/commons-lang-2.0.jar"/>
<classpathentry kind="lib" path="D:/classes/commons-logging-1.0.3.jar"/>
SpringDao.java文件
package com.bcxy.spring.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import com.bcxy.spring.dao.model.TestModel;
public class SpringDao {
DataSource ds = null;
JdbcTemplate jt = null;
Log log = LogFactory.getLog(SpringDao.class);
public List springQuery(){
final List tests = new ArrayList();
String sql = " select * from test ";
jt = new JdbcTemplate(ds);
jt.query(sql, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
//
TestModel tm = new TestModel();
tm.setId(rs.getInt("id"));
tm.setPw(rs.getString("pw"));
tm.setUn(rs.getString("un"));
//
tests.add(tm);
}
});
return tests;
}
public void springUpdate() {
String sql = "update test set pw=? where id=?";
jt = new JdbcTemplate(ds);
jt.update(sql, new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
//
ps.setString(1, "maxcard");
ps.setInt(2, 1);
}
});
log.info("update test a record.");
}
/**
* @return
*/
public DataSource getDs() {
return ds;
}
/**
* @param source
*/
public void setDs(DataSource source) {
ds = source;
}
}
Spring配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<description>Spring Quick Start</description>
<!-- datasource -->
<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/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<!-- test dao -->
<bean id="testdao"
class="com.bcxy.spring.dao.SpringDao"
>
<property name="ds">
<ref local="datasource"/>
</property>
</bean>
</beans>
下面我们用JUnit测试一下:
......
public void testSpringUpdate() throws FileNotFoundException {
//
InputStream is = new FileInputStream("bean.xml");
XmlBeanFactory bean = new XmlBeanFactory(is);
SpringDao sd = (SpringDao)bean.getBean("testdao");
sd.springUpdate();
}