zoukankan      html  css  js  c++  java
  • Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test

     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单元测试,可以让方法独立执行   @Test
    14     // 获取JdbcTemplate对象    连接池
    15     JdbcTemplate temp = new JdbcTemplate(JdbcUtils.getDataSource());
    16 
    17     //1. 修改1005号数据的 salary 为 10000
    18     @Test
    19     public void Test1(){
    20         //定义sql语句
    21         String sql = "update emp set salary=10000 where id=1005";
    22         // 执行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对象的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. 查询总记录数
    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. 查询总记录数
    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 }
  • 相关阅读:
    c#泛型的使用
    如何调试由于heap corruption导致的程序崩溃的简单示例
    Windows的SEH机理简要介绍
    利用定制行为扩展WCF之利用MessageInsepctor behaviourExtension扩展WCF行为(自定义消息头)
    欧拉函数
    JZOJ.1349 最小公约数
    关于扩展中国剩余定理(excrt)的证明与拙见
    【USACO 2021 US Open, Gold】United Cows of Farmer John & JZOJ7220
    线性求逆元
    【USACO 2021 January Contest, Platinum】Problem 1. Sum of Distances JZOJ.7241
  • 原文地址:https://www.cnblogs.com/zhangmenghui/p/10658574.html
Copyright © 2011-2022 走看看