SpringJDBC操作技术总结——简单版

通过SpringIOC技术, 实现数据库的增删改查

1.SpringJDBC

@Transactional
public class StudentDAOImpl implements StudentDAO {

    /**
     * 依赖注入
     * Spring 提供的 jdbc模板,操作数据表CRUD
     */
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //DAO实现层的操作代码
    @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
    @Override
    public int saveStudent(Student student) {
        //DBUtil   
        int count = this.jdbcTemplate.update(
                "insert into T_STUDENT (STUNAME) VALUES (?)",
                new Object[]{student.getStuName()});
        return count;
    }


    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    @Override
    public Student getStudentById(Integer id) {

        return (Student) this.jdbcTemplate.queryForObject(
                "select * from T_STUDENT where stuid = ?",
                new Object[]{id},
                new StudentRowMapper());


    }
    //处理器
    class StudentRowMapper implements RowMapper{
        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            Student student = new Student();

            //把Student结果集里面的数据复制给   Student对象
            student.setStuId(rs.getInt("stuid"));
            student.setStuName(rs.getString("stuname"));

            return student;
        }


    }

}

 

2.Spring提供jdbc模板配置 (本次使用的是Oracle数据源)

    
    
           
     

 

3.配置dbcp数据源 模板配置

    
    
        
        
        
        
        
    


    
    
        
        
        
        
        
    	

 

4.依赖注入给DAOImpl实现层

    
    
        
    

你可能感兴趣的:(杨小白的java框架学习,Spring)