JDBC Template - 基础篇

JDBC Template - 基础篇_第1张图片

  • execute:DDL语句,如:建表、修改表结构
  • update、batchUpdate:增删改
  • query、queryXXX:查
  • call:存储过程

JDBC Template - 基础篇_第2张图片JDBC Template - 基础篇_第3张图片JDBC Template - 基础篇_第4张图片JDBC Template - 基础篇_第5张图片
JDBC Template - 基础篇_第6张图片




    
        
        
        
        
    

    
        
    

    

案例一:

package com.imooc.sc.dao;

import com.imooc.sc.entity.Course;
import com.imooc.sc.entity.Student;
import java.util.List;

public interface CourseDao {
    
    void insert(Course course);
    void update(Course course);
    void delete(int id);
    Course select(int id);
    List selectAll();
    
}
package com.imooc.sc.dao.impl;

import com.imooc.sc.dao.CourseDao;
import com.imooc.sc.entity.Course;
import com.imooc.sc.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CourseDaoImpl implements CourseDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Course course) {
        String sql = "insert into course(name,score) values(?,?)";
        jdbcTemplate.update(sql,course.getName(),course.getScore());
    }

    public void update(Course course) {
        String sql = "update course set name=?,score=? where id=?";
        jdbcTemplate.update(sql,course.getName(),course.getScore(),course.getId());
    }

    public void delete(int id) {
        String sql = "delete from course where id=?";
        jdbcTemplate.update(sql,id);
    }

    public Course select(int id) {
        String sql = "select * from course where id=?";
        return jdbcTemplate.queryForObject(sql,new CourseRowMapper(),id);
    }

    public List selectAll() {
        String sql = "select * from course";
        return jdbcTemplate.query(sql,new CourseRowMapper());
    }

    private class CourseRowMapper implements RowMapper {

        public Course mapRow(ResultSet resultSet, int i) throws SQLException {
            Course course = new Course();
            course.setId(resultSet.getInt("id"));
            course.setName(resultSet.getString("name"));
            course.setScore(resultSet.getInt("score"));
            return course;
        }

    }

}

 案例二:

package com.imooc.sc.dao;

import com.imooc.sc.entity.Selection;
import java.util.List;
import java.util.Map;

public interface SelectionDao {

    void insert(List seles);
    void delete(int sid,int cid);
    List> selectByStudent(int sid);
    List> selectByCourse(int cid);

}
package com.imooc.sc.dao.impl;

import com.imooc.sc.dao.SelectionDao;
import com.imooc.sc.entity.Selection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SelectionDaoImpl implements SelectionDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(List seles) {
        String sql = "insert into selection values(?,?,?,?)";
        List list = new ArrayList();
        for(Selection sel:seles){
            Object[] args = new Object[4];
            args[0] = sel.getSid();
            args[1]=sel.getCid();
            args[2] = sel.getSelTime();
            args[3] =sel.getScore();
            list.add(args);
        }
        jdbcTemplate.batchUpdate(sql,list);
    }

    public void delete(int sid,int cid) {
        String sql = "delete from selection where student=? and course=?";
        jdbcTemplate.update(sql,sid,cid);
    }

    public List> selectByStudent(int sid) {
        String sql = "select se.*,stu.name sname,cou.name cname from selection se " +
                "left join student stu on se.student=stu.id " +
                "left join course cou on se.course=cou.id" +
                "where student=?";
        return jdbcTemplate.queryForList(sql,sid);
    }

    public List> selectByCourse(int cid) {
        String sql = "select se.*,stu.name sname,cou.name cname from selection se " +
                "left join student stu on se.student=stu.id " +
                "left join course cou on se.course=cou.id" +
                "where course=?";
        return jdbcTemplate.queryForList(sql,cid);
    }
}

案例三:

package com.imooc.sc.dao;

import com.imooc.sc.entity.Student;
import java.util.List;

public interface StudentDao {

    void insert(Student stu);
    void update(Student stu);
    void delete(int id);
    Student select(int id);
    List selectAll();

}
package com.imooc.sc.dao.impl;

import com.imooc.sc.dao.StudentDao;
import com.imooc.sc.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Student stu) {
        String sql = "insert into student(name,sex,born) values(?,?,?)";
        jdbcTemplate.update(sql,stu.getName(),stu.getSex(),stu.getBorn());
    }

    public void update(Student stu) {
        String sql = "update student set name=?,sex=?,born=? where id=?";
        jdbcTemplate.update(sql,stu.getName(),stu.getSex(),stu.getBorn(),stu.getId());
    }

    public void delete(int id) {
        String sql = "delete from student where id=?";
        jdbcTemplate.update(sql,id);
    }

    public Student select(int id) {
        String sql = "select * from student where id=?";
        return jdbcTemplate.queryForObject(sql,new StudentRowMapper(),id);
    }

    public List selectAll() {
        String sql = "select * from student";
        return jdbcTemplate.query(sql,new StudentRowMapper());
    }

    private class StudentRowMapper implements RowMapper {

        public Student mapRow(ResultSet resultSet, int i) throws SQLException {
            Student stu = new Student();
            stu.setId(resultSet.getInt("id"));
            stu.setName(resultSet.getString("name"));
            stu.setSex(resultSet.getString("sex"));
            stu.setBorn(resultSet.getDate("born"));
            return stu;
        }

    }

}

优点:
 - 简单、灵活:封装了 JDBC API。

缺点:
 - Java代码和SQL代码混合:使得后端不能很好的和数据库端分离开发,影响开发效率。
 - 功能不丰富:1、分页;2、关联关系。

总结:JDBC只能是相对于JDBC API开发前进了一小步,但是还不足够强大到真正的ORM关系。JDBC Template是Spring框架对JDBC操作的封装,简单、灵活但不够强大。实际应用中还需要和其它ORM框架混合使用。

你可能感兴趣的:(#,JavaWeb,#,JDBC,Template)