- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
Solon 3.0 引入了新的 SqlUtils 用于数据库基础操作,SqlUtils 是对 JDBC 较为原始的封装,采用了 Utils API 的风格,极为反普归真。 特性有:
SqlUtils 是一个轻量的数据库操作框架,采用 Utils API 风格,简单灵活,易于阅读和维护,支持编写复杂的SQL。对于不适合使用复杂的 ORM 框架,或者需要编写复杂的 SQL 的场景,可以使用 SqlUtils 来操作数据库.
implementation 'org.noear:solon-data-sqlutils'
<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 channel like ?", 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 的风格也更容易编写和阅读.
最后此篇关于Solon3.0新特性:SqlUtils的文章就讲到这里了,如果你想了解更多关于Solon3.0新特性:SqlUtils的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
目录 C#特性 1. 概括 2. 语法 定义特性类 应用特性
是否有关于 Python 语言必须提供哪些功能的文章/论文?为什么应该使用 Python 而不是任何其他语言? Python 的优点和缺点是什么? 最佳答案 Why Python和 Why Pytho
我想弄清楚为什么 .x比*.x具有更高的特异性当后者有望获胜时。 不是*.x应该具有 0-0-1-1 的特异性(1 个类,1 个标签)而 .x只是一个类(class) 0-0-1-0 ? 考虑以下基本
在尝试评估非 instanceof 条件时,我发现了我在 Groovy 2.4.7、1.6.0 JVM 中没有预料到的行为。 总之: class Foo { static Boolean
当使用可变结构和属性时,编译器可以解决一些问题,但不能对其他相当明显的事情做同样的事情,这对我来说似乎很奇怪。 以下面的自动属性为例: Vector2 Vector { get; set; } 而不是
我对 ES3 有一定的了解,但我对 ES5 的特性不是很了解。我 - 或多或少 - 知道: Object.create(), Object.freeze() “使用严格” getter 和 sette
我最近开始将 Django 1.5.4 用于带有 MySQL 后端的 Web 应用程序。就在一开始,我遇到了某些限制,这让我想知道 Django 是否是继续前进的正确方法。 一些明显的缺点是: 缺少复
在函数模板的定义中,模板参数的实例化一般是未知的。类型特征可用于在编译时获取一些信息。例如,这是 is_pointer 的一个简单应用: template void foo(T p) { cout
我正在设计页面的样式,该页面具有除最后一个框外的带底部边框的连续框。我为所有框应用类 .box 并添加 .box_last 以仅隐藏最后一个框的边框。 .box { border-bottom-s
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我有兴趣编制一份不建议在嵌入式系统中使用的 c++ 功能列表(这可能会导致人们推荐使用 c 而不是 c++)。如果您知道,请尝试添加原因,或者将您的原因添加到其他人的答案中。 这是一个开始(我知道的唯
作为一名 Ruby 程序员,您是否曾觉得任何使用起来有点冒险的功能,可能是因为它的奇怪行为?它可能有很好的文档记录,但在调试时很难找到,或者难以记住? 我通常尽量远离 String#gsub!。文档说
我正在开发一个连接到健身 watch 的蓝牙应用程序。这是我第一次使用蓝牙。我设法使用出色的 FlutterBlue 将我的应用程序与设备连接起来图书馆。 但是我无法理解我从阅读中获得的结果。这是我阅
1. 迭代器(Iterator)的介绍 背景:指针可以用来遍历存储空间连续的数据结构,但是对于存储空间费连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历。 定义:迭代器是一种检
嗨,伙计们,有没有动态更改 Spring 属性文件内容的好例子?如果您能给我一些示例或链接,我将不胜感激。 非常感谢 最佳答案 我想你可以使用 ReloadableResourceBundleMess
SystemVerilog 引入了一些非常有用的结构来改进编码风格。然而,正如我的一位同事经常说的,“你不是在写软件,你是在描述硬件。”考虑到这一点,当最终结果需要合成时,应该避免语言的哪些特征?这个
我定义了这些测试依赖项 / Test Dependencies lazy val wiremock = "com.github.tomakehurst" % "wir
我正在为 Android 手机和 Android watch (wearOS) 编写应用程序。这些应用程序将通过蓝牙相互通信。基本上,Android 手机上的应用程序将与 WearOS 设备绑定(bi
我正在为 Android 手机和 Android watch (wearOS) 编写应用程序。这些应用程序将通过蓝牙相互通信。基本上,Android 手机上的应用程序将与 WearOS 设备绑定(bi
我有兴趣提高我的设计能力(设计具有属性、方法等的类)。即如何决定类、方法和属性应该是什么? 你们能建议我改进这个的好 Material 吗? 最佳答案 请看: Any source of good o
我是一名优秀的程序员,十分优秀!