hyorm框架进阶用法

简介: hyorm框架的原生使用方法,源于php laravel框架的orm层实现方案,基于单Query的设计方式,确保在java多线程中的线程安全性。

使用简介

项目地址

文档合集和示例地址

使用方法:引入maven

<dependency>
        <groupId>com.hyutils</groupId>
        <artifactId>core</artifactId>
        <version>1.0-SNAPSHOT</version>
</dependency>

不带条件简单查询

simpleGet方法

查询单个对象,如果有多个,只返回第一个

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.find("value").find("id").find("thesaurus_code").size(1).simpleGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words  limit 1 ;

listMapGet方法

查询全部对象

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.find("value").find("id").find("thesaurus_code").size(2).listMapGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words  limit 2 ;

结果为:

[{value=xxxxxxxxx, id=585730307940941826, thesaurus_code=23}, {value=18&xxxxx, id=585730307940941827, thesaurus_code=23}]

find方法

指查询某个字段

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.find("value").find("id").find("thesaurus_code").size(1).simpleGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words  limit 1 ;

page方法和size方法

多少页,页的大小为多少,page方法需要和size一起使用,但是size方法可以单独使用

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.find("value").find("id").find("thesaurus_code").page(1).size(1).simpleGet());
    }

生成的sql为

SELECT value,id,thesaurus_code FROM material_thesaurus_words  offset 0  limit 1 ;

orderBy方法

按条件排序的方法

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .orderBy("thesaurus_code","desc").orderBy("value","desc")
                .page(1).size(2)
                .listMapGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words order by thesaurus_code desc,value desc  offset 0  limit 2 ;

带条件查询

where方法

该方法的入参是WhereSyntaxTree类,该类有两个继承类,分别是AndWhereSyntaxTreeOrWhereSyntaxTree,这两种不同的where树代表了两种不同的语义。

以下的内容均来源于上面两个语义类

andWheres方法

AndWhereSyntaxTree语义,即内部的是用and连接起来的操作

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .orderBy("thesaurus_code","desc").orderBy("value","desc")
                .page(1).size(2)
                .andWheres(new HashMap<String, Object>(){
   
                    {
   
                        put("thesaurus_code","14");
                        put("value","demo123");
                    }
                })
                .listMapGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words WHERE value = :value AND thesaurus_code = :thesaurus_code order by thesaurus_code desc,value desc  offset 0  limit 2 ;

参数为:

{value=demo123, thesaurus_code=14}

orwhere方法

OrWhereSyntaxTree语义,即内部使用or连接

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .orderBy("thesaurus_code","desc").orderBy("value","desc")
                .page(1).size(2)
                .orWheres(new HashMap<String, Object>(){
   
                    {
   
                        put("thesaurus_code","14");
                        put("value","demo123");
                    }
                })
                .listMapGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words WHERE value = :value OR thesaurus_code = :thesaurus_code order by thesaurus_code desc,value desc  offset 0  limit 2 ;

多个相同的参数or/and起来

由于hashmap本身是去重的,导致传入的时候无法做一个没有去重的参数,所以这里引入了andWheres和orWheres的重载方法,传入一个三元组。除了在这里可用之外,三元组还有别的用法,这里先介绍重复参数的用法。

orwheres传入三元组

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .page(1).size(2)
                .orWheres(new ArrayList<Triplet<String, String, Object>>(){
   
                    {
   
                        add(new Triplet<>("thesaurus_code","=","99"));
                        add(new Triplet<>("thesaurus_code","=","14"));
                    }
                })
                .listMapGet());
    }

生成的sql为

SELECT value,id,thesaurus_code FROM material_thesaurus_words WHERE thesaurus_code = :thesaurus_code OR thesaurus_code = :a231e89bab64bed3388a6f9d0745be11  offset 0  limit 2 ;

参数为:

{thesaurus_code=99, 96000698b09c6b1afa517dd677fb90f5=14}

andwheres传入三元组

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .page(1).size(2)
                .andWheres(new ArrayList<Triplet<String, String, Object>>(){
   
                    {
   
                        add(new Triplet<>("thesaurus_code","=","99"));
                        add(new Triplet<>("thesaurus_code","=","14"));
                    }
                })
                .listMapGet());
    }

