EmployeeDao 代码
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* @author gaohu
* @function 通过DBCP连接池查询数据表tb_employee
* @datatime 20016-5-12 下午22:20
*/
public class EmployeeDao extends JdbcDaoSupport{
public List findAll(){
List list = getJdbcTemplate().queryForList("SELECT * FROM tb_employee");
return list;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDao eDao = (EmployeeDao) context.getBean("employeeDao");
List<Map> list = eDao.findAll();
for(int i = 0 ; i < list.size() ; i++){
Map map = list.get(i);
System.out.println("编号:"+map.get("id")+" 姓名:"+map.get("name")+" 部门:"+map.get("dept")+" 职务:"+map.get("level"));
}
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_database17"/>
<property name="username" value="root"/>
<property name="password" value="111"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao" class="EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>