Solon 3.0 新特性:SqlUtils
Solon 3.0 引入了新的 SqlUtils 用于数据库基础操作,SqlUtils 是对 JDBC 较为原始的封装,采用了 Utils API 的风格,极为反普归真。 特性有:
- 支持事务管理
- 支持多数据源
- 支持流式输出
- 支持批量执行
- 支持存储过程
一、概述
SqlUtils 是一个轻量的数据库操作框架,采用 Utils API 风格,简单灵活,易于阅读和维护,支持编写复杂的SQL。对于不适合使用复杂的 ORM 框架,或者需要编写复杂的 SQL 的场景,可以使用 SqlUtils 来操作数据库。
二、引入 SqlUtils
- gradle 依赖
implementation 'org.noear:solon-data-sqlutils'
- maven 依赖
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-data-sqlutils</artifactId>
</dependency>
三、配置数据源
配置数据源(具体参考:《数据源的配置与构建》)
solon.dataSources:
rock!:
class: "com.zaxxer.hikari.HikariDataSource"
jdbcUrl: jdbc:mysql://localhost:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: 123456
之后就可以按数据源名注入 SqlUtils 了(带 ! 结尾的数据源名,为默认)
@Component
public class DemoService {
@Inject //默认数据源名
SqlUtils sqlUtils;
}
四、查询操作
查数量:
public Long findCount() throws SQLException {
return sqlUtils.selectValue("select count(*) from appx where app_id = ?", id);
}
按照主键查数据:
public Appx findDataById(Integer id) throws SQLException {
return sqlUtils.selectRow("select * from appx where app_id = ?", id)
.toBean(Appx.class);
}
按照自定义查询条件查数据:
public List<Appx> findDataByGroup(Integer group_id) throws SQLException {
return sqlUtils.selectRowList("select * from appx where group_id = ?", group_id)
.toBeanList(Appx.class);
}
以上几种查询方式,查询条件中的变量使用的是占位符(SqlUtils 只支持占位符),也比较简单。复杂的查询怎么办?比如管理后台的条件统计,可以先使用构建器:
public List<Appx> findDataStat(int group_id, String channel, int scale) throws SQLException {
SqlBuilder builder = new SqlBuilder();
builder.append("select group_id, sum(amount) amount from appx ")
.append("where group_id = ? group by group_id", group_id);
builder.appendIf(channel != null, " and title channel ?", channel + "%");
if(scale > 10){
builder.append(" and scale = ?", scale);
}
return sqlUtils.selectRowList(builder.getSql(), builder.getArgs())
.toBeanList(Appx.class);
}
管理后台常见的分页查询:
public void findDataPage(int group_id, String channel) throws SQLException {
SqlBuilder builder = new SqlBuilder()
.append(" from appx where group_id = ?", group_id)
.appendIf(channel != null, " and title channel ?", channel + "%");
//备份
builder.backup();
builder.insert("select *");
builder.append(" limit ?,?", 10,10); //分页获取列表
//查询列表
List<Appx> list = sqlUtils.selectRowList(builder.getSql(), builder.getArgs())
.toBeanList(Appx.class);
//回滚(可以复用备份前的代码构建)
builder.restore();
builder.insert("select count(*)");
//查询总数
Long total = sqlUtils.selectValue(builder.getSql(), builder.getArgs());
}
五、流式查询操作
支持 fetchSize 参数
public void findDataAll(Integer group_id) throws SQLException {
try (RowIterator iterator = sqlUtils.selectRowIterator("select * from appx where group_id = ?", 100, group_id)) {
while (iterator.hasNext()){
Appx app = iterator.next().toBean(Appx.class);
//....
}
}
}
六、插入操作
单条插入:
public void addData(int id) throws SQLException {
return sqlUtils.insert("insert appx(app_id) values(?)", id);
}
单条插入并返回Key:
public void addData(int id) throws SQLException {
return sqlUtils.insertReturnKey("insert appx(app_id) values(?)", id);
}
批量插入:
public void addDataBatch() throws SQLException {
List<Object[]> argsList = new ArrayList<>();
argsList.add(new Object[]{1});
argsList.add(new Object[]{2});
argsList.add(new Object[]{3});
argsList.add(new Object[]{4});
argsList.add(new Object[]{5});
sqlUtils.executeBatch("insert appx(app_id) values(?)", argsList);
}
六、执行操作(更新或删除)
支持事务控制
@Tran
public void delData(int id) throws SQLException {
sqlUtils.execute("delete from appx where app_id=?", id);
}
@Tran
public void updateData(int id) throws SQLException {
sqlUtils.execute("update appx set group_id=? where app_id=?", 2, id);
}
七、存储过程操作
查询操作
public Appx findDataById(int id) throws SQLException {
return sqlUtils.selectRow("{call findDataById(?)}", id).toBean(Appx.class);
}
删除操作
public int findDataById(int id) throws SQLException {
return sqlUtils.execute("{call delDataById(?)}", id);
}
八、总结
通过上述的示例,可以看到基本的数据库操作都可以用 SqlUtils 实现,避免了复杂的ORM框架的使用,切操作要比ORM框架简单灵活的多。Utils API 的风格也更容易编写和阅读。
Solon 3.0 新特性:SqlUtils的更多相关文章
- 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?
来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...
- Java基础和JDK5.0新特性
Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...
- Visual Studio 2015速递(1)——C#6.0新特性怎么用
系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...
- atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性
atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...
- 背水一战 Windows 10 (1) - C# 6.0 新特性
[源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...
- C# 7.0 新特性2: 本地方法
本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- C# 7.0 新特性1: 基于Tuple的“多”返回值方法
本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...
- C# 7.0 新特性3: 模式匹配
本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...
- C# 7.0 新特性4: 返回引用
本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- C#发展历程以及C#6.0新特性
一.C#发展历程 下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的. 二.C#6.0新特性 1.字符串插值 (String In ...
随机推荐
- windows server dhcp与AD域
创建两台windows server 2016 同一个网络适配器 windows1 配置window1手动网络 安装域 设置密码下一步下一步 重启 完成域安装后创建用户 配置windows1 dhcp ...
- onnxruntime无法使用GPU加速 加速失败 解决方法【非常详细】
onnx 无法使用GPU加速 加速失败 解决方法[非常详细] 应该是自目前以来最详细的加速失败解决方法GPU加速,收集了各方的资料.引用资料见后文 硬件配置: GPU CUDA版本:12.2 客户架构 ...
- AI开源是否应该完全开源?AI的完全开源是否可以实现?
看了一个视频: 袁进辉:零代码改动,加速AIGC 里面提到了一个完全开源的概念,感觉有些意思,虽然觉得可实现性不高,嘿嘿嘿!!! AI的完全开源: 训练数据开源.数据清洗过程开源.模型权重开源.项目代 ...
- 编程语言mojo报错:error: cannot call function that may raise in a context that cannot raise
代码: from python import Python fn main(): # fn main() raises: # This is equivalent to Python's `impor ...
- 推荐一款界面优雅、功能强大的 .NET + Vue 权限管理系统
前言 今天推荐一款用 .NET 和 Vue3 实现的开源权限管理系统.它的界面清爽干净,功能强大,还具备灵活的角色权限分配功能,能够满足不同规模企业的管理需求.无论你是开发新手还是大神,都能轻松上手, ...
- 后端开发学习敏捷需求-->产品价值的定位
产品价值的定位 为什么要写这一系列文章 2023年网上报名学习了,敏捷软件需求的培训课程 ,一直都没有进行回顾,回顾学习,总结 业务分析的能力偏弱,学习和了解关于业务需求相关的方法和理论 每一年都有一 ...
- JavaWeb中的Tomcat,Servlet详解
JavaWeb JavaWeb技术主要包括服务器技术(后端),如Tomcat,Servlet,JSP等待,以及客户端技术(前端)如HTML,CSS,JavaScript等等 Web服务器 Web服务器 ...
- Python 潮流周刊#64:Python 的函数调用还很慢么?(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- 推荐一个优秀的 .NET MAUI 组件库
前言 .NET MAUI 的发布,项目中可以使用这个新的跨平台 UI 框架来轻松搭建的移动和桌面应用. 为了帮助大家更快地构建美观且功能丰富的应用,本文将推荐一款优秀的 .NET MAUI 组件库MD ...
- rcc of stm32
1. G0 2. F0 / F1 / F3 F0 F1 F3 3. F2/F4 F205 f429 f7