JAVA下唯一一款搞定OLTP+OLAP的强类型查询这就是最好用的ORM相见恨晚

JAVA下唯一一款搞定OLTP+OLAP的强类型查询这就是最好用的ORM相见恨晚

介绍

首先非常感谢 FreeSQL 提供的部分源码,让我借鉴了不少功能点,整体设计并没有参考FreeSQL(因为java压根没有expression所以没办法参考)只是在数据库方言上FreeSQL提供的SQL让我少走了很多弯路,所以才让easy-query可以走的这么迅速

丑话说在前头,这是java下面唯一一款可以完全替代SQL的强类型ORM,完美支持OLTP和OLAP语法筛选记住是唯一一款

想体验完整版请查看文档博客篇幅有限见谅本次仅展示OLTP的对象关联查询

easy-query

文档地址 https://xuejmnet.github.io/easy-query-doc/ (为什么没有gitee的文档因为gitee pages挂掉了目前没办法更新)

GITHUB地址 https://github.com/xuejmnet/easy-query

GITEE地址 https://gitee.com/xuejm/easy-query

java下面唯一一款支持强类型OLTP和OLAP语法并且支持分表分库的最好用的ORM,为什么是最好用的OLTP那么我们先来看一个简单的例子

  • 用户、角色、菜单典型的多对多关联关系(隐式子查询)
  • 其中用户和用户所在地址为一对一关系(隐式join)

@Table("t_user")
@Data
@EntityProxy
public class SysUser implements ProxyEntityAvailable<SysUser , SysUserProxy> {
    @Column(primaryKey = true)
    private String id;
    private String name;
    private LocalDateTime createTime;

    @Navigate(value = RelationTypeEnum.ManyToMany,
            mappingClass = UserRole.class,
            selfMappingProperty = "userId",
            targetMappingProperty = "roleId")
    private List<SysRole> roles;

    @Navigate(value = RelationTypeEnum.OneToOne,targetProperty = "userId")
    private SysUserAddress address;

    @Override
    public Class<SysUserProxy> proxyTableClass() {
        return SysUserProxy.class;
    }
}


@Table("t_role")
@Data
@EntityProxy
public class SysRole implements ProxyEntityAvailable<SysRole, SysRoleProxy> {
    @Column(primaryKey = true)
    private String id;
    private String name;
    private LocalDateTime createTime;

    @Navigate(value = RelationTypeEnum.ManyToMany,
            mappingClass = UserRole.class,
            selfMappingProperty = "roleId",
            targetMappingProperty = "userId")
    private List<SysUser> users;

    @Navigate(value = RelationTypeEnum.ManyToMany,
            mappingClass = RoleMenu.class,
            selfMappingProperty = "roleId",
            targetMappingProperty = "menuId")
    private List<SysMenu> menus;

    @Override
    public Class<SysRoleProxy> proxyTableClass() {
        return SysRoleProxy.class;
    }
}


@Table("t_user_role")
@Data
@EntityProxy
public class UserRole implements ProxyEntityAvailable<UserRole , UserRoleProxy> {
    @Column(primaryKey = true)
    private String id;
    private String userId;
    private String roleId;

    @Override
    public Class<UserRoleProxy> proxyTableClass() {
        return UserRoleProxy.class;
    }
}


@Table("t_menu")
@Data
@EntityProxy
public class SysMenu implements ProxyEntityAvailable<SysMenu , SysMenuProxy> {
    @Column(primaryKey = true)
    private String id;
    private String name;
    private String route;
    private String icon;

    @Navigate(value = RelationTypeEnum.ManyToMany,
            mappingClass = RoleMenu.class,
            selfMappingProperty = "menuId",
            targetMappingProperty = "roleId")
    private List<SysRole> roles;

    @Override
    public Class<SysMenuProxy> proxyTableClass() {
        return SysMenuProxy.class;
    }
}


@Table("t_role_menu")
@Data
@EntityProxy
public class RoleMenu implements ProxyEntityAvailable<RoleMenu , RoleMenuProxy> {
    @Column(primaryKey = true)
    private String id;
    private String roleId;
    private String menuId;

    @Override
    public Class<RoleMenuProxy> proxyTableClass() {
        return RoleMenuProxy.class;
    }
}
@Table("t_user_address")
@Data
@EntityProxy
public class SysUserAddress implements ProxyEntityAvailable<SysUserAddress , SysUserAddressProxy> {
    @Column(primaryKey = true)
    private String id;
    private String userId;
    private String province;
    private String city;
    private String area;
    private String addr;

    @Override
    public Class<SysUserAddressProxy> proxyTableClass() {
        return SysUserAddressProxy.class;
    }
}

