当前位置: 首页 > article >正文

Solon 3.0 新特性:SqlUtils

Solon 3.0 引入了新的 SqlUtils 用于数据库基础操作,SqlUtils 是对 JDBC 较为原始的封装,极为反朴归真。 特性有:

  • 支持事务管理
  • 支持多数据源
  • 支持流式输出
  • 支持批量执行
  • 支持存储过程

一、概述

SqlUtils 是一个轻量的数据库操作框架,简单灵活,易于阅读和维护,支持编写复杂的SQL。对于不适合使用复杂的 ORM 框架,或者需要编写复杂的 SQL 的场景,可以使用 SqlUtils 来操作数据库。

SqlUtils 总体上分为查询操作(query 开发)和更新操作(update 开头)。分别对应 JDBC 的 Statement:executeQuery()Statement:executeUpdate()

二、引入 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.sql("select count(*) from appx where app_id = ?", id).queryValue();
}

按照主键查数据:

public Appx findDataById(Integer id) throws SQLException {
    return sqlUtils.sql("select * from appx where app_id = ?", id)
                   .queryRow()
                   .toBean(Appx.class);
}

按照自定义查询条件查数据:

public List<Appx> findDataByGroup(Integer group_id) throws SQLException {
    return sqlUtils.sql("select * from appx where group_id = ?", group_id)
                   .queryRowList()
                   .toBeanList(Appx.class);
}

以上几种查询方式,都是一行代码就解决的。复杂的查询怎么办?比如管理后台的条件统计,可以先使用构建器:

public List<Appx> findDataStat(int group_id, String channel, int scale) throws SQLException {
    SqlBuilder sqlSpec = new SqlBuilder();
    sqlSpec.append("select group_id, sum(amount) amount from appx ")
           .append("where group_id = ? ", group_id)
           .appendIf(channel != null, "and channel like ? ", channel + "%");

    //可以分离控制
    if(scale > 10){
        sqlSpec.append("and scale = ? ", scale);
    }
    
    sqlSpec.append("group by group_id ");

    return sqlUtils.sql(sqlSpec).queryRowList().toBeanList(Appx.class);
}

管理后台常见的分页查询:

public Page<Appx> findDataPage(int group_id, String channel) throws SQLException {
    SqlBuilder sqlSpec = new SqlBuilder()
      .append("from appx  where group_id = ? ", group_id)
      .appendIf(channel != null, "and channel like ? ", channel + "%");
    
    //备份
    sqlSpec.backup();
    sqlSpec.insert("select * ");
    sqlSpec.append("limit ?,? ", 10,10); //分页获取列表
    
    //查询列表
    List<Appx> list = sqlUtils.sql(sqlSpec).queryRowList().toBeanList(Appx.class);
    
    //回滚(可以复用备份前的代码构建)
    sqlSpec.restore();
    sqlSpec.insert("select count(*) ");
    
    //查询总数
    Long total = sqlUtils.sql(sqlSpec).queryValue();
    
    return new Page(list, total);
}

构建器支持 ?... 集合占位符查询:

public List<Appx> findDataList() throws SQLException {
    SqlBuilder sqlSpec = new SqlBuilder()
        .append("select * from appx  where app_id in (?...) ", Arrays.asList(1,2,3,4));
    
    //查询列表
    List<Appx> list = sqlUtils.sql(sqlSpec).queryRowList().toBeanList(Appx.class);
}

五、流式查询操作

支持 fetchSize 参数

public void findDataAll(Integer group_id) throws SQLException {
    SqlBuilder sqlSpec = new SqlBuilder()
        .append("select * from appx where group_id = ?", group_id);
    
    try (RowIterator iterator = sqlUtils.sql(sqlSpec).queryRowIterator(100)) {
        while (iterator.hasNext()){
            Appx app = iterator.next().toBean(Appx.class);
            //....
        }
    }
}

六、插入操作

单条插入:

public void addData(int id) throws SQLException {
    return sqlUtils.sql("insert appx(app_id) values(?)", id).update();
}

单条插入并返回主键:

public Long addData(int id) throws SQLException {
    return sqlUtils.sql("insert appx(app_id) values(?)", id).updateReturnKey();
}

批量插入:

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.sql("insert appx(app_id) values(?)").updateBatch(argsList);
}

六、更新操作(更新或删除)

支持事务控制

@Tran
public void updateData(int id, Appx app) throws SQLException {
    SqlBuilder sqlSpec = new SqlBuilder();
    sqlSpec.append("update appx set ")
    sqlSpec.append("  group_id=?, ", app.group_id)
    sqlSpec.append("  name=?, ", app.name)
    sqlSpec.append("  channel=?, ", app.channel)
    sqlSpec.append("  amount=? ", app.amount)
    sqlSpec.append("where app_id=? ", id)
    
    sqlUtils.sql(sqlSpec).queryRow();
}

@Tran
public void delData(int id) throws SQLException {
    sqlUtils.sql("delete from appx where app_id=?", id).update(); 
}

七、存储过程操作

查询操作

public Appx findDataById(int id) throws SQLException {
    return sqlUtils.sql("{call findDataById(?)}", id).queryRow().toBean(Appx.class);
}

删除操作

public int findDataById(int id) throws SQLException {
    return sqlUtils.sql("{call delDataById(?)}", id).update();
}

八、总结

通过上述的示例,可以看到基本的数据库操作都可以用 SqlUtils 实现,可以避免了复杂的ORM框架的使用。


http://www.kler.cn/news/343196.html

相关文章:

  • LeetCode-871 最低加油次数
  • 注册安全分析报告:惠农网
  • 【一文理解】conda install pip install 区别
  • sql 语句相关的函数
  • 深入解析 Go 语言中的结构体:从基础用法到高级技巧的全方位指南
  • requests 中data=xxx、json=xxx、params=xxx 分别什么时候用
  • 做ppt用什么软件好?5个办公必备的ppt工具推荐!
  • VS Code最新版本Retome远程ssh不兼容旧服务器问题
  • 在Windows中使用谷歌浏览器观看和录制游戏直播
  • 【C#生态园】打造现代化跨平台应用:深度解析.NET桌面应用工具
  • c# gobal using
  • rdagent框架代码拆解:自动化因子挖掘
  • Java基础-Wrapper Classes(包装类)
  • Vulnhub靶场案例渗透[6]- DC6
  • JAVA基础面试题汇总(持续更新)
  • C++11 开发中的 Atomic 原子操作
  • react 封装防抖
  • 【优选算法】--- 分治 快速排序
  • 如何快速给word文件加拼音?请跟着步骤完成吧
  • centos7更新yum国内源