本章使用的是 scott 用户
Emp 实体类
package cn.entity; import java.util.Date; public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno; @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]"; } public Emp() { } public Emp(String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm, Integer deptno) { this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; } public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm, Integer deptno) { this.empno = empno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } }
EmpDao 业务类
package cn.dao; import java.util.List; import java.util.Map; import cn.entity.Emp; public interface EmpDao { public List<Map<String, Object>> findEmpAndDept(); public List<Emp> findAllEmps(); public Emp findEmp(Integer empno); public int countEmp(Integer deptno); public int save(Emp emp); }
package cn.dao.impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import cn.dao.EmpDao; import cn.entity.Emp; public class EmpDaoImpl extends JdbcDaoSupport implements EmpDao { /** * 内部类,将结果与类进行封装 * */ private class EmpRowMapper implements RowMapper<Emp>{ @Override public Emp mapRow(ResultSet rs,int rowNum) throws SQLException{ Emp emp = new Emp(); emp.setEmpno(rs.getInt("empno")); emp.setEname(rs.getString("ename")); emp.setJob(rs.getString("job")); emp.setMgr(rs.getInt("mgr")); emp.setHiredate(rs.getDate("hiredate")); emp.setSal(rs.getDouble("sal")); emp.setComm(rs.getDouble("comm")); emp.setDeptno(rs.getInt("deptno")); return emp; } } /** * 每条记录封装成 Map 对象 K- 列名 V- 记录内容 * */ @Override public List<Map<String, Object>> findEmpAndDept() { return super.getJdbcTemplate().queryForList( "select empno, ename, sal, comm, dname from emp,dept where emp.deptno = dept.deptno"); } @Override public List<Emp> findAllEmps() { return super.getJdbcTemplate().query( "select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp", new EmpRowMapper()); } @Override public Emp findEmp(Integer empno) { return super.getJdbcTemplate().queryForObject( "select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where empno=?", new EmpRowMapper(),empno); } @Override public int countEmp(Integer deptno) { return super.getJdbcTemplate().queryForInt( "select count(*) from emp where deptno=?",deptno); } @Override public int save(Emp emp) { return super.getJdbcTemplate().update( "insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (seq_emp.nextval, ?, ?, ?, ?, ?, ?, ?)", emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(), emp.getSal(),emp.getComm(),emp.getDeptno() ); } }
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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle11" /> <property name="username" value="scott" /> <property name="password" value="tiger" /> </bean> <!-- 配置 jbdc 模板并注入 dataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置 DAO --> <bean id="empDao" class="cn.dao.impl.EmpDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
TestEmp JUnit测试类
package cn.test; import java.util.Date; import java.util.List; import java.util.Map; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.dao.EmpDao; import cn.entity.Emp; public class TestEmp { EmpDao empDao; static ApplicationContext ctx; @BeforeClass public static void init(){ ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); } @Before public void setUp(){ empDao=(EmpDao) ctx.getBean("empDao"); } @After public void tearDown(){ empDao=null; } @AfterClass public static void destory(){ ctx=null; } //@Test public void tesFindEmpAndDept(){ List<Map<String, Object>> emps = empDao.findEmpAndDept(); //每个 Map 代表一条记录 for (Map<String, Object> emp : emps) { System.out.println("编号:"+emp.get("empno")+ ",姓名:"+emp.get("ename")+",工资:"+ emp.get("sal")+",部门:"+emp.get("dname")); } } //@Test public void testFindAllEmps(){ List<Emp> emps = empDao.findAllEmps(); for (Emp emp : emps) { System.out.println(emp); } } //@Test public void testFindEmp(){ System.out.println(empDao.findEmp(7782)); } //@Test public void testCountEmp(){ System.out.println(empDao.countEmp(20)); } @Test public void testSave(){ Emp emp = new Emp("景临境", "CLERK", 7902, new Date(), 800.00, 300.00, 30); System.out.println(empDao.save(emp)); } }