“使用 QueryRunner 查询表中一条或多条记录”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1eJ411c7rf?p=52”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1eJ411c7rf?p=52 | https://www.bilibili.com/video/BV1eJ411c7rf?p=52 | ||
+ | |||
+ | === BeanHandler === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | @Test | ||
+ | public void testQueryBean() { | ||
+ | Connection conn = null; | ||
+ | String sql = "select id, name, email, birth from customers where id = ?"; | ||
+ | |||
+ | /* | ||
+ | AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler, | ||
+ | BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler, | ||
+ | KeyedHandler, MapHandler, MapListHandler, ScalarHandler | ||
+ | */ | ||
+ | BeanHandler<Customer> rsh = new BeanHandler(Customer.class); | ||
+ | Customer customer = null; | ||
+ | |||
+ | try { | ||
+ | QueryRunner runner = new QueryRunner(); | ||
+ | |||
+ | conn = JDBCUtils.getConnectionFromDruid(); | ||
+ | |||
+ | customer = runner.query(conn, sql, rsh, 9); | ||
+ | |||
+ | } catch (SQLException e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | JDBCUtils.closeResource(conn, null); | ||
+ | } | ||
+ | System.out.println(customer); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === BeanListHandler === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | /** | ||
+ | * BeanListHandler:是 ResultSetHandler 接口的实现类,用于封装表中的多条记录构成的集合 | ||
+ | */ | ||
+ | @Test | ||
+ | public void testQueryBeanList() { | ||
+ | Connection conn = null; | ||
+ | String sql = "select id, name, email, birth from customers where id < ?"; | ||
+ | |||
+ | /* | ||
+ | AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler, | ||
+ | BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler, | ||
+ | KeyedHandler, MapHandler, MapListHandler, ScalarHandler | ||
+ | */ | ||
+ | BeanListHandler<Customer> rsh = new BeanListHandler(Customer.class); | ||
+ | List<Customer> list = null; | ||
+ | QueryRunner runner = new QueryRunner(); | ||
+ | |||
+ | try { | ||
+ | |||
+ | |||
+ | conn = JDBCUtils.getConnectionFromDruid(); | ||
+ | |||
+ | list = runner.query(conn, sql, rsh, 9); | ||
+ | |||
+ | } catch (SQLException e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | JDBCUtils.closeResource(conn, null); | ||
+ | } | ||
+ | |||
+ | list.forEach(System.out::println); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === MapHandler === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | /** | ||
+ | * MapHandler:是 ResultSetHandler 接口的实现类,对应表中的一条记录 | ||
+ | * 将字段及相应字段的值作为 Map 中的 key 和 value | ||
+ | */ | ||
+ | @Test | ||
+ | public void testQueryMap() { | ||
+ | Connection conn = null; | ||
+ | String sql = "select id, name, email, birth from customers where id = ?"; | ||
+ | |||
+ | /* | ||
+ | AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler, | ||
+ | BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler, | ||
+ | KeyedHandler, MapHandler, MapListHandler, ScalarHandler | ||
+ | */ | ||
+ | MapHandler rsh = new MapHandler(); | ||
+ | Map<String, Object> map = new HashMap<>(); | ||
+ | QueryRunner runner = new QueryRunner(); | ||
+ | |||
+ | try { | ||
+ | |||
+ | conn = JDBCUtils.getConnectionFromDruid(); | ||
+ | |||
+ | map = runner.query(conn, sql, rsh, 9); | ||
+ | |||
+ | } catch (SQLException e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | JDBCUtils.closeResource(conn, null); | ||
+ | } | ||
+ | |||
+ | map.forEach((k, v) -> System.out.printf("key:%s, value:%s\n", k, v)); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === MapListHandler === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | /** | ||
+ | * MapListHandler:是 ResultSetHandler 接口的实现类,对应表中的一条记录 | ||
+ | * 将字段及相应字段的值作为 Map 中的 key 和 value | ||
+ | */ | ||
+ | @Test | ||
+ | public void testQueryMapList() { | ||
+ | Connection conn = null; | ||
+ | String sql = "select id, name, email, birth from customers where id < ?"; | ||
+ | |||
+ | /* | ||
+ | AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler, | ||
+ | BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler, | ||
+ | KeyedHandler, MapHandler, MapListHandler, ScalarHandler | ||
+ | */ | ||
+ | MapListHandler rsh = new MapListHandler(); | ||
+ | List<Map<String, Object>> list = new ArrayList<>(); | ||
+ | QueryRunner runner = new QueryRunner(); | ||
+ | |||
+ | try { | ||
+ | |||
+ | conn = JDBCUtils.getConnectionFromDruid(); | ||
+ | |||
+ | list = runner.query(conn, sql, rsh, 9); | ||
+ | |||
+ | } catch (SQLException e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | JDBCUtils.closeResource(conn, null); | ||
+ | } | ||
+ | |||
+ | list.forEach(System.out::println); | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> |
2023年1月14日 (六) 06:43的版本
https://www.bilibili.com/video/BV1eJ411c7rf?p=52
BeanHandler
@Test
public void testQueryBean() {
Connection conn = null;
String sql = "select id, name, email, birth from customers where id = ?";
/*
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler,
BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler,
KeyedHandler, MapHandler, MapListHandler, ScalarHandler
*/
BeanHandler<Customer> rsh = new BeanHandler(Customer.class);
Customer customer = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnectionFromDruid();
customer = runner.query(conn, sql, rsh, 9);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
System.out.println(customer);
}
BeanListHandler
/**
* BeanListHandler:是 ResultSetHandler 接口的实现类,用于封装表中的多条记录构成的集合
*/
@Test
public void testQueryBeanList() {
Connection conn = null;
String sql = "select id, name, email, birth from customers where id < ?";
/*
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler,
BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler,
KeyedHandler, MapHandler, MapListHandler, ScalarHandler
*/
BeanListHandler<Customer> rsh = new BeanListHandler(Customer.class);
List<Customer> list = null;
QueryRunner runner = new QueryRunner();
try {
conn = JDBCUtils.getConnectionFromDruid();
list = runner.query(conn, sql, rsh, 9);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
list.forEach(System.out::println);
}
MapHandler
/**
* MapHandler:是 ResultSetHandler 接口的实现类,对应表中的一条记录
* 将字段及相应字段的值作为 Map 中的 key 和 value
*/
@Test
public void testQueryMap() {
Connection conn = null;
String sql = "select id, name, email, birth from customers where id = ?";
/*
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler,
BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler,
KeyedHandler, MapHandler, MapListHandler, ScalarHandler
*/
MapHandler rsh = new MapHandler();
Map<String, Object> map = new HashMap<>();
QueryRunner runner = new QueryRunner();
try {
conn = JDBCUtils.getConnectionFromDruid();
map = runner.query(conn, sql, rsh, 9);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
map.forEach((k, v) -> System.out.printf("key:%s, value:%s\n", k, v));
}
MapListHandler
/**
* MapListHandler:是 ResultSetHandler 接口的实现类,对应表中的一条记录
* 将字段及相应字段的值作为 Map 中的 key 和 value
*/
@Test
public void testQueryMapList() {
Connection conn = null;
String sql = "select id, name, email, birth from customers where id < ?";
/*
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler,
BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler,
KeyedHandler, MapHandler, MapListHandler, ScalarHandler
*/
MapListHandler rsh = new MapListHandler();
List<Map<String, Object>> list = new ArrayList<>();
QueryRunner runner = new QueryRunner();
try {
conn = JDBCUtils.getConnectionFromDruid();
list = runner.query(conn, sql, rsh, 9);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
list.forEach(System.out::println);
}