对应关系为用户和角色是多对多,角色和菜单也是多对多

案例1

查询杭州或绍兴的用户


        List<SysUser> userInHz = easyEntityQuery.queryable(SysUser.class)
                .where(s -> {
                    //隐式子查询会自动join用户表和地址表
                    s.or(()->{
                        s.address().city().eq("杭州市");
                        s.address().city().eq("绍兴市");
                    });
                }).toList();
SELECT
    t.`id`,
    t.`name`,
    t.`create_time` 
FROM
    `t_user` t 
LEFT JOIN
    `t_user_address` t1 
        ON t1.`user_id` = t.`id` 
WHERE
    (
        t1.`city` = '杭州市' 
        OR t1.`city` = '绍兴市'
    )

查询用户叫做小明的返回小明的姓名和小明所在地址


        List<Draft2<String, String>> userNameAndAddr = easyEntityQuery.queryable(SysUser.class)
                .where(s -> {
                    s.name().eq("小明");
                }).select(s -> Select.DRAFT.of(
                        s.name(),
                        s.address().addr()//隐式join因为用户返回了地址标的地址信息
                )).toList();

SELECT
    t.`name` AS `value1`,
    t1.`addr` AS `value2` 
FROM
    `t_user` t 
LEFT JOIN
    `t_user_address` t1 
        ON t1.`user_id` = t.`id` 
WHERE
    t.`name` = '小明'

查询用户叫做小明的返回用户的姓名地址和角色数量


    List<Draft3<String, String, Long>> userNameAndAddrAndRoleCount = easyEntityQuery.queryable(SysUser.class)
            .where(s -> {
                s.name().eq("小明");
            }).select(s -> Select.DRAFT.of(
                    s.name(),
                    s.address().addr(),
                    s.roles().count()//隐式子查询返回用户拥有的角色数量
            )).toList();
            
SELECT
    t.`name` AS `value1`,
    t1.`addr` AS `value2`,
    (SELECT
        COUNT(*) 
    FROM
        `t_role` t3 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                `t_user_role` t4 
            WHERE
                t4.`role_id` = t3.`id` 
                AND t4.`user_id` = t.`id` LIMIT 1
        )
    ) AS `value3` 
FROM
    `t_user` t 
LEFT JOIN
    `t_user_address` t1 
        ON t1.`user_id` = t.`id` 
WHERE
    t.`name` = '小明'         
           

案例2

查询用户下面存在角色是收货员的用户


        List<SysUser> 收货员 = easyEntityQuery.queryable(SysUser.class)
                .where(s -> {
                    //筛选条件为角色集合里面有角色名称叫做收货员的
                    s.roles().where(role -> {
                        role.name().eq("收货员");
                    }).any();
                }).toList();

SELECT
    t.`id`,
    t.`name`,
    t.`create_time` 
FROM
    `t_user` t 
WHERE
    EXISTS (
        SELECT
            1 
        FROM
            `t_role` t1 
        WHERE
            EXISTS (
                SELECT
                    1 
                FROM
                    `t_user_role` t2 
                WHERE
                    t2.`role_id` = t1.`id` 
                    AND t2.`user_id` = t.`id` LIMIT 1
            ) 
            AND t1.`name` = '收货员' LIMIT 1
        )

案例3

查询用户下面存在角色是XX员,并且存在个数大于5个的用户,就是说需要满足用户下面的角色是xx员的起码有5个及以上的


        List<SysUser> 收货员 = easyEntityQuery.queryable(SysUser.class)
                .where(s -> {
                    //筛选条件为角色集合里面有角色名称叫做xx员的
                    s.roles().where(role -> {
                        role.name().likeMatchRight("员");
                    }).count().gt(5L);//count数量大于5个
                }).toList();


-- 第1条sql数据
SELECT
    t.`id`,
    t.`name`,
    t.`create_time` 
FROM
    `t_user` t 
WHERE
    (
        SELECT
            COUNT(*) 
        FROM
            `t_role` t1 
        WHERE
            EXISTS (
                SELECT
                    1 
                FROM
                    `t_user_role` t2 
                WHERE
                    t2.`role_id` = t1.`id` 
                    AND t2.`user_id` = t.`id` LIMIT 1
            ) 
            AND t1.`name` LIKE '%员'
        ) > 5

案例4

查询用户下面存在的任意角色不大于2022年创建的



LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 0, 0);
List<SysUser> 收货员 = easyEntityQuery.queryable(SysUser.class)
        .where(s -> {
            //筛选条件为角色集合里面有角色最大时间不能大于2022年的
            s.roles().max(role -> role.createTime()).lt(localDateTime);
        }).toList();

SELECT
    t.`id`,
    t.`name`,
    t.`create_time` 
