spring中使用JdbcDaoSupport来实现增删改查

和使用jdbcTemplate比较:(可以看一下我上一篇文章spring使用jdbcTemplate实现增删改查)

StudentDaoImpl 接口

package com.java1234.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.java1234.dao.StudentDao;
import com.java1234.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()};
        return this.getJdbcTemplate().update(sql,params);
    }

    @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()};
        return this.getJdbcTemplate().update(sql,params);
    }

    @Override
    public int deleteStudent(int id) {
        String sql="delete from t_student where id=?";
        Object []params=new Object[]{id};
        return this.getJdbcTemplate().update(sql,params);
    }

    @Override
    public List<Student> findStudents() {
        String sql="select * from t_student";
        final List<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;
    }

}
 

beans.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: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">
        
    <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>
    
    <context:property-placeholder location="jdbc.properties"/>
    
    <bean id="studentDao" class="com.java1234.dao.impl.StudentDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean> 
    <bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"></property>
    </bean> 
</beans>

猜你喜欢

转载自blog.csdn.net/qq_40135955/article/details/88418959
今日推荐