package cn.zmh.PingCe;

 import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate; import java.util.List;
import java.util.Map;
/**
* Spring框架 JdbcTemplate类
* */
public class Demo {
//Junit单元测试,可以让方法独立执行 @Test
// 获取JdbcTemplate对象 连接池
JdbcTemplate temp = new JdbcTemplate(JdbcUtils.getDataSource()); //1. 修改1005号数据的 salary 为 10000
@Test
public void Test1(){
//定义sql语句
String sql = "update emp set salary=10000 where id=1005";
// 执行sql语句
int i = temp.update(sql);
System.out.println(i);
} //2. 添加一条记录
@Test
public void test2(){
String sql = "insert into emp (id,ename,salary) values (1015,'码云',200)";
int i = temp.update(sql);
System.out.println(i);
} //3. 删除最后一条的记录
@Test
public void test3(){
String sql = "delete from emp where id=?";
int i = temp.update(sql, 1015);
System.out.println(i);
} //4. 查询id为1的记录,将其封装为Map集合
@Test
public void test4(){
String sql = "select * from emp where id=1001";
Map<String, Object> map = temp.queryForMap(sql);
System.out.println(map);
} //5. 查询所有记录,将其封装为List
@Test
public void test5(){
String sql = "select * from emp";
List<Map<String, Object>> maps = temp.queryForList(sql);
for(Map<String ,Object> m:maps){
System.out.println(m);
}
} //6. 查询所有记录,将其封装为Emp对象的List集合
@Test
public void test6(){
String sql = "select * from emp";
List<Emp> list = temp.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
for(Emp e:list){
System.out.println(e);
}
} //7. 查询总记录数
@Test
public void test7(){
String sql = "select count(id) from emp";
List<Map<String, Object>> maps = temp.queryForList(sql);
System.out.println(maps);
} //7_1. 查询总记录数
@Test
public void test7_1(){
String sql = "select count(id) from emp";
Long aLong = temp.queryForObject(sql, long.class);
System.out.println(aLong);
}
}

最新文章

  1. eclipse — 导入android项目后识别成java项目的问题及解决
  2. Sass学习笔记
  3. shell脚本入门及基本元素
  4. 使用 SVG 制作单选和多选框动画【附源码】
  5. Python基础9- 字典
  6. 2015GitWebRTC编译实录10
  7. php---实现保留小数点后两位
  8. [原]Linux系统管理使用技巧总结
  9. STM32F0xx_TIM输出PWM配置详细过程
  10. 百度ueditor编辑器背景不显示问题
  11. php中传值与传引用的区别。什么时候传值什么时候传引用?
  12. sql 判断表、列、视图等是否存在
  13. Pandoc将markdown转换为word
  14. restful架构风格设计准则(六)版本管理
  15. js匿名函数,闭包
  16. static(静态)关键字
  17. 依赖、耦合、解耦、控制反转(IOC)、依赖注入(DI)
  18. 实战fortran77基础语法
  19. 趣味编程:FizzBuzz(Swift版)
  20. 设置DatagridView的列头样式

热门文章

  1. 使用xcode workspace 多个project协同工作
  2. luogu P2574 XOR的艺术 (线段树)
  3. php登录加密加盐
  4. 如何用纯 CSS 创作在文本前后穿梭的边框
  5. Kali入门配置使用(一)
  6. verilog behavioral modeling--branch statement
  7. 剑指offer重构二叉树 给出二叉树的前序和后序重构二叉树
  8. Verilog学习笔记基本语法篇(三)&#183;&#183;&#183;&#183;&#183;&#183;&#183;&#183;&#183;赋值语句(待补充)
  9. Python基础之字符编码,文件操作流与函数
  10. MySQL容量规划之tcpcopy应用之道