团队对封装组件的代码范例

MongodbExample.java 11KB

    package com.ai.ipu.example.mongodb; import com.ai.ipu.basic.log.ILogger; import com.ai.ipu.basic.log.IpuLoggerFactory; import com.ai.ipu.nosql.INoSql; import com.ai.ipu.nosql.mongodb.MongoCacheFactory; import com.ai.ipu.nosql.util.MongoConstant; import com.alibaba.fastjson.JSONObject; import junit.framework.TestCase; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author miaozy@asiainfo.com * @date 2020/9/27 7:36 下午 * @desc mongoDB操作示例 */ public class MongodbExample extends TestCase { transient protected ILogger log = IpuLoggerFactory.createLogger(MongodbExample.class); private String connName; private String dbName; private String tableName; private INoSql noSql; @Override protected void setUp() throws Exception { super.setUp(); connName = "data"; dbName = "test"; tableName = "mycol1"; noSql = MongoCacheFactory.getMongoDao(connName, dbName, tableName); } @Override protected void tearDown() throws Exception { //如果开启了事务开关,就要打开这里的提交代码 //如果不需要支持事务,请注释这里的提交代码 if (noSql != null) noSql.commitTransaction(); } /** * 插入一条数据 * * @throws Exception */ public void testExecuteInsert() throws Exception { JSONObject param = new JSONObject(); param.put("name", "mongo"); log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString())); JSONObject data = new JSONObject(); data.put("name", "mongo"); data.put("nickName", "tom"); data.put("age", 21); Map info = new HashMap(); info.put("ver", 7.0); data.put("info", info); noSql.executeInsert(data); log.debug("after insert, num is :" + noSql.takeRecordNum(param.toJSONString())); } /** * 删除符合条件的所有数据 * * @throws Exception */ public void testExecuteDeleteMany() throws Exception { JSONObject param = new JSONObject(); param.put("nickName", "tom1"); noSql.executeDeleteMany(param.toJSONString()); } /** * 删除collection里最后一条符合查询条件的记录 * * @throws Exception */ public void testExecuteDeleteLast() throws Exception { JSONObject param = new JSONObject(); param.put("nickName", "miaozy10"); noSql.executeDeleteLast(param.toJSONString()); } /** * 更新collection里所有符合更新条件的记录 * * @throws Exception */ public void testExecuteUpdateMany() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "miaozy"); JSONObject field = new JSONObject(); field.put("name", "1256"); noSql.executeUpdateMany(cond.toJSONString(), field.toJSONString()); } /** * 更新collection里所有符合更新条件的第一条记录 * * @throws Exception */ public void testExecuteUpdateFirst() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "miaozy"); JSONObject field = new JSONObject(); field.put("name", "mongo"); noSql.executeUpdateFirst(cond.toJSONString(), field.toJSONString()); } /** * 更新collection里所有符合更新条件的最后一条记录 * * @throws Exception */ public void testExecuteUpdateLast() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "miaozy"); JSONObject field = new JSONObject(); field.put("name", "mongo"); noSql.executeUpdateLast(cond.toJSONString(), field.toJSONString()); } /** * 返回collection里所有符合条件的记录数 * * @throws Exception */ public void testTakeRecordNum() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "miaozy"); log.debug("符合条件记录数:" + noSql.takeRecordNum(cond.toJSONString())); } /** * 查询collection里符合条件的所有记录 * * @throws Exception */ public void testExecuteSelectAll() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "tom"); log.debug("符合条件记录数:" + noSql.executeSelect(cond.toJSONString())); } /** * 返回collection里符合条件的首记录(升序) * 参数{"_id":1}: 1:代表升序输出,-1代表降序输出 * @throws Exception */ public void testExecuteSelectOne() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "tom"); log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), "{\"_id\":1}")); } /** * 指定返回字段,查询collection里符合条件的首记录 * 参数{"_id":1}: 1:代表升序输出,-1代表降序输出 * @throws Exception */ public void testExecuteSelectFirst() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "tom"); JSONObject selectField = new JSONObject(); selectField.put("name", 1);//1,返回。0不返回 log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), selectField.toJSONString(), "{\"_id\":1}")); } /** * 返回collection里符合条件的最后一条记录 * * @throws Exception */ public void testExecuteSelectLast() throws Exception { JSONObject condParam = new JSONObject(); condParam.put("nickName", "tom"); JSONObject sortParam = new JSONObject(); sortParam.put("_id", -1); log.debug("查询结果为:" + noSql.executeSelect(condParam.toJSONString(), sortParam.toJSONString(), 1)); } /** * 查询符合条件的所有数据,并排序 * * @throws Exception */ public void testExecuteSelectWithSort() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "tom"); JSONObject sort = new JSONObject(); sort.put("_id", 1); log.debug("查询结果为:" + noSql.executeSelect(cond.toJSONString(), sort.toJSONString())); } /** * 查询collection符合条件的数据,设置返回记录的最大条数, * 类似mysql的limit * 类似oracle的rownum * 类似sqlserver的top * * @throws Exception */ public void testExecuteSelectWithCount() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "tom"); JSONObject sort = new JSONObject(); sort.put("_id", 1); log.debug("查询结果为:" + noSql.executeSelect(cond.toJSONString(), sort.toJSONString(), 1)); } /** * 查询collection里符合条件的数据:指定返回字段,对结果排序,并设置返回记录的最大条数. * * @throws Exception */ public void testExecuteSelectWithSortAndField() throws Exception { JSONObject cond = new JSONObject(); cond.put("nickName", "tom"); JSONObject sort = new JSONObject(); sort.put("_id", 1); JSONObject selectField = new JSONObject(); selectField.put("name", 1);//1,返回。0不返回) log.debug("查询结果为:" + noSql.executeSelect(cond.toJSONString(), selectField.toJSONString(), sort.toJSONString(), 1)); } /** * 查询collection里符合比较符条件的数据 * 比较符: >,>=,=,<=,<,!= * @throws Exception */ public void testExecuteSelectWithCompare() throws Exception { JSONObject cond = new JSONObject(); cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN,"4");//大于 //cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN_OR_EQUAL,"4");//大于等于 //cond.put(MongoConstant.ConditionalOperator.EQUAL_TO,"3");//等于 //cond.put(MongoConstant.ConditionalOperator.NOT_EQUAL_TO, "3");//不等于 //cond.put(MongoConstant.ConditionalOperator.LESS_THAN,"3");//小于 //cond.put(MongoConstant.ConditionalOperator.LESS_THAN_OR_EQUAL,"2");//小于等于 JSONObject param = new JSONObject(); param.put("age", cond); List<String> ret = noSql.executeSelect(param.toJSONString(), "{}"); log.debug("符合条件的数据有" + ret.size() + "条"); log.debug("查询结果为:" + ret.toString()); } /** * 查询collection里符合范围条件的数据 * 操作符:in,not in * @throws Exception */ public void testExecuteSelectWithScope() throws Exception { JSONObject cond = new JSONObject(); String[] data = {"4", "5"}; //cond.put(MongoConstant.ConditionalOperator.IN,data);//in cond.put(MongoConstant.ConditionalOperator.NOT_IN, data);//not in JSONObject param = new JSONObject(); param.put("age", cond); List<String> ret = noSql.executeSelect(param.toJSONString(), "{}"); log.debug("符合条件的数据有" + ret.size() + "条"); log.debug("查询结果为:" + ret.toString()); } /** * 查询collection里符合条件的数据 * 操作符:and, or * @throws Exception */ public void testExecuteSelectWithAndOr() throws Exception { JSONObject param = new JSONObject(); JSONObject cond1 = new JSONObject(); JSONObject cond2 = new JSONObject(); cond1.put(MongoConstant.ConditionalOperator.LESS_THAN, "4"); cond2.put(MongoConstant.ConditionalOperator.GREATOR_THAN, "2"); List list = new ArrayList(); Map info = new HashMap(); info.put("age", cond1); list.add(info); info = new HashMap(); info.put("age", cond2); list.add(info); //param.put(MongoConstant.RelationalOperator.OR, list);//or param.put(MongoConstant.RelationalOperator.AND, list);//and List<String> ret = noSql.executeSelect(param.toJSONString(), "{}"); log.debug("符合条件的数据有" + ret.size() + "条"); log.debug("查询结果为:" + ret.toString()); } /** * 查询collection里符合条件的数据 * 操作符:not * * @throws Exception */ public void testExecuteSelectWithNot() throws Exception { JSONObject param = new JSONObject(); JSONObject cond = new JSONObject(); cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN, "2"); JSONObject cond1 = new JSONObject(); cond1.put(MongoConstant.RelationalOperator.NOT, cond); param.put("age", cond1); List<String> ret = noSql.executeSelect(param.toJSONString(), "{}"); log.debug("符合条件的数据有" + ret.size() + "条"); log.debug("查询结果为:" + ret.toString()); } /** * 多条件查询 * select name,nickName,count(1) totalNum,sum(age) sumAge * from mycol1 where name != 'mike' * group by name,nickName * having sumAge >10 * order by sumAge asc * @throws Exception */ public void testExecuteSelectWithManyCondition() throws Exception { JSONObject cond = new JSONObject(); cond.put("name", "{ $ne : \"mike\" }"); JSONObject sort = new JSONObject(); sort.put("totalAge", 1); JSONObject havingParam = new JSONObject(); havingParam.put("totalAge", "{ $gt : 10}"); JSONObject sumField = new JSONObject(); sumField.put("totalAge", "age"); JSONObject selectField = new JSONObject(); selectField.put("name", 1); selectField.put("nickName", 1); log.debug("查询结果为:" + noSql.executeAggregateSelect(selectField.toJSONString(), cond.toJSONString(), sort.toJSONString(), havingParam.toJSONString(), sumField.toJSONString(), true)); } }