生成的sql为:

SELECT value,id,thesaurus_code FROM material_thesaurus_words WHERE thesaurus_code = :thesaurus_code AND thesaurus_code = :5363ad8bf3fa098218eb28a15b2f3872  offset 0  limit 2 ;

参数为:

{5363ad8bf3fa098218eb28a15b2f3872=14, thesaurus_code=99}

组合使用

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .page(1).size(2)
                .orWheres(new ArrayList<Triplet<String, String, Object>>(){
   
                    {
   
                        add(new Triplet<>("thesaurus_code","=","99"));
                        add(new Triplet<>("thesaurus_code","=","14"));
                    }
                })
                .andWheres(new ArrayList<Triplet<String, String, Object>>(){
   
                    {
   
                        add(new Triplet<>("thesaurus_code","=","99"));
                        add(new Triplet<>("thesaurus_code","=","14"));
                    }
                })
                .listMapGet());
    }

生成的sql为

SELECT value, id, thesaurus_code
FROM material_thesaurus_words
WHERE thesaurus_code = :thesaurus_code
   OR thesaurus_code = :f6c7a5c9ac4f6fb2f77539b6b31c8f14 AND
      (thesaurus_code = :f65a9d7f4cafa174ce97dfb216321307 AND thesaurus_code = :27d714567083ce319446329df70ea1ae)
offset 0 limit 2;

参数为:

{f6c7a5c9ac4f6fb2f77539b6b31c8f14=14, 27d714567083ce319446329df70ea1ae=14, f65a9d7f4cafa174ce97dfb216321307=99, thesaurus_code=99}

多级参数

前面介绍的参数均出现在单层,所以在进行or操作的时候,如果再和别的条件进行and,可以发现其实生成的sql是有问题的。那么此时就需要引入多级参数了。

多级参数中,三元组的第三个参数可以传入一个WhereSyntaxTree对象,则这个对象作为二级参数存在。来看一个例子

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        WhereSyntaxTree whereSyntaxTree = new WhereSyntaxTree();
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .page(1).size(2)
                .andWheres(new ArrayList<Triplet<String, String, Object>>(){
   
                    {
   
                        add(new Triplet<>("thesaurus_code","=", whereSyntaxTree.createOrTreeByOperate(new ArrayList<Triplet<String, String, Object>>(){
   
                            {
   
                                add(new Triplet<>("thesaurus_code","=","99"));
                                add(new Triplet<>("thesaurus_code","=","14"));
                            }
                        })));
                        add(new Triplet<>("这个随便写","=",whereSyntaxTree.createAndTreeByOperate(new ArrayList<Triplet<String, String, Object>>(){
   
                            {
   
                                add(new Triplet<>("value","=","22"));
                                add(new Triplet<>("value","=","11"));
                            }
                        })));
                        add(new Triplet<>("deleted_mark","=",false));
                    }
                })
                .listMapGet());
    }

生成的sql为:

SELECT value, id, thesaurus_code
FROM material_thesaurus_words
WHERE (thesaurus_code = :thesaurus_code OR thesaurus_code = :824abb38c3759e0a61a01f434237d6ed)
  AND (value = :value AND value = :c621bd6517d712bda0a05c313111ac67)
  AND deleted_mark = :deleted_mark
offset 0 limit 2;

参数为

{824abb38c3759e0a61a01f434237d6ed=14, deleted_mark=false, c621bd6517d712bda0a05c313111ac67=11, value=22, thesaurus_code=99}

可以看见,这次or语句就被括号包裹起来了。

条件不是等号的查询

利用三元组

三元组法上面已经介绍了,这里简单提供一些使用例子

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        WhereSyntaxTree whereSyntaxTree = new WhereSyntaxTree();
        System.out.println(wordsQuery
                .find("value").find("id").find("thesaurus_code")
                .page(1).size(2)
                .andWheres(new ArrayList<Triplet<String, String, Object>>(){
   
                    {
   
                        add(new Triplet<>("thesaurus_code","=", whereSyntaxTree.createOrTreeByOperate(new ArrayList<Triplet<String, String, Object>>(){
   
                            {
   
                                add(new Triplet<>("thesaurus_code","like","99%"));
                                add(new Triplet<>("thesaurus_code","like","14%"));
                            }
                        })));
                        add(new Triplet<>("这个随便写","=",whereSyntaxTree.createAndTreeByOperate(new ArrayList<Triplet<String, String, Object>>(){
   
                            {
   
                                add(new Triplet<>("value","!=","22"));
                                add(new Triplet<>("value","!=","11"));
                            }
                        })));
                        add(new Triplet<>("id",">",1L));
                        add(new Triplet<>("deleted_mark","=",false));
                    }
                })
                .listMapGet());
    }