FROM
    `t_user` t 
WHERE
    (
        SELECT
            MAX(t1.`create_time`) 
        FROM
            `t_role` t1 
        WHERE
            EXISTS (
                SELECT
                    1 
                FROM
                    `t_user_role` t2 
                WHERE
                    t2.`role_id` = t1.`id` 
                    AND t2.`user_id` = t.`id` LIMIT 1
            )
        ) < '2022-01-01 00:00'

案例5

查询每个用户和前3个最早创建的角色(支持分页)适用于评论和评论子表前N个


        List<SysUser> 收货员 = easyEntityQuery.queryable(SysUser.class)
                //前面的表达式表示要返回roles后面的表示如何返回返回按时间正序的3个
                .includes(s -> s.roles(),x->{
                    x.orderBy(r->r.createTime().asc()).limit(3);
                })
                .toList();

案例6

查询用户小明下面的菜单


//方式1多次查询
        List<SysMenu> menus = easyEntityQuery.queryable(SysUser.class)
                .where(s -> {
                    s.name().eq("小明");
                })
                .toList(x -> x.roles().flatElement().menus().flatElement());


//方式2一次次查询
        List<SysMenu> menus = easyEntityQuery.queryable(SysMenu.class)
                .where(s -> {
                    //判断菜单下的角色存在角色的用户叫做小明的
                    s.roles().any(role -> {
                        role.users().any(user -> {
                            user.name().eq("小明");
                        });
                    });
                }).toList();


-- 第1条sql数据
SELECT
    t.`id`,
    t.`name`,
    t.`route`,
    t.`icon` 
FROM
    `t_menu` t 
WHERE
    EXISTS (
        SELECT
            1 
        FROM
            `t_role` t1 
        WHERE
            EXISTS (
                SELECT
                    1 
                FROM
                    `t_role_menu` t2 
                WHERE
                    t2.`role_id` = t1.`id` 
                    AND t2.`menu_id` = t.`id` LIMIT 1
            ) 
            AND EXISTS (
                SELECT
                    1 
                FROM
                    `t_user` t3 
                WHERE
                    EXISTS (
                        SELECT
                            1 
                        FROM
                            `t_user_role` t4 
                        WHERE
                            t4.`user_id` = t3.`id` 
                            AND t4.`role_id` = t1.`id` LIMIT 1
                    ) 
                    AND t3.`name` = '小明' LIMIT 1
                ) LIMIT 1
        )

案例7

自动返回用户和用户下的角色和角色下的菜单

首先通过idea插件EasyQueryAssistant在指定目录创建Struct DTO

最终会生成如下dto


/**
 * this file automatically generated by easy-query struct dto mapping
 * 当前文件是easy-query自动生成的 结构化dto 映射
 * {@link com.easy.query.test.entity.blogtest.SysUser }
 *
 * @author easy-query
 */
@Data
public class UserRoleMenuDTO {


    private String id;
    private String name;
    @Navigate(value = RelationTypeEnum.ManyToMany)
    private List<InternalRoles> roles;


    /**
     * {@link com.easy.query.test.entity.blogtest.SysRole }
     */
    @Data
    public static class InternalRoles {
        private String id;
        private String name;
        @Navigate(value = RelationTypeEnum.ManyToMany)
        private List<InternalMenus> menus;


    }


    /**
     * {@link com.easy.query.test.entity.blogtest.SysMenu }
     */
    @Data
    public static class InternalMenus {
        private String id;
        private String name;
        private String route;
        private String icon;


    }

}

查询selectAutoInclude


        List<UserRoleMenuDTO> menus = easyEntityQuery.queryable(SysUser.class)
                .where(u -> {
                    u.name().like("小明");
                    u.createTime().rangeClosed(LocalDateTime.now().plusDays(-100),LocalDateTime.now());
                })
                .selectAutoInclude(UserRoleMenuDTO.class)
                .toList();
//通过selectAutoInclude即可映射到我们的DTO 可以返回任意对象关系

最后

这边展示了非常强大的OLTP查询模式,OLAP也是非常强大可以group+join,实现from (匿名sql) 也可以join (匿名sql)

一款具有强类型OLTP+OLAP的完美解决方案,并且完美支持mybatis系列的任意架构逐步构建迁移,不会产生任何冲突,因为easy-query本身就是零依赖,并且完全免费,完全开源(包括文档!!!包括文档!!!包括文档!!!)

我相信easy-query是一款可以完完全全打动您的ORM作品,也是全java唯一一款全sql替代性产品

热门相关:美食萌后:皇上,喂不饱!   校花之贴身高手   我在镇夜司打开地狱之门   我在镇夜司打开地狱之门   误踩老公底线:甜心难招架!