简介:Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
步骤:
1. 导入jar包
2. 创建JdbcTemplate对象。依赖于数据源DataSource
* JdbcTemplate template = new JdbcTemplate(ds);
3. 调用JdbcTemplate的方法来完成CRUD的操作
* update():执行DML语句。增、删、改语句
* queryForMap():查询结果将结果集封装为map集合,将列名作为key,将值作为value 将这条记录封装为一个map集合
* 注意:这个方法查询的结果集长度只能是1
* queryForList():查询结果将结果集封装为list集合
* 注意:将每一条记录封装为一个Map集合,再将Map集合装载到List集合中
* query():查询结果,将结果封装为JavaBean对象
* query的参数:RowMapper
* 一般我们使用BeanPropertyRowMapper实现类。可以完成数据到JavaBean的自动封装
* new BeanPropertyRowMapper<类型>(类型.class)
* queryForObject:查询结果,将结果封装为对象
* 一般用于聚合函数的查询
//简单例子
import JDBCUtils.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TempleteDQLDemo {
/**
* 查询id=1002 封装为Map集合
* 用到的JDBCUTiles.getDataSource()为Druid工具类
* 详见:https://blog.csdn.net/m0_52440148/article/details/112185786
*/
@Test
public void test01(){
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from emp where id = ?";
Map<String,Object> emp = template.queryForMap(sql,1002);
System.out.println(emp);
}
/**
* 查询emp表,包装为List集合,输出所有结果
*/
@Test
public void test02(){
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from emp";
List<Map<String,Object>> list = template.queryForList(sql);
for(Map<String,Object> emp : list){
System.out.println(emp);
}
}
/**
* 查询emp表,包装为Emp包装类,输出所有结果
*/
@Test
public void test03(){
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from emp";
List<Emp> list = template.query(sql,new BeanPropertyRowMapper<Emp>(Emp.class));
for(Emp emp :list){
System.out.println(emp);
}
}
}
//Emp 包装类
import java.util.Date;
public class Emp {
private Integer id;
private String ename;
private Integer job_id;
private Integer mgr;
private Date joindate;
private Double salary;
private Double bonus;
private Integer dept_id;
//为防止因null数据出现异常,应定义为基本类型对应的包装类
public Emp() {
}
public Emp(Integer id, String ename, Integer job_id, Integer mgr, Date joindate, Double salary, Double bonus, Integer dept_id) {
this.id = id;
this.ename = ename;
this.job_id = job_id;
this.mgr = mgr;
this.joindate = joindate;
this.salary = salary;
this.bonus = bonus;
this.dept_id = dept_id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Integer getJob_id() {
return job_id;
}
public void setJob_id(Integer job_id) {
this.job_id = job_id;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getJoindate() {
return joindate;
}
public void setJoindate(Date joindate) {
this.joindate = joindate;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Double getBonus() {
return bonus;
}
public void setBonus(Double bonus) {
this.bonus = bonus;
}
public Integer getDept_id() {
return dept_id;
}
public void setDept_id(Integer dept_id) {
this.dept_id = dept_id;
}
@Override
public String toString() {
return "emp{" +
"id=" + id +
", ename='" + ename + '\'' +
", job_id=" + job_id +
", mgr=" + mgr +
", joindate=" + joindate +
", salary=" + salary +
", bonus=" + bonus +
", dept_id=" + dept_id +
'}';
}
}