1,复制sping403,改名为spring403-02,StudentDaoImpl 添加继承JdbcDaoSupport,删除JdbcTemplate成员变量及set方法。
package com.cruise.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.cruise.dao.StudentDao;
import com.cruise.model.Student;
public class StudentDaoImpl extends JdbcDaoSupport implements StudentDao{
@Override
public int addStudent(Student student) {
String sql ="insert into t_student values(null,?,?)";
Object[] params = new Object[]{student.getName(),student.getAge()};
int i = this.getJdbcTemplate().update(sql,params);
return i;
}
@Override
public int updateStudent(Student student) {
String sql ="update t_student set name=? ,age=? where id=?";
Object[] params = new Object[]{student.getName(),student.getAge(),student.getId()};
int i = this.getJdbcTemplate().update(sql,params);
return i;
}
@Override
public int deleteStudent(Integer id) {
String sql ="delete from t_student where id=?";
Object[] params = new Object[]{id};
int i = this.getJdbcTemplate().update(sql,params);
return i;
}
@Override
public List<Student> findStudents() {
String sql ="select * from t_student";
final ArrayList<Student> studentList = new ArrayList<Student>();
this.getJdbcTemplate().query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
studentList.add(student);
}
});
return studentList;
}
}
2.在beans.xml中,删除JdbcTemplate的bean,修改studentDao1定义的bean。
<?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:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource1" 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="studentDao1" class="com.cruise.dao.impl.StudentDaoImpl">
<property name="dataSource" ref="dataSource1"></property>
</bean>
<bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao1"></property>
</bean>
</beans>
3,测试-运行
package com.cruise.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.model.Student;
import com.cruise.service.StudentService;
public class T {
public static void main(String[] args) {
ClassPathXmlApplicationContext CPXAC=newClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) CPXAC.getBean("studentService");
int i = studentService.addStudent(new Student("王黄",21));
if (i==1) {
System.out.println("添加成功!");
}
}
}
第九讲:9.1 spring使用JdbcDaoSupport
猜你喜欢
转载自blog.csdn.net/u010393325/article/details/83747543
今日推荐
周排行