當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test
生活随笔
收集整理的這篇文章主要介紹了
Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 package cn.zmh.PingCe;
2
3 import org.junit.Test;
4 import org.springframework.jdbc.core.BeanPropertyRowMapper;
5 import org.springframework.jdbc.core.JdbcTemplate;
6
7 import java.util.List;
8 import java.util.Map;
9 /**
10 * Spring框架 JdbcTemplate類
11 * */
12 public class Demo {
13 //Junit單元測(cè)試,可以讓方法獨(dú)立執(zhí)行 @Test
14 // 獲取JdbcTemplate對(duì)象 連接池
15 JdbcTemplate temp = new JdbcTemplate(JdbcUtils.getDataSource());
16
17 //1. 修改1005號(hào)數(shù)據(jù)的 salary 為 10000
18 @Test
19 public void Test1(){
20 //定義sql語句
21 String sql = "update emp set salary=10000 where id=1005";
22 // 執(zhí)行sql語句
23 int i = temp.update(sql);
24 System.out.println(i);
25 }
26
27 //2. 添加一條記錄
28 @Test
29 public void test2(){
30 String sql = "insert into emp (id,ename,salary) values (1015,'碼云',200)";
31 int i = temp.update(sql);
32 System.out.println(i);
33 }
34
35 //3. 刪除最后一條的記錄
36 @Test
37 public void test3(){
38 String sql = "delete from emp where id=?";
39 int i = temp.update(sql, 1015);
40 System.out.println(i);
41 }
42
43 //4. 查詢id為1的記錄,將其封裝為Map集合
44 @Test
45 public void test4(){
46 String sql = "select * from emp where id=1001";
47 Map<String, Object> map = temp.queryForMap(sql);
48 System.out.println(map);
49 }
50
51 //5. 查詢所有記錄,將其封裝為List
52 @Test
53 public void test5(){
54 String sql = "select * from emp";
55 List<Map<String, Object>> maps = temp.queryForList(sql);
56 for(Map<String ,Object> m:maps){
57 System.out.println(m);
58 }
59 }
60
61 //6. 查詢所有記錄,將其封裝為Emp對(duì)象的List集合
62 @Test
63 public void test6(){
64 String sql = "select * from emp";
65 List<Emp> list = temp.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
66 for(Emp e:list){
67 System.out.println(e);
68 }
69 }
70
71 //7. 查詢總記錄數(shù)
72 @Test
73 public void test7(){
74 String sql = "select count(id) from emp";
75 List<Map<String, Object>> maps = temp.queryForList(sql);
76 System.out.println(maps);
77 }
78
79 //7_1. 查詢總記錄數(shù)
80 @Test
81 public void test7_1(){
82 String sql = "select count(id) from emp";
83 Long aLong = temp.queryForObject(sql, long.class);
84 System.out.println(aLong);
85 }
86 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangmenghui/p/10658574.html
總結(jié)
以上是生活随笔為你收集整理的Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在PADS中如何导出PCB封装库
- 下一篇: Pytorch基础(六)——激活函数