日期:2014-05-16 浏览次数:20454 次
package com.jiaozg.dao; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.JdbcTemplate; import com.jiaozg.model.Dept; import com.jiaozg.util.SpringUtil; 主要类: public class DeptDao { private JdbcTemplate jdbcT; public void setJdbcT(JdbcTemplate jdbcT) { this.jdbcT = jdbcT; } public List findALL() { String sql = "select * from dept"; return jdbcT.queryForList(sql); } public List<Dept> findALLDepts() { List<Dept> depts = new ArrayList<Dept>();; String sql = "select * from Dept"; List list = jdbcT.queryForList(sql); Iterator iterator = list.iterator(); Dept dept = null; while (iterator.hasNext()) { Map map4dept = (Map) iterator.next(); dept = new Dept(); dept.setDeptNo(((BigDecimal) map4dept.get("DEPTNO")).intValue()); dept.setDName((String)map4dept.get("DNAME")); dept.setLoc((String)map4dept.get("LOC")); depts.add(dept); } return depts; } public int delete(int bid){ String sql = "delete from DeptInfo where bid =?"; return jdbcT.update(sql, new Object[]{bid}); } public static void main(String[] args) { DeptDao dao = (DeptDao) SpringUtil.getBean("deptDao"); List<Dept> depts = dao.findALLDepts();; for(Dept dept:depts){ System.out.println(dept.getDeptNo()+","+dept.getDName()+","+dept.getLoc()); } System.out.println("---------------------------------"); List list = dao.findALL(); for(Iterator it = list.iterator(); it.hasNext(); ) { System.out.println(it.next()); } } }
public class SpringUtil { private static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); public static Object getBean(String beanName){ return ctx.getBean(beanName); } }
<?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="springDSN" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"> </property> <property name="username" value="scott"></property> <property name="password" value="Qwer1234"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" lazy-init="false" autowire="default" dependency-ch