1 Star 0 Fork 0

mission_cn / component-data-mongodb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

component-data-mongodb

An enhanced component for Mongodb to simplify development.

Background

Write a project for person learning.

Example 1

        // 方式1: CriteriaSimpleBuilder
        DepartmentDTO departmentDTO = new DepartmentDTO();
        departmentDTO.setCreateTime(example.getCreateTime());
        departmentDTO.setCharacter(example.getCharacter());

        // 以And连接,dto属性注解@BindQuery + CriteriaSimpleBuilder.byModel(queryModel),构造 Criteria
        Criteria criteria1 = CriteriaBuilder.createAndLinkBuilder()
                .byModel(departmentDTO)
                .build();

        // 以Or连接,dto属性注解@BindQuery + CriteriaSimpleBuilder.byModel(queryModel), 构造 Criteria
        Criteria criteria2 = CriteriaBuilder.createOrLinkBuilder()
                .byModel(departmentDTO)
                .build();

        //  以And连接,CriteriaSimpleBuilder.eq(column, params)...链式构造,构造 Criteria
        Criteria criteria3 = CriteriaBuilder.createAndLinkBuilder()
                .gte("createTime", example.getCreateTime())
                .eq("parentId",example.getCreateTime())
                .build();

        //  CriteriaSimpleBuilder复杂组合,构造 Criteria
        Criteria criteria4 = CriteriaBuilder.createAndLinkBuilder()
                .byModel(departmentDTO)
                .gte("createTime",  example.getCreateTime())
                .eq("department.parentId", example.getCreateTime())
                .append(Criteria.where("orgId").is(1))
                .append(
                        CriteriaBuilder
                                .createOrLinkBuilder()
                                .like("name", "test")
                                .build()
                        )
                .build();


        // 方式2:CriteriaLambdaBuilder
        // 以And连接,注解@BindQuery + CriteriaLambdaBuilder + SerializableFunction组合 构造 Criteria, 用法与上方相似
        Criteria criteria5 = CriteriaBuilder.createAndLinkLambdaBuilder()
                .byModel(departmentDTO)
                .gte(DepartmentDTO::getCreateTime, example.getCreateTime())
                .like(DepartmentDTO::getCharacter, example.getCharacter())
                .build();


        // elemMatch 数组模糊查询
        Criteria criteria6 = CriteriaBuilder.createOrLinkLambdaBuilder()
                .arrayElemLike(SimpleSplitVO::getManagers, TestUser::getUsername, "test" )
                .build();


        // QueryBuilder 构造 Query, 并链式调用printQueryLog 打印Find和Count语句
        Query query1 = QueryBuilder.query(criteria1)
                .skip(0)
                .limit(10)
                .printQueryLog(mongoConverter, Department.class) //打印 db.collection.find()
                .printCountLog(mongoConverter, Department.class) //打印 db.collection.count()
                .build();


        List<Department> list1 = mongoTemplate.find(query1, Department.class);
        Assert.assertTrue(list1.size() >= 1);

        long count = mongoTemplate.count(query1, Department.class);
        Assert.assertTrue(count >= 1);


        // 从 QueryBuilder 获取 日志, 打印
        QueryBuilder queryBuilder = QueryBuilder.query(criteria1);
        System.out.println(queryBuilder.getQueryLog(mongoConverter, Department.class));
        Query query2 = queryBuilder.build();

        List<Department> list2 = mongoTemplate.find(query2,Department.class);
        Assert.assertTrue(list2.size() >= 1);

        // sort 构造器
        Sort sort = SortBuilder.create()
                .add("createTime", Sort.Direction.DESC)
                .add(Department::getId, Sort.Direction.ASC)
                .add(Sort.Order.asc("parentId"))
                .build();

        query1.with(sort);
        //打印 含有排序语句
        QueryBuilder.of(query1).printQueryLog(mongoConverter, Department.class);
        List<Department> list3 = mongoTemplate.find(query1, Department.class);
        Assert.assertTrue(list3.size() >= 1);

MIT License Copyright (c) 2023 Missionary Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

An enhanced component for Mongodb to simplify development. 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/missionodd/component-data-mongodb.git
git@gitee.com:missionodd/component-data-mongodb.git
missionodd
component-data-mongodb
component-data-mongodb
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891