Spring官方有一句很是经典的宣言"Don't Reinvent theWheel",翻译过来就是"不要重复发明轮子"。java
因此咱们能够看到不少Spring出品的技术都是在原有的技术基础之上进一步封装、重构、改造,也所以成就了Spring这样一个伟大的技术生态,在Java开发领域作出了很是大的贡献。JdbcTemplate的存在无疑也是最好的见证。spring
Tips:凡是咱们看到xxxTemplate的类,都是Spring对xxx的封装的模板类。sql
咱们都知道使用原始的JDBC在操做数据库是比较麻烦的,因此Spring为了提升开发的效率,顺带着就把JDBC封装、改造了一番,而JdbcTemplate就是Spring对原始JDBC封装以后提供的一个操做数据库的工具类。数据库
咱们能够借助JdbcTemplate来完成全部数据库操做,好比:增删改查等。改造以后的JdbcTemplate主要提供如下三种类型的方法apache
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句app
public JdbcTemplate(DataSource dataSource) 建立JdbcTemplate对象,方便执行SQL语句
public void execute(final String sql) execute能够执行全部SQL语句,由于没有返回值,通常用于执行DDL语句。
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
JdbcTemplate
对象,传入Druid
链接池execute
、update
、queryXxx
等方法public class Demo04 { public static void main(String[] args) { // 建立表的SQL语句 String sql = "CREATE TABLE product(" + "pid INT PRIMARY KEY AUTO_INCREMENT," + "pname VARCHAR(20)," + "price DOUBLE" + ");"; JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource()); jdbcTemplate.execute(sql); } }
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句ide
public int update(final String sql) 用于执行`INSERT`、`UPDATE`、`DELETE`等DML语句。
1.建立JdbcTemplate对象
2.编写SQL语句
3.使用JdbcTemplate对象的update方法进行增删改工具
public class Demo05 { public static void main(String[] args) throws Exception { // test01(); // test02(); // test03(); } // JDBCTemplate添加数据 public static void test01() throws Exception { JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource()); String sql = "INSERT INTO product VALUES (NULL, ?, ?);"; jdbcTemplate.update(sql, "iPhone3GS", 3333); jdbcTemplate.update(sql, "iPhone4", 5000); jdbcTemplate.update(sql, "iPhone4S", 5001); jdbcTemplate.update(sql, "iPhone5", 5555); jdbcTemplate.update(sql, "iPhone5C", 3888); jdbcTemplate.update(sql, "iPhone5S", 5666); jdbcTemplate.update(sql, "iPhone6", 6666); jdbcTemplate.update(sql, "iPhone6S", 7000); jdbcTemplate.update(sql, "iPhone6SP", 7777); jdbcTemplate.update(sql, "iPhoneX", 8888); } // 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); } }
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句ui
public int queryForInt(String sql) 执行查询语句,返回一个int类型的值。
// queryForInt返回一个整数 public static void test01() throws Exception { // String sql = "SELECT COUNT(*) FROM product;"; String sql = "SELECT pid FROM product WHERE price=18888;"; JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource()); int forInt = jdbcTemplate.queryForInt(sql); System.out.println(forInt); }
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句翻译
public long queryForLong(String sql) 执行查询语句,返回一个long类型的数据。
// queryForLong 返回一个long类型整数 public static void test02() throws Exception { String sql = "SELECT COUNT(*) FROM product;"; // String sql = "SELECT pid FROM product WHERE price=18888;"; JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource()); long forLong = jdbcTemplate.queryForLong(sql); System.out.println(forLong); }
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句
public <T> T queryForObject(String sql, Class<T> requiredType) 执行查询语句,返回一个指定类型的数据。
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); }
public Map<String, Object> queryForMap(String sql) 执行查询语句,将一条记录放到一个Map中。
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); }
可以掌握JdbcTemplate中queryForList方法的使用
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句
public List<Map<String, Object>> queryForList(String sql) 执行查询语句,返回一个List集合,List中存放的是Map类型的数据。
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); } }
queryForList方法的做用?将返回的一条记录保存在Map集合中,多条记录对应多个Map,多个Map存储到List集合中
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句
public <T> List<T> query(String sql, RowMapper<T> rowMapper) 执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。
// 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); } }
org.springframework.jdbc.core.JdbcTemplate
类方便执行SQL语句
public <T> List<T> query(String sql, RowMapper<T> rowMapper) 执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。 public class BeanPropertyRowMapper<T> implements RowMapper<T> BeanPropertyRowMapper类实现了RowMapper接口
// 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); } }