一、JdbcTemplate
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.2:3306/test?useUnicode=true&characterEncoding=GBK jdbc.username=root jdbc.password=root
lesson4.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Scans within the base package of the application for @Components to configure as beans --> <context:component-scan base-package="bupt.lesson4" /> <context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <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> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
package bupt.lesson4; import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Repository; @Repository("student") public class Student { private int id; private String name; private String age; public Student(){ } public Student(int id, String name, String age) { this.id = this.id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
package bupt.lesson4; import java.util.List; public interface StudentDao { void insert(Student s); Student query(int id); List<Student> queryList(); void deleteAll(); void delete(int id); }
package bupt.lesson4; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; @Repository public class StudentDaoImpl implements StudentDao { private JdbcTemplate jdbcTemplate; @Autowired public void init(@Qualifier("dataSource") DataSource dataSource){ this.jdbcTemplate = new JdbcTemplate(dataSource); } @Override public void insert(Student s) { // TODO Auto-generated method stub String sql = "insert into student values (?,?,?)"; jdbcTemplate.update(sql, new Object[]{s.getId(),s.getName(),s.getAge()}); } @Override public Student query(int id) { // TODO Auto-generated method stub String sql = "select id,name,age from student where id = ?"; return (Student) jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper(){ @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub System.out.println("rowNum=" + rowNum); Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getString("age")); return student; }}); } @Override public void delete(int id) { // TODO Auto-generated method stub String sql = "delete from student where id = ?"; jdbcTemplate.update(sql, new Object[]{id}); } @Override public void deleteAll() { // TODO Auto-generated method stub String sql = "delete from student"; jdbcTemplate.execute(sql); } @Override public List<Student> queryList() { // TODO Auto-generated method stub String sql = "select id,name,age from student"; List<Student> list = jdbcTemplate.query(sql, new RowMapper(){ @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub System.out.println("rowNum=" + rowNum); Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getString("age")); return student; }}); return list; } }
package test.bupt.lesson4; import java.util.Iterator; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import bupt.lesson4.Student; import bupt.lesson4.StudentDao; import junit.framework.TestCase; public class testStudentDaoImpl extends TestCase { public void testInsert() { ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson4.xml"); StudentDao sDao = (StudentDao) ctx.getBean("studentDaoImpl"); Student s = new Student(); s.setId(1); s.setName("abc"); s.setAge("26"); sDao.insert(s); } public void testQuery(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson4.xml"); StudentDao sDao = (StudentDao) ctx.getBean("studentDaoImpl"); Student s = sDao.query(1); System.out.println("name=" + s.getName()); } public void testQueryList(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson4.xml"); StudentDao sDao = (StudentDao) ctx.getBean("studentDaoImpl"); List<Student> list = sDao.queryList(); Iterator iterator = list.iterator(); while(iterator.hasNext()){ Student s = (Student) iterator.next(); System.out.println("name=" + s.getName()); } } public void testDelete(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson4.xml"); StudentDao sDao = (StudentDao) ctx.getBean("studentDaoImpl"); sDao.delete(1); System.out.println("delete..."); } public void testDeleteAll(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson4.xml"); StudentDao sDao = (StudentDao) ctx.getBean("studentDaoImpl"); sDao.deleteAll(); System.out.println("delete..."); } }