Spring JdbcTemplate实例

时间:2022-03-15 04:41:58

1、常用方法

* update():执行DML语句。增、删、改语句
* queryForMap():查询结果将结果集封装为map集合,将列名作为key,将值作为value 将这条记录封装为一个map集合
         * 注意:这个方法查询的结果集长度只能是1
* queryForList():查询结果将结果集封装为list集合
         * 注意:将每一条记录封装为一个Map集合,再将Map集合装载到List集合中
  * query():查询结果,将结果封装为JavaBean对象
         * query的参数:RowMapper
           * 一般我们使用BeanPropertyRowMapper实现类。可以完成数据到JavaBean的自动封装
            * new BeanPropertyRowMapper<类型>(类型.class)
 * queryForObject:查询结果,将结果封装为对象
                * 一般用于聚合函数的查询

2、示例代码

 1 package com.alphajuns.jdbcTest;
 2 
 3 import com.alphajuns.pojo.User;
 4 import com.alphajuns.utils.JDBCUtils;
 5 import org.junit.Test;
 6 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 7 import org.springframework.jdbc.core.JdbcTemplate;
 8 import org.springframework.jdbc.core.RowMapper;
 9 
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.util.List;
13 import java.util.Map;
14 
15 /**
16  * @ClassName JDBCTest
17  * @Description
18  * @Author AlphaJunS
19  * @Date 2019/10/30 22:19
20  * @Version V1.0
21  **/
22 public class JDBCTest {
23 
24     private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
25 
26     @Test
27     public void update1() {
28         String sql = "update user set username = ? where id = ?";
29         int count = jdbcTemplate.update(sql, "zhangsanfeng", 3);
30         System.out.println(count);
31     }
32 
33     @Test
34     public void update2() {
35         String sql = "insert user (id, username, password) VALUE (?, ?, ?)";
36         int count = jdbcTemplate.update(sql, 6, "zhaoliu", "654321");
37         System.out.println(count);
38     }
39 
40     @Test
41     public void query1() {
42         String sql = "select * from user";
43         List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
44         for (Map<String, Object> stringObjectMap : mapList) {
45             System.out.println(stringObjectMap);
46         }
47     }
48 
49     @Test
50     public void query2() {
51         String sql = "select * from user where id = ?";
52         Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql, 4);
53         System.out.println(stringObjectMap);
54     }
55 
56     @Test
57     public void query3() {
58         String sql = "select * from user";
59         List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() {
60             @Override
61             public User mapRow(ResultSet resultSet, int i) throws SQLException {
62                 User user = new User();
63                 user.setId(resultSet.getInt("id"));
64                 user.setUsername(resultSet.getString(1));
65                 user.setPassword(resultSet.getString("password"));
66                 return user;
67             }
68         });
69         System.out.println(userList);
70     }
71 
72     @Test
73     public void query4() {
74         String sql = "select * from user";
75         List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
76         System.out.println(userList);
77     }
78 
79 }