查看“使用 QueryRunner 查询表中一条或多条记录”的源代码
←
使用 QueryRunner 查询表中一条或多条记录
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
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><syntaxhighlight lang="console"> Customer{id=9, name='蔡徐坤', email='caixukun@126.com', birth=1997-09-08} </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><syntaxhighlight lang="console"> Customer{id=2, name='李四', email='lisi@126.com', birth=2023-01-09} Customer{id=3, name='章子怡', email='zzy@gmail.com', birth=2001-02-02} Customer{id=4, name='刘攀', email='liupan@126.com', birth=1993-09-08} Customer{id=5, name='张三丰', email='zhangsanfeng@126.com', birth=1992-09-08} Customer{id=6, name='测试图片大于1M', email='xxxx@126.com', birth=1992-09-08} Customer{id=7, name='测试图片大于1M', email='xxxx@126.com', birth=1992-09-08} </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><syntaxhighlight lang="console"> key:name, value:蔡徐坤 key:birth, value:1997-09-08 key:id, value:9 key:email, value:caixukun@126.com </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><syntaxhighlight lang="console"> {name=李四, birth=2023-01-09, id=2, email=lisi@126.com} {name=章子怡, birth=2001-02-02, id=3, email=zzy@gmail.com} {name=刘攀, birth=1993-09-08, id=4, email=liupan@126.com} {name=张三丰, birth=1992-09-08, id=5, email=zhangsanfeng@126.com} {name=测试图片大于1M, birth=1992-09-08, id=6, email=xxxx@126.com} {name=测试图片大于1M, birth=1992-09-08, id=7, email=xxxx@126.com} </syntaxhighlight>https://github.com/jihch/jdbc_2/blob/main/src/main/java/io/github/jihch/dbutils/QueryRunnerTest.java
返回至
使用 QueryRunner 查询表中一条或多条记录
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息