1. pom.xml,需要的spring jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jd.myspring</groupId> <artifactId>myspring</artifactId> <version>0.0.1-SNAPSHOT</version> <name>myspring</name> <description>myspring</description> <properties> <spring-framework.version>3.2.3.RELEASE</spring-framework.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.dom4j</groupId> <artifactId>com.springsource.org.dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.log4j</groupId> <artifactId>com.springsource.org.apache.log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> </dependencies> </project>
2. application.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" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 开启注解 --> <context:annotation-config/> <context:component-scan base-package="com.jd"/> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 加载jdbc配置文件 --> <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:/*.properties</value> </list> </property> </bean> --> <!-- 加载jdbc配置文件 --> <context:property-placeholder location="jdbc.properties"/> <!-- 定义dataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 定义jdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 定义 studentDao bean,注入jdbcTemplate--> <bean id="studentDao" class="com.jd.dao.impl.StudentDaoImpl"> <property name="template" ref="jdbcTemplate"></property> </bean> </beans>
3. StudentDao.java
package com.jd.dao; import java.util.List; public interface StudentDao<T> { public void saveStudent(); public T queryStudent(int id); public List<T> queryStudentList(); }
4. StudentDaoImpl.java
package com.jd.dao.impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import com.jd.dao.StudentDao; import com.jd.vo.Student; public class StudentDaoImpl<T> implements StudentDao<T>{ //private DataSource dataSource; private JdbcTemplate template; /*public DataSource getDataSource() { return dataSource; } *//** * 注入dataSource * @param dataSource *//* @Resource public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.template = new JdbcTemplate(dataSource); }*/ public JdbcTemplate getTemplate() { return template; } /** * 注解注入 * @param template */ @Resource public void setTemplate(JdbcTemplate template) { this.template = template; } public void saveStudent() { /*try { Connection conn = dataSource.getConnection(); Student s = new Student(); s.setId(4); s.setName("张四"); String sql = "insert into student values(?,?)"; PreparedStatement state = conn.prepareStatement(sql); state.setInt(1, s.getId()); state.setString(2, s.getName()); state.execute(); System.out.println("保存学生成功"); } catch (SQLException e) { e.printStackTrace(); }*/ Student s = new Student(); s.setId(7); s.setName("张七"); template.update("insert into student values(?,?)", new Object[]{s.getId(),s.getName()}, new int[]{java.sql.Types.INTEGER,java.sql.Types.VARCHAR}); System.out.println("保存学生成功"); } @SuppressWarnings({ "rawtypes", "unchecked" }) public T queryStudent(int id) { //回调函数 RowMapper rowMapper = new RowMapper(){ public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Student stu = new Student(); stu.setId(rs.getInt(1)); stu.setName(rs.getString(2)); return stu; }}; System.out.println("查询学生id:" + id); return (T)template.queryForObject("select * from Student where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER}, rowMapper); } @SuppressWarnings({ "unchecked", "rawtypes" }) public List<T> queryStudentList() { RowMapper rowMapper = new RowMapper(){ public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Student stu = new Student(); stu.setId(rs.getInt(1)); stu.setName(rs.getString(2)); return stu; }}; System.out.println("查询所有学生"); return (List<T>)template.query("select * from Student", null, null, rowMapper); } }
JdbcTemplate主要提供以下五类方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
query方法及queryForXXX方法:用于执行查询相关语句;
call方法:用于执行存储过程、函数相关语句。
5. 测试类
import java.util.List; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jd.dao.StudentDao; import com.jd.vo.Student; public class TestSpringJdbcTemplate extends TestCase { public void testSpringJdbc(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); StudentDao dao = (StudentDao)ctx.getBean("studentDao"); dao.saveStudent(); Student stu = (Student) dao.queryStudent(5); System.out.println("学号:" + stu.getId() + ", 姓名:" + stu.getName()); List<Student> students = dao.queryStudentList(); for(Student stu:students){ System.out.println("学号:" + stu.getId() + ", 姓名:" + stu.getName()); } } }