Spring数据访问之JdbcTemplate
使用JdbcTemplate的基本操作步骤
1.引jar包
项目的基本架构
这里重点看实现类的内容
1 package cn.hmy.dao.impl; 2 3 4 import java.util.List; 5 6 import org.springframework.jdbc.core.support.JdbcDaoSupport; 7 8 import cn.hmy.beans.Emp; 9 import cn.hmy.dao.IEmpDao; 10 import cn.hmy.util.MyRowMapper; 11 12 public class EmpDaoImpl extends JdbcDaoSupport implements IEmpDao { 13 14 //获取员工总人数 15 public int getTotalCount() throws Exception { 16 String sql="select * from emp"; 17 Integer count=this.getJdbcTemplate().queryForObject(sql, Integer.class); 18 return count; 19 } 20 21 22 //添加员工信息 23 public int addEmp(Emp emp) throws Exception { 24 String sql="insert into emp(empNo,empName,deptNo) values(?,?,?)"; 25 int count = this.getJdbcTemplate().update(sql, emp.getEmpNo(),emp.getEmpName(),emp.getDeptNo()); 26 return count; 27 } 28 29 //查询所有的员工信息 30 public List<Emp> getAllEmps() throws Exception { 31 String sql="select * from emp"; 32 List<Emp> list = this.getJdbcTemplate().query(sql,new MyRowMapper()); 33 return list; 34 } 35 36 37 38 }
1 package cn.hmy.service.impl; 2 3 import java.util.List; 4 5 import cn.hmy.beans.Emp; 6 import cn.hmy.dao.IEmpDao; 7 import cn.hmy.service.IEmpService; 8 9 public class EmpServiceImpl implements IEmpService{ 10 11 private IEmpDao empDao; 12 13 //获取员工总记录数 14 public int getTotalCount() throws Exception{ 15 return empDao.getTotalCount(); 16 } 17 18 //添加员工 19 public int addEmp(Emp emp) throws Exception { 20 return empDao.addEmp(emp); 21 } 22 23 //查询员工的所有信息 24 public List<Emp> getAllEmps() throws Exception { 25 return empDao.getAllEmps(); 26 } 27 public IEmpDao getEmpDao() { 28 return empDao; 29 } 30 31 public void setEmpDao(IEmpDao empDao) { 32 this.empDao = empDao; 33 } 34 35 36 37 38 }
在查询的时候我们为了能使查询到的结果集返回泛型集合,我们又为了减少代码量,就提出一个util类MyRowMapper类,来转换结果集
1 package cn.hmy.util; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 6 import org.springframework.jdbc.core.RowMapper; 7 8 import cn.hmy.beans.Emp; 9 10 public class MyRowMapper implements RowMapper{ 11 12 public Emp mapRow(ResultSet rs, int rowNum) throws SQLException { 13 Emp emp=new Emp(); 14 emp.setDeptNo(rs.getInt("deptNo")); 15 emp.setEmpName(rs.getString("empName")); 16 emp.setEmpNo(rs.getInt("empNo")); 17 return emp; 18 } 19 20 }
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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注册jdbc属性 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!-- Spring 内置配置数据源 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.className}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> --> <!--注册dbcp数据源 --> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.className}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> --> <!--注册c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.className}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置jdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 管理empDao实现类 --> <bean id="empDao" class="cn.hmy.dao.impl.EmpDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 管理empService实现类 --> <bean id="empService" class="cn.hmy.service.impl.EmpServiceImpl"> <property name="empDao" ref="empDao"></property> </bean> </beans>
书写测试类
1 package cn.hmy.test; 2 3 import java.util.List; 4 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import cn.hmy.beans.Emp; 10 import cn.hmy.service.IEmpService; 11 12 public class MyTest { 13 @Test 14 public void getAdd() throws Exception{ 15 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 16 IEmpService service=(IEmpService)context.getBean("empService"); 17 Emp emp=new Emp(); 18 emp.setDeptNo(1); 19 emp.setEmpName("123"); 20 emp.setEmpNo(10); 21 int addEmp; 22 try { 23 addEmp = service.addEmp(emp); 24 System.out.println(addEmp); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 } 29 @Test 30 public void getAllEmps() throws Exception{ 31 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 32 IEmpService service=(IEmpService)context.getBean("empService"); 33 List<Emp> allEmps = service.getAllEmps(); 34 for (Emp emp : allEmps) { 35 System.out.println(emp.getEmpName()); 36 } 37 38 } 39 }
原文:http://www.cnblogs.com/hmy-1365/p/5958303.html