生成的sql为:

SELECT value, id, thesaurus_code
FROM material_thesaurus_words
WHERE (thesaurus_code like :thesaurus_code OR thesaurus_code like :2cdde9107318c2af77faa6bc8d62df95)
  AND (value != :value AND value != :5408b2f7440d44234993b8ae44ae3860)
  AND id > :id
  AND deleted_mark = :deleted_mark
offset 0 limit 2;

除了like,数据库支持的其他简单操作也都能支持,只不过有一些特殊的函数需要手动编写SQL。下面简单介绍下编写SQL时,该框架的使用方法。

直接执行

获取列表

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.totalSql("select thesaurus_code,count(id) from material_thesaurus_words group by thesaurus_code;").listMapGet());
}

输出结果为:

[{thesaurus_code=19, count=3772}, {thesaurus_code=23, count=6226}, {thesaurus_code=99, count=2}, {thesaurus_code=57, count=411}, {thesaurus_code=97, count=3353}, {thesaurus_code=12, count=2}, {thesaurus_code=27, count=3427}, {thesaurus_code=24, count=6}, {thesaurus_code=30, count=229}, {thesaurus_code=16, count=289}]

获取一个

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.totalSql("select thesaurus_code,count(id) from material_thesaurus_words group by thesaurus_code order by count desc limit 1;").simpleGet());
}

执行的结果为:

{
   thesaurus_code=23, count=6226}

带参数

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        System.out.println(wordsQuery.totalSql("select thesaurus_code,count(id) from material_thesaurus_words where deleted_mark = :deleted_mark group by thesaurus_code order by count desc;").addParams(new HashMap<String, Object>(){
   
            {
   
                put("deleted_mark",false);
            }
        }).listMapGet());
}

方法为addParams,用法与jdbc差不多。

更新数据

update方法

与条件查询方法一样,update方法也可以传入hashmap和三元组列表两种,适用于不同的场景。

传入map

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        Map<String,Object> condition = wordsQuery.find("id").size(1).simpleGet();
        wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        wordsQuery.update(condition,new HashMap<String, Object>(){
   
            {
   
                put("deleted_mark",true);
            }
        });
}

生成的sql为:

UPDATE material_thesaurus_words SET  deleted_mark=:setdeleted_mark,modified_time=:setmodified_time WHERE id = :id ;

参数为:

{modified_time=2023-06-05T17:37:57, setmodified_time=2023-06-05T17:37:56.998, id=585730307940941826, setdeleted_mark=true}

其中modified_time为默认更新字段

传入三元组列表

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        Map<String,Object> condition = wordsQuery.find("id").size(1).simpleGet();
        wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        wordsQuery.update(new ArrayList<Triplet<String, String, Object>>(){
   
            {
   
                add(new Triplet<>("deleted_mark","!=",true));
            }
        },new HashMap<String, Object>(){
   
            {
   
                put("deleted_mark",true);
            }
        });
}

生成的sql是:

UPDATE material_thesaurus_words SET  deleted_mark=:setdeleted_mark,modified_time=:setmodified_time WHERE deleted_mark != :deleted_mark ;

保存数据

insert方法

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        wordsQuery.insert(new HashMap<String, Object>(){
   
            {
   
                put("value","123");
                put("thesaurus_code","1");
            }
        });
}

生成的sql为:

INSERT INTO material_thesaurus_words(value,thesaurus_code) VALUES (:value,:thesaurus_code) RETURNING id;

batchInsert方法

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);
        wordsQuery.batchInsert(new ArrayList<Map<String, Object>>(){
   
            {
   
                add(new HashMap<String, Object>(){
   
                    {
   
                        put("value","123");
                        put("thesaurus_code","1");
                    }
                });
                add(new HashMap<String, Object>(){
   
                    {
   
                        put("value","1234");
                        put("thesaurus_code","1");
                    }
                });
            }
        });

