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
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
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框架混合使用。