【JDBC】 JDBCTemplate的使用

基本介绍

JdbcTemplate是Spring MVC内置的对JDBC的一个封装,其目的是使JDBC更加易于使用 ,JdbcTemplate是Spring的一部分。

JdbcTemplate处理了资源的建立和释放。他帮助我们避免一些常见的错误,比如忘了总要关闭连接。他运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。

JdbcTemplate使用步骤

1.准备DruidDataSource连接池

2.导入依赖的jar包

  • spring-beans-4.1.2.RELEASE.jar
  • spring-core-4.1.2.RELEASE.jar
  • spring-jdbc-4.1.2.RELEASE.jar
  • spring-tx-4.1.2.RELEASE.jar
  • com.springsource.org.apache.commons.logging-1.1.1.jar

3.依赖于数据源DataSource,创建JdbcTemplate象,传入Druid连接池

4.调用JdbcTemplate的方法来完成CRUD的操作

  • update(): 执行DML语句。增删改语句
  • queryForMap():查询结果将结果集封装为map集合
  • queryForList():查询结果将结果封装为list集合
  • query():查询结果,将结果封装为JavaBean对象
  • queryForObject():查询结果将结果封装为对象

实例1-入门

// JdbcTemplate入门
public static void test() throws Exception {
  
   //定义SQL语句
   String sql = update pid set price=18888 WHERE id=?;";
   //创建JdbcTemplate对象
   JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
   //调用方法
   int count = jdbcTemplate.update(sql,1);
   System.out.println("影响的行数: " + count);
}

实例2-JdbcTemplate实现增删改

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

	
	// JDBCTemplate添加数据
	public static void test01() throws Exception {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
		
		String sql = "INSERT INTO product VALUES (NULL, ?, ?);";
		
		jdbcTemplate.update(sql, "iPhone4", 5000);
		jdbcTemplate.update(sql, "iPhone4S", 5001);

	}
	
	// JDBCTemplate修改数据
	public static void test02() throws Exception {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
		
		String sql = "UPDATE product SET pname=?, price=? WHERE pid=?;";
		
		int i = jdbcTemplate.update(sql, "XVIII", 18888, 10);
		System.out.println("影响的行数: " + i);
	}

	// JDBCTemplate删除数据
	public static void test03() throws Exception {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
		String sql = "DELETE FROM product WHERE pid=?;";
		int i = jdbcTemplate.update(sql, 7);
		System.out.println("影响的行数: " + i);
	}
}

实例3-queryForInt返回一个int整数

public int queryForInt(String sql)
执行查询语句,返回一个int类型的值。
    // queryForInt返回一个整数
public static void test03() throws Exception {

   String sql = "SELECT pid FROM product WHERE price=18888;";
   JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
   int forInt = jdbcTemplate.queryForInt(sql);
   System.out.println(forInt);
}

实例4-queryForObject返回String

public <T> T queryForObject(String sql, Class<T> requiredType)
执行查询语句,返回一个指定类型的数据。
public static void test04() 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);
}

实例5-RowMapper返回自定义对象

public <T> List<T> query(String sql, RowMapper<T> rowMapper)
执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。
    // query使用rowMap做映射返回一个对象

public static void test05() 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);
   }
}

实例6-BeanPropertyRowMapper返回自定义对象

根据源码:

public class BeanPropertyRowMapper<T> implements RowMapper<T>

BeanPropertyRowMapper类实现了RowMapper接口

// query使用BeanPropertyRowMapper做映射返回对象

public static void test06() throws Exception {
	JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());

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

	for (Product product : list) {
		System.out.println(product);
	}
}
发布了169 篇原创文章 · 获赞 101 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/zzu_seu/article/details/104994086