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);
}
}