0
点赞
收藏
分享

微信扫一扫

JDBC Template - 基础篇


JDBC Template - 基础篇_基础篇

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

JDBC Template - 基础篇_Spring_02JDBC Template - 基础篇_JDBC Template_03JDBC Template - 基础篇_基础篇_04JDBC Template - 基础篇_JDBC Template_05

JDBC Template - 基础篇_Spring_06

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/selection_course?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

<context:component-scan base-package="com.imooc.sc"/>

</beans>

案例一:

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<Course> 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<Course> selectAll() {
String sql = "select * from course";
return jdbcTemplate.query(sql,new CourseRowMapper());
}

private class CourseRowMapper implements RowMapper<Course> {

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<Selection> seles);
void delete(int sid,int cid);
List<Map<String,Object>> selectByStudent(int sid);
List<Map<String,Object>> 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<Selection> seles) {
String sql = "insert into selection values(?,?,?,?)";
List<Object[]> list = new ArrayList<Object[]>();
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<Map<String, Object>> 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<Map<String, Object>> 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<Student> 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<Student> selectAll() {
String sql = "select * from student";
return jdbcTemplate.query(sql,new StudentRowMapper());
}

private class StudentRowMapper implements RowMapper<Student> {

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



举报

相关推荐

0 条评论