几年前写的一个JDBC工具,一直没时间整理出来,断断续续整理了一下,具有特性:
数组SQL参数支持。
分页支持(Oracle,SQLServer,MySQL等),可自定义分页逻辑。
数据库连接共享。
批量数据操作。
轻量,仅依赖slf4j。
为什么要有这玩意,行业内已经有了Spring JDBC,Commons DbUtils等,整体来说实现思路相似,算是重新发明轮子,
但是如果您想尝尝鲜,可以尝试下。
源代码:
GitHub https://github.com/chyxion/newbie-jdbc
GitOSC http://git.oschina.net/chyxion/newbie-jdbc
使用方式:
Maven依赖:
<dependency> <groupId>me.chyxion</groupId> <artifactId>newbie-jdbc</artifactId> <version>0.0.1-RELEASE</version> </dependency>
使用样例:
// init datasource, here use DruidDataSource as demo DruidDataSource datasource = null; datasource = new DruidDataSource(); datasource.setUrl("jdbc:mysql://127.0.0.1/demo"); datasource.setUsername("root"); datasource.setPassword("password"); datasource.init(); // create NewbieJdbc object NewbieJdbc jdbc = new NewbieJdbcSupport(datasource);
Basic Query
// count of users int count = jdbc.findValue( "select count(1) from users"); // find name of user id is 2008110101 String name = jdbc.findValue( "select name from users where id = ?", "2008110101"); // find names of user id is 101 or 102 // 0. array as params List<String> names = jdbc.listValue( "select name from users where id in (?)", "101", "102"); // 1. collection as params names = jdbc.listValue( "select name from users where id in (?)", Arrays.asList("101", "102")); // 2. map as params Map<String, Object> params = new HashMap<String, Object>(); params.put("id", Arrays.asList("101", "102")); // or: // params.put("id", new String[] {"101", "102"}); names = jdbc.listValue( "select name from users where id in (:id)", params); // find user of id is 101 Map<String, Object> mapUser = jdbc.findMap( "select id, name, gender from users where id = ?", "101"); // list users of age is 24 List<Map<String, Object>> listUsers = jdbc.listMap( "select id, name, gender from users where age = ?", 24);
Advance Query
// find id and name as a string array String[] idAndName = jdbc.findOne(new Ro<String[]>() { public String[] exec(ResultSet rs) throws SQLException { return new String[] { rs.getString("id"), rs.getString("name")}; } }, "select id, name from users where id = ?", "101"); // find names of gender is M String names = jdbc.list(new Ro<String>() { public String exec(ResultSet rs) throws SQLException { return rs.getString("name"); } }, "select name from users where gender = ?", "M"); // find name of user id is 101, same as findValue String name = jdbc.query(new Ro<String>() { public String exec(ResultSet rs) throws SQLException { return rs.next() ? rs.getString(1) : null; } }, "select name from users where id = ?", "101"); // list users of gender F offset 10 limit 16 List<Map<String, Object>> users = jdbc.listMapPage( "select * from users where gender = ?", Arrays.asList(new Order("date_created", Order.DESC)), 10, 16, "F");
Insert And Update
// insert one Map<String, Object> mapUser = new HashMap<String, Object>(); mapUser.put("id", "103"); mapUser.put("name", "Shaun Chyxion"); mapUser.put("gender", "M"); mapUser.put("date_created", new Date()); jdbc.insert("users", mapUser); // insert batch Collection<Collection<?>> users = Arrays.<Collection<?>>asList( Arrays.<Object>asList("104", "Xuir", "F", new Date()), Arrays.<Object>asList("105", "Sorina Nyco", "F", new Date()), Arrays.<Object>asList("106", "Gemily", "F", new Date()), Arrays.<Object>asList("107", "Luffy", "M", new Date()), Arrays.<Object>asList("108", "Zoro", "M", new Date()), Arrays.<Object>asList("109", "Bruck", "M", new Date())); jdbc.insert("users", Arrays.asList("id", "name", "gender", "date_created"), args, 3); // update gender to F of user 102 jdbc.update("update users set gender = ? where id = ?", "F", "102"); Reusble Connection And Transaction // find user of id is 101 and books uses same connection Map<String, Object> mapUserWithBooks = jdbc.execute(new Co<Map<String, Object>>() { @Override protected Map<String, Object> run() throws SQLException { String userId = "101"; Map<String, Object> mapRtn = findMap( "select * from users where id = ?", userId); mapRtn.put("books", listMap("select * from books where user_id = ?", userId)); return mapRtn; } }); // execute transaction Map<String, Object> mapUser = jdbc.executeTransaction(new Co<Map<String, Object>>() { @Override protected Map<String, Object> run() throws SQLException { update("delete users where id = ?", "104"); update("update users set age = ? where id = ?", 24, "103"); return findMap("select * from users where id = ?", 106); } });
Execute SQL
// create table users
jdbc.execute( "create table users (" + "id varchar(36) not null, " + "name varchar(36) not null, " + "primary key (id))");
Customize Newbie JDBC
// create table users CustomResolver customResolver = new CustomResolver() { // set StringBuilder as String public void setParam(PreparedStatement ps, int index, Object param) throws SQLException { if (param instanceof StringBuilder) { ps.setString(index, param.toString()); } else { ps.setObject(index, param); } } // read CLOB as String public Object readValue(ResultSet rs, int index) throws SQLException { Object valueRtn = null; if (Types.CLOB == rs.getMetaData().getColumnType(index)) { valueRtn = rs.getClob(index).toString(); } else { valueRtn = rs.getObject(index); } return valueRtn; } // use MySQLCompatiblePaginationProcessor to paginate public PaginationProcessor getPaginationProcessor( Connection conn) { return new MySQLCompatiblePaginationProcessor(); } }; jdbc = new NewbieJdbcSupport(dataSource, customResolver);
使用 Junit 测试类编写 public class JdbcTest { private Connection con = null;// 创建一个数据库连接 private PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement private ResultSet result = null;
https://blog.csdn.net/newbie_907486852/article/details/81391525 springboot2.0配置多数据源: spring.datasource.primary.url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=true
jdbcUrl is required with driverClassName springboot2.0配置多数据源: spring.datasource.primary.url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datas
我正在Eclipse Neon中使用Hibernate工具(JBoss tools 4.4.0.Final)。现在,我想将数据库表反向工程为POJO对象和Hibernate映射文件。 我遵循了一些关于如何设置Eclipse来生成POJO对象的教程。在我运行配置之前,一切看起来都很好。什么都没发生,也没有抛出错误。有人能帮我吗?数据库是一个微软SQL服务器2014。 我的逆向工程配置文件看起来像:
龙虎牛熊多头合约池 接口名称 long_pool 接口描述 龙虎牛熊多头合约池接口 请求参数 参数名 说明 举例 date 查询日期 2018-08-08 返回参数 参数名 类型 说明 symbol string 品种编码 code string 合约代号 示例代码 from akshare import pro_api pro = pro_api(token="在此处输入您的token,可以通过
工具 客户端 客户端分为三种:完整客户端、轻量级客户端和在线客户端。 完整客户端:存储所有的交易历史记录,功能完备; 轻量级客户端:不保存交易副本,交易需要向别人查询; 在线客户端:通过网页模式来浏览第三方服务器提供的服务。 钱包 矿机 专门为“挖矿”设计的硬件,包括基于 GPU 和 ASIC 的芯片。 脚本 比特币交易支持一种比较简单的脚本语言(类 Forth 的栈脚本语言),可以写入 UTXO
工具 以下的一些工具可以帮助你自动检查项目中的 Ruby 代码是否符合这份指南。 RuboCop [RuboCop][] 是一个基于本指南的 Ruby 代码风格检查工具。RuboCop 涵盖了本指南相当大的部分,其同时支持 MRI 1.9 和 MRI 2.0,且与 Emacs 整合良好。 RubyMine RubyMine 的代码检查部分基于本指南。
10.7. 工具 本章剩下的部分将讨论Go语言工具箱的具体功能,包括如何下载、格式化、构建、测试和安装Go语言编写的程序。 Go语言的工具箱集合了一系列的功能的命令集。它可以看作是一个包管理器(类似于Linux中的apt和rpm工具),用于包的查询、计算包的依赖关系、从远程版本控制系统下载它们等任务。它也是一个构建系统,计算文件的依赖关系,然后调用编译器、汇编器和链接器构建程序,虽然它故意被设计成
vse命令行工具 yocode扩展生成器 范例
提供各种支付需要的配置生成方法。 配置 <?php use EasyWeChat\Pay\Application; $config = [...]; $app = new Application($config); $utils = $app->getUtils(); 注意 生成支付 JS 配置 有四种发起支付的方式:WeixinJSBridge, JSSDK, 小程序支付, APP We
CoreOS 内置了 服务发现,容器管理 工具。 服务发现 CoreOS 的第一个重要组件就是使用 etcd 来实现的服务发现。在 CoreOS 中 etcd 默认以 rkt 容器方式运行。 etcd 使用方法请查看 etcd 章节。 容器管理 第二个组件就是 Docker,它用来运行你的代码和应用。CoreOS 内置 Docker,具体使用请参考本书其他章节。