如何快速上手Easy-Query:5分钟完成第一个数据库查询

如何快速上手Easy-Query:5分钟完成第一个数据库查询 如何快速上手Easy-Query5分钟完成第一个数据库查询【免费下载链接】easy-queryjava/kotlin high performance lightweight solution for jdbc query,support oltp and olap query,一款java下面支持强类型、轻量级、高性能的ORM,致力于解决jdbc查询,拥有对象模型筛选、隐式子查询、隐式join项目地址: https://gitcode.com/gh_mirrors/ea/easy-query想要在Java项目中快速实现数据库操作但又不想被繁琐的SQL语句和复杂的ORM配置困扰 Easy-Query正是你需要的解决方案这是一款专为Java/Kotlin开发者设计的高性能、轻量级ORM框架支持强类型查询和隐式关联让你在5分钟内就能完成第一个数据库查询操作。本文将为你提供完整的Easy-Query快速入门指南帮助你轻松上手这个强大的数据库查询工具。 Easy-Query是什么Easy-Query是一款Java/Kotlin高性能轻量级JDBC查询解决方案它支持OLTP和OLAP查询致力于解决传统JDBC查询的痛点。通过强大的类型安全查询API和隐式关联特性Easy-Query让数据库操作变得简单直观同时保持极高的性能表现。核心优势速览✅强类型查询编译时检查避免运行时错误✅隐式关联自动处理表关联无需手动写JOIN✅高性能轻量级设计查询效率极高✅多数据库支持MySQL、PostgreSQL、Oracle等主流数据库✅简洁API链式调用代码可读性强 快速安装与配置1. 添加Maven依赖在你的Spring Boot项目中只需添加以下依赖即可开始使用Easy-Queryproperties easy-query.version最新版本/easy-query.version /properties dependency groupIdcom.easy-query/groupId artifactIdsql-springboot-starter/artifactId version${easy-query.version}/version /dependency dependency groupIdcom.easy-query/groupId artifactIdsql-mysql/artifactId version${easy-query.version}/version /dependency2. 配置数据源在application.yml中配置数据库连接spring: datasource: url: jdbc:mysql://localhost:3306/your_database username: root password: your_password driver-class-name: com.mysql.cj.jdbc.Driver‍♂️ 5分钟完成第一个查询步骤1定义实体类首先创建一个简单的博客实体类Data Table(t_blog) EntityProxy public class BlogEntity { Column(primaryKey true) private String id; private String title; private String content; private Integer star; private LocalDateTime createTime; private Boolean deleted; }步骤2编写第一个查询现在让我们用Easy-Query实现一个简单的查询Service public class BlogService { Autowired private EasyEntityQuery easyEntityQuery; public ListBlogEntity findPopularBlogs() { return easyEntityQuery.queryable(BlogEntity.class) .where(blog - blog.star().gt(100)) .orderByDesc(blog - blog.createTime()) .limit(10) .toList(); } }步骤3查看生成的SQL上面的代码会自动生成以下SQL语句SELECT t.id, t.title, t.content, t.star, t.create_time FROM t_blog t WHERE t.deleted false AND t.star 100 ORDER BY t.create_time DESC LIMIT 10看就是这么简单 你已经完成了第一个Easy-Query查询操作。 五大隐式特性详解Easy-Query最强大的功能之一就是它的隐式特性让复杂查询变得异常简单1. 隐式JOIN查询无需手动写JOIN语句自动处理表关联// 查询属于XX公司的用户 ListSysUser users entityQuery.queryable(SysUser.class) .where(user - user.company().name().like(XX公司)) .orderBy(user - user.company().registerMoney().desc()) .toList();2. 隐式子查询自动处理一对多关系的子查询// 查询包含小明用户的企业 ListCompany companies entityQuery.queryable(Company.class) .where(company - company.users().any(u - u.name().like(小明))) .toList();3. 隐式分组查询优化多个子查询合并为高效的分组查询ListCompany companies entityQuery.queryable(Company.class) .subQueryToGroupJoin(company - company.users()) .where(company - { company.users().any(u - u.name().like(小明)); company.users().max(u - u.birthday()).gt(LocalDateTime.now()); }).toList(); 完整CRUD操作示例查询操作// 单表查询 Topic topic easyEntityQuery.queryable(Topic.class) .where(o - o.id().eq(3)) .firstOrNull(); // 多表关联查询 Topic topic entityQuery.queryable(Topic.class) .leftJoin(BlogEntity.class, (t, b) - t.id().eq(b.id())) .where(o - o.id().eq(3)) .firstOrNull(); // 分页查询 EasyPageResultBlogEntity page easyEntityQuery.queryable(BlogEntity.class) .where(b - b.content().like(技术)) .orderByDesc(b - b.createTime()) .toPageResult(1, 20);插入操作Topic topic new Topic(); topic.setId(1); topic.setStars(100); topic.setTitle(Easy-Query入门); topic.setCreateTime(LocalDateTime.now()); long rows easyEntityQuery.insertable(topic).executeRows();更新操作// 实体更新 Topic topic easyEntityQuery.queryable(Topic.class) .where(o - o.id().eq(7)) .firstNotNull(未找到数据); topic.setTitle(更新后的标题); long rows easyEntityQuery.updatable(topic).executeRows(); // 表达式更新 long rows easyEntityQuery.updatable(Topic.class) .setColumns(o - o.stars().set(12)) .where(o - o.id().eq(2)) .executeRows();删除操作// 条件删除 long rows easyQuery.deletable(Topic.class) .where(o - o.title().eq(标题998)) .executeRows(); // 实体删除 Topic topic easyQuery.queryable(Topic.class) .whereId(997) .firstNotNull(未找到数据); long rows easyQuery.deletable(topic).executeRows();️ 高级功能一览动态表名支持easyEntityQuery.queryable(BlogEntity.class) .asTable(a - aa_bb_cc) // 动态指定表名 .where(o - o.id().eq(123)) .toList();UNION查询QueryableTopic q1 easyQuery.queryable(Topic.class); QueryableTopic q2 easyQuery.queryable(Topic.class); QueryableTopic q3 easyQuery.queryable(Topic.class); ListTopic list q1.union(q2, q3) .where(o - o.id().eq(123321)) .toList();子查询支持// IN子查询 EntityQueryableStringProxy, String idQuery easyEntityQuery.queryable(BlogEntity.class) .where(o - o.id().eq(1)) .select(o - new StringProxy(o.id())); ListTopic list easyEntityQuery.queryable(Topic.class) .where(o - o.id().in(idQuery)) .toList(); // EXISTS子查询 ListTopic list easyEntityQuery.queryable(Topic.class) .where(o - o.exists(() - easyEntityQuery.queryable(BlogEntity.class) .where(q - q.id().eq(o.id())) )).toList();️ 支持的数据库Easy-Query支持多种主流数据库让你的项目迁移无忧数据库包名Spring Boot配置MySQLsql-mysqlmysqlPostgreSQLsql-pgsqlpgsqlOraclesql-oracleoracleSQL Serversql-mssqlmssqlH2sql-h2h2SQLitesql-sqlitesqliteClickHousesql-clickhouseclickhouse达梦数据库sql-damengdameng人大金仓sql-kingbase-eskingbase_es 最佳实践建议1. 使用实体代理为实体类添加EntityProxy注解让Easy-Query自动生成代理类获得更好的类型安全和IDE支持。2. 合理使用隐式特性充分利用五大隐式特性隐式JOIN、隐式子查询等减少手动编写复杂SQL的工作量。3. 分页优化对于大数据量的分页查询建议使用toPageResult方法它会自动优化分页查询性能。4. 逻辑删除Easy-Query内置逻辑删除支持只需在实体字段上添加LogicDelete注解即可。LogicDelete(strategy LogicDeleteStrategyEnum.BOOLEAN) private Boolean deleted; 常见问题解答Q: Easy-Query适合什么场景A: Easy-Query特别适合需要复杂查询、多表关联、分库分表的Java/Kotlin项目。它既适合中小型项目快速开发也适合大型企业级应用。Q: 性能如何A: Easy-Query采用轻量级设计生成的SQL语句经过优化性能接近原生JDBC同时提供了类型安全和便捷的API。Q: 学习曲线陡峭吗A: 不陡峭如果你熟悉Lambda表达式和链式调用基本上30分钟就能掌握基本用法。本文的5分钟入门就是一个很好的证明Q: 支持事务吗A: 支持Easy-Query完全兼容Spring的事务管理可以与其他框架无缝集成。 进阶学习路径基础掌握单表CRUD → 多表关联 → 复杂查询高级特性隐式关联 → 分库分表 → 读写分离性能优化查询优化 → 索引建议 → 缓存策略源码学习代理机制 → SQL生成 → 扩展开发 开始你的Easy-Query之旅现在你已经掌握了Easy-Query的核心概念和基本用法。只需5分钟你就能在自己的项目中完成第一个数据库查询✨记住Easy-Query的设计哲学是简单不简单——让简单的操作保持简单让复杂的操作变得可能。无论你是刚接触ORM的新手还是经验丰富的开发者Easy-Query都能为你提供极佳的开发体验。立即行动克隆项目仓库开始体验吧git clone https://gitcode.com/gh_mirrors/ea/easy-query开始享受类型安全、高性能的数据库操作体验吧 如果你在学习和使用过程中遇到任何问题欢迎查阅官方文档或加入社区讨论。提示更多详细配置和高级用法请参考项目中的sql-springboot-starter模块和官方文档。【免费下载链接】easy-queryjava/kotlin high performance lightweight solution for jdbc query,support oltp and olap query,一款java下面支持强类型、轻量级、高性能的ORM,致力于解决jdbc查询,拥有对象模型筛选、隐式子查询、隐式join项目地址: https://gitcode.com/gh_mirrors/ea/easy-query创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考