JdbcTemplate基本使用(非原创)

 

增删改

API介绍

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

  1. public int update(final String sql)
    用于执行`INSERT`、`UPDATE`、`DELETE`等DML语句。

 

 

queryForInt

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

API介绍

public int queryForInt(String sql)
执行查询语句,返回一个int类型的值。

 

queryForLong

讲解

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

API介绍

public long queryForLong(String sql)
执行查询语句,返回一个long类型的数据。

 

queryForObject

讲解

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

API介绍

public <T> T queryForObject(String sql, Class<T> requiredType)
执行查询语句,返回一个指定类型的数据。
12

使用步骤

  1. 创建JdbcTemplate对象

  2. 编写查询的SQL语句

  3. 使用JdbcTemplate对象的queryForObject方法,并传入需要返回的数据的类型

  4. 输出结果

案例代码

public static void test03() throws Exception {
  String sql = "SELECT pname FROM product WHERE price=7777;";
  JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
  String str = jdbcTemplate.queryForObject(sql, String.class);
  System.out.println(str);
}

 

 

queryForMap

API介绍

public Map<String, Object> queryForMap(String sql)
执行查询语句,将一条记录放到一个Map中。
12

使用步骤

  1. 创建JdbcTemplate对象

  2. 编写查询的SQL语句

  3. 使用JdbcTemplate对象的queryForMap方法

  4. 处理结果

public static void test04() throws Exception {
  String sql = "SELECT * FROM product WHERE pid=?;";
  JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
  Map<String, Object> map = jdbcTemplate.queryForMap(sql, 6);
  System.out.println(map);
}

 

 

queryForList

目标

能够掌握JdbcTemplate中queryForList方法的使用

讲解

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

API介绍

public List<Map<String, Object>> queryForList(String sql)
执行查询语句,返回一个List集合,List中存放的是Map类型的数据。
12

使用步骤

  1. 创建JdbcTemplate对象

  2. 编写查询的SQL语句

  3. 使用JdbcTemplate对象的queryForList方法

  4. 处理结果

public static void test05() throws Exception {
  String sql = "SELECT * FROM product WHERE pid<?;";
  JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
  List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, 8);
  for (Map<String, Object> map : list) {
     System.out.println(map);
  }
}
12345678

queryForList方法的作用?将返回的一条记录保存在Map集合中,多条记录对应多个Map,多个Map存储到List集合中

 

RowMapper返回自定义对象

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

API介绍

public <T> List<T> query(String sql, RowMapper<T> rowMapper)
执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。
12

使用步骤

  1. 定义Product类

  2. 创建JdbcTemplate对象

  3. 编写查询的SQL语句

  4. 使用JdbcTemplate对象的query方法,并传入RowMapper匿名内部类

  5. 在匿名内部类中将结果集中的一行记录转成一个Product对象

案例代码

// query使用rowMap做映射返回一个对象
public static void test06() throws Exception {
   JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());

  // 查询数据的SQL语句
  String sql = "SELECT * FROM product;";

  List<Product> query = jdbcTemplate.query(sql, new RowMapper<Product>() {
     @Override
     public Product mapRow(ResultSet arg0, int arg1) throws SQLException {
        Product p = new Product();
        p.setPid(arg0.getInt("pid"));
        p.setPname(arg0.getString("pname"));
        p.setPrice(arg0.getDouble("price"));
        return p;
    }
  });

  for (Product product : query) {
     System.out.println(product);
  }
}
12345678910111213141516171819202122
  1. 使用JdbcTemplate对象的query方法,并传入RowMapper匿名内部类

  2. 在匿名内部类中将结果集中的一行记录转成一个Product对象

 

 

BeanPropertyRowMapper返回自定义对象

org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

使用BeanPropertyRowMapper自动绑定,需要确保数据库列表名称和Java实体类属性名称相同,如实体类的userName映射数据库的user_name。

API介绍

public <T> List<T> query(String sql, RowMapper<T> rowMapper)
执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。
12
public class BeanPropertyRowMapper<T> implements RowMapper<T>
BeanPropertyRowMapper类实现了RowMapper接口
12

使用步骤

  1. 定义Product类

  2. 创建JdbcTemplate对象

  3. 编写查询的SQL语句

  4. 使用JdbcTemplate对象的query方法,并传入BeanPropertyRowMapper对象

// query使用BeanPropertyRowMapper做映射返回对象
public static void test07() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());

// 查询数据的SQL语句
String sql = "SELECT * FROM product;";
List<Product> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));

for (Product product : list) {
System.out.println(product);
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2020-08-27 14:08  东方1024  阅读(255)  评论(0)    收藏  举报

导航