??本文介紹在幾種典型情境中如何使用 Spring 5 JDBC 進(jìn)行查詢操作嚷节。
??在使用 Spring 提供的 JDBC 操作前要進(jìn)行一些相關(guān)的配置啊犬,比如配置連接池剖效、連接數(shù)據(jù)庫等嫉入,此處不做介紹焰盗,可以參考網(wǎng)上的相關(guān)教程。另外咒林,本文假設(shè)讀者已經(jīng)對 NamedParameterJdbcTemplate 和 JdbcTemplate 這兩個類有基本了解熬拒。
??示例代碼使用的數(shù)據(jù)庫是 MySQL,用到的表 orders 的結(jié)構(gòu)如下:
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| order_num | int(11) | NO | PRI | NULL | auto_increment |
| order_date | datetime | NO | | NULL | |
| cust_id | int(11) | NO | MUL | NULL | |
+------------+----------+------+-----+---------+----------------+
??示例代碼中所有的查詢都將結(jié)果包裝成為一個對象:
//將查詢結(jié)果包裝為對象
public class Order {
private int order_num;
private int cust_id;
private Timestamp order_date;
public int getOrder_num() {
return order_num;
}
public void setOrder_num(int order_num) {
this.order_num = order_num;
}
public int getCust_id() {
return cust_id;
}
public void setCust_id(int cust_id) {
this.cust_id = cust_id;
}
public Timestamp getOrder_date() {
return order_date;
}
public void setOrder_date(Timestamp order_date) {
this.order_date = order_date;
}
@Override
public String toString() {
return "{order_num:" + order_num + ",order_date:" + order_date + ",cust_id:" + cust_id + "}";
}
}
查詢條件中具有不同個數(shù)的參數(shù)
參數(shù)只有一個
??查詢指定客戶的訂單(假設(shè)該客戶只有一條訂單記錄):
//根據(jù)客戶 id垫竞,查詢該客戶的訂單
public Order getOrder(int order_num) {
//使用 NamedParameterJdbcTemplate
NamedParameterJdbcTemplate namedTempl = new NamedParameterJdbcTemplate(dataSource);
String sql = "SELECT order_num,order_date,cust_id FROM orders WHERE order_num = :order_num";
Map<String,Integer> param = new HashMap<>();
//將參數(shù)封裝在 Map 中
param.put("order_num", order_num);
return namedTempl.queryForObject(sql, param, BeanPropertyRowMapper.newInstance(Order.class));
}
參數(shù)不止一個
??查詢?nèi)舾蓚€時間點(diǎn)上的訂單記錄:
public List<Order> getOrder(Date date1, Date date2) {
//使用 JdbcTemplate
JdbcTemplate templ = new JdbcTemplate(dataSource);
String sql = "SELECT order_num,order_date,cust_id FROM orders "
+ "WHERE Date(order_date) IN (?,?)";
//將參數(shù)放在 Object 數(shù)組中
Object[] params = {start,end};
return templ.query(sql, params, BeanPropertyRowMapper.newInstance(Order.class));
}
??這種情況也可以使用 NamedParameterJdbcTemplate澎粟,但是需要多次調(diào)用 put 方法,這就不如直接把參數(shù)放入數(shù)組中方便欢瞪。
查詢結(jié)果返回的記錄數(shù)目不同
結(jié)果只有一條記錄
??查詢指定客戶在指定日期的訂單:
public Order getOrder(int cust_id, Date order_date) {
JdbcTemplate templ = new JdbcTemplate(dataSource);
String sql = "SELECT order_num,order_date,cust_id FROM orders "
+ "WHERE cust_id = ? AND Date(order_date) = ?";
//將參數(shù)放在 Object 數(shù)組中
Object[] params = {cust_id,order_date};
//使用 queryForObject 方法
return templ.queryForObject(sql, params, BeanPropertyRowMapper.newInstance(Order.class));
}
結(jié)果不只一條記錄
??查詢?nèi)舾蓚€指定客戶的訂單:
public List<Order> getOrder(int cust_id1, int cust_id2) {
JdbcTemplate templ = new JdbcTemplate(dataSource);
String sql = "SELECT order_num,order_date,cust_id FROM orders "
+ "WHERE cust_id IN (cust_id1, int cust_id2)";
//將參數(shù)放在 Object 數(shù)組中
Object[] params = {cust_id1,cust_id2};
//使用 query 方法
return templ.query(sql, params, BeanPropertyRowMapper.newInstance(Order.class));
}