生成的sql为:

INSERT INTO material_thesaurus_words(value,thesaurus_code) VALUES (:value0,:thesaurus_code0),(:value1,:thesaurus_code1);

删除

delete方法

按id删除

    @Test
    public void demo2(){
   
        SimpleQuery wordsQuery = new SimpleQuery(MaterialThesaurusWords.class);

        wordsQuery.delete(wordsQuery.find("id").size(1).simpleGet().get("id"));
}

生成sql:

UPDATE material_thesaurus_words SET  deleted_mark=:setdeleted_mark,deleted_time=:setdeleted_time WHERE id = :1c950fec1d9bb9f2a2a14502fa82d6d8 ;
目录
相关文章
|
7月前
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
92 2
|
7月前
|
SQL 存储 关系型数据库
SQL 入门教程:从基础到实践
**SQL 概述与基础操作** SQL,结构化查询语言,用于管理和操作数据库。核心概念包括数据库、表、行和列。基本语法涵盖DQL(查询)、DDL(定义)、DML(操纵)和DCL(控制)。关键操作: 1. **查询**:`SELECT`从表中获取数据。 2. **插入**:`INSERT INTO`添加新记录。 3. **更新**:`UPDATE`修改数据。 4. **删除**:`DELETE`移除记录。高级操作涉及条件、排序、分组和联合查询。实践操作需要数据库环境,如MySQL或在线编辑器。通过实例学习,如查询员工信息、部门员工及增删改数据,掌握SQL基础。
|
8月前
|
SQL 关系型数据库 MySQL
【MySQL】DQL-案例练习-DQL基本介绍&语法&执行顺序(代码演示)
【MySQL】DQL-案例练习-DQL基本介绍&语法&执行顺序(代码演示)
|
关系型数据库 MySQL Java
使用Go语言+IDEA+mysql 逆向生成dao 和 domain 的方法
如果需要扩展则只需要在该文件夹内加入对应的模板即可
75 0
|
SQL 关系型数据库 MySQL
设计模式之Database/SQL与GORM实践|青训营笔记
本篇笔记暂时的定位是以介绍两种方式操作(MySQL)数据库为主,并辅以一些源码的解读帮助大家更好理解数据库连接的过程。
262 0
设计模式之Database/SQL与GORM实践|青训营笔记
|
SQL 存储 自然语言处理
MyBatis 学习笔记(八)---源码分析篇--SQL 执行过程详细分析
在面试中我们经常会被到MyBatis中 #{} 占位符与${}占位符的区别。大多数的小伙伴都可以脱口而出#{} 会对值进行转义,防止SQL注入。而${}则会原样输出传入值,不会对传入值做任何处理。本文将通过源码层面分析为啥#{} 可以防止SQL注入。
435 0
MyBatis 学习笔记(八)---源码分析篇--SQL 执行过程详细分析
|
SQL 缓存 Java
MyBatis 学习笔记(七)---源码分析篇---SQL的执行过程(一)
接上一篇,今天我们接着来分析MyBatis的源码。今天的分析的核心是SQL的执行过程。主要分为如下章节进行分析
113 0
MyBatis 学习笔记(七)---源码分析篇---SQL的执行过程(一)
|
SQL 存储 数据库
数据库原理与应用(SQL Server)笔记 第七章 流程控制语句、系统内置函数
数据库原理与应用(SQL Server)笔记 第七章 流程控制语句、系统内置函数
数据库原理与应用(SQL Server)笔记 第七章 流程控制语句、系统内置函数
|
SQL 数据库
数据库原理与应用(SQL Server)笔记 第四章 嵌套查询和其他查询子句
数据库原理与应用(SQL Server)笔记 第四章 嵌套查询和其他查询子句
数据库原理与应用(SQL Server)笔记 第四章 嵌套查询和其他查询子句
|
SQL Oracle 关系型数据库
SQL入门第二篇——函数
单行处理函数的特点:一个输入对应一个输出 多行处理函数的特点:多个输入对应一个输出 单行处理函数有哪些