Ver Código Fonte

规范Mongodb示例方法名称

miaozy 4 anos atrás
pai
commit
7788d573c3

+ 42 - 49
ipu-mongodb-example/src/test/java/com/ai/ipu/example/mongodb/MongodbExample.java

47

47

48
  /**
48
  /**
49
   * 插入一条数据
49
   * 插入一条数据
50
   * @throws Exception
51
   */
52
  public void testExecuteInsert() throws Exception {
53
    JSONObject param = new JSONObject();
54
    param.put("name", "mongo");
55
    param.put("nikeName", "jack");
56
    param.put("ver", 7.0);
57
    param.put("age",21);
58
    param.put("info", "this is a test info");
59
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
60
    noSql.executeInsert(param);
61
    log.debug("after insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
62
  }
63

64

65
  /**
66
   * 插入一条数据
67
   *
50
   *
68
   * @throws Exception
51
   * @throws Exception
69
   */
52
   */
70
  public void testExecuteInsert2() throws Exception {
53
  public void testExecuteInsert() throws Exception {
71
    JSONObject param = new JSONObject();
54
    JSONObject param = new JSONObject();
72
    param.put("name", "mongo");
55
    param.put("name", "mongo");
73
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
56
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
157
    log.debug("符合条件记录数:" + noSql.takeRecordNum(cond.toJSONString()));
140
    log.debug("符合条件记录数:" + noSql.takeRecordNum(cond.toJSONString()));
158
  }
141
  }
159

142

143

160
  /**
144
  /**
161
   * 查询符合条件的首记录(升序)
145
   * 查询collection里符合条件的所有记录
162
   *
146
   *
163
   * @throws Exception
147
   * @throws Exception
164
   */
148
   */
149
  public void testExecuteSelectAll() throws Exception {
150
    JSONObject cond = new JSONObject();
151
    cond.put("nickName", "tom");
152
    log.debug("符合条件记录数:" + noSql.executeSelect(cond.toJSONString()));
153
  }
154

155

156
  /**
157
   * 返回collection里符合条件的首记录(升序)
158
   * 参数{"_id":1}: 1:代表升序输出,-1代表降序输出
159
   * @throws Exception
160
   */
165
  public void testExecuteSelectOne() throws Exception {
161
  public void testExecuteSelectOne() throws Exception {
166
    JSONObject cond = new JSONObject();
162
    JSONObject cond = new JSONObject();
167
    cond.put("nickName", "miaozy");
163
    cond.put("nickName", "tom");
168
    log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), "{\"_id\":1}"));
164
    log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), "{\"_id\":1}"));
169
  }
165
  }
170

166

171
  /**
167
  /**
172
   * 查询符合条件的首记录(返回指定字段)
173
   *
168
   * 指定返回字段,查询collection里符合条件的首记录
169
   * 参数{"_id":1}: 1:代表升序输出,-1代表降序输出
174
   * @throws Exception
170
   * @throws Exception
175
   */
171
   */
176
  public void testExecuteSelectOne2() throws Exception {
172
  public void testExecuteSelectFirst() throws Exception {
177
    JSONObject cond = new JSONObject();
173
    JSONObject cond = new JSONObject();
178
    cond.put("nickName", "苗子阳");
174
    cond.put("nickName", "tom");
179
    JSONObject selectField = new JSONObject();
175
    JSONObject selectField = new JSONObject();
180
    selectField.put("name", 1);//1,返回。0不返回
176
    selectField.put("name", 1);//1,返回。0不返回
181
    log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), selectField.toJSONString(), "{\"_id\":1}"));
177
    log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), selectField.toJSONString(), "{\"_id\":1}"));
182
  }
178
  }
183

179

184
  /**
180
  /**
185
   * 查询符合条件的最后一条记录
181
   * 返回collection里符合条件的最后一条记录
186
   *
182
   *
187
   * @throws Exception
183
   * @throws Exception
188
   */
184
   */
195
  }
191
  }
196

192

197
  /**
193
  /**
198
   * 查询符合条件的所有数据
194
   * 查询符合条件的所有数据,并排序
199
   *
195
   *
200
   * @throws Exception
196
   * @throws Exception
201
   */
197
   */
202
  public void testExecuteSelect() throws Exception {
198
  public void testExecuteSelectWithSort() throws Exception {
203
    JSONObject cond = new JSONObject();
199
    JSONObject cond = new JSONObject();
204
    cond.put("nickName", "tom");
200
    cond.put("nickName", "tom");
205
    JSONObject sort = new JSONObject();
201
    JSONObject sort = new JSONObject();
208
  }
204
  }
209

205

210
  /**
206
  /**
211
   * 设置返回记录的最大条数,查询符合条件的数据。
207
   * 查询collection符合条件的数据,设置返回记录的最大条数,
212
   * 类似mysql的limit
208
   * 类似mysql的limit
213
   * 类似oracle的rownum
209
   * 类似oracle的rownum
214
   * 类似sqlserver的top
210
   * 类似sqlserver的top
215
   *
211
   *
216
   * @throws Exception
212
   * @throws Exception
217
   */
213
   */
218
  public void testExecuteSelect2() throws Exception {
214
  public void testExecuteSelectWithCount() throws Exception {
219
    JSONObject cond = new JSONObject();
215
    JSONObject cond = new JSONObject();
220
    cond.put("nickName", "tom");
216
    cond.put("nickName", "tom");
221
    JSONObject sort = new JSONObject();
217
    JSONObject sort = new JSONObject();
224
  }
220
  }
225

221

226
  /**
222
  /**
227
   * 查询符合条件指定返回字段,并对结果排序
223
   * 查询collection里符合条件的数据:指定返回字段,对结果排序,并设置返回记录的最大条数.
228
   *
224
   *
229
   * @throws Exception
225
   * @throws Exception
230
   */
226
   */
231
  public void testExecuteSelect3() throws Exception {
227
  public void testExecuteSelectWithSortAndField() throws Exception {
232
    JSONObject cond = new JSONObject();
228
    JSONObject cond = new JSONObject();
233
    cond.put("nickName", "tom");
229
    cond.put("nickName", "tom");
234
    JSONObject sort = new JSONObject();
230
    JSONObject sort = new JSONObject();
240

236

241

237

242
  /**
238
  /**
243
   * 大于,大于等于
244
   * 等于,不等于
245
   * 小于,小于等于
246
   *
239
   * 查询collection里符合比较符条件的数据
240
   * 比较符: >,>=,=,<=,<,!=
247
   * @throws Exception
241
   * @throws Exception
248
   */
242
   */
249
  public void testExecuteSelect4() throws Exception {
243
  public void testExecuteSelectWithCompare() throws Exception {
250
    JSONObject cond = new JSONObject();
244
    JSONObject cond = new JSONObject();
251
    //cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN,"4");//大于
245
    cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN,"4");//大于
252
    //cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN_OR_EQUAL,"4");//大于等于
246
    //cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN_OR_EQUAL,"4");//大于等于
253
    //cond.put(MongoConstant.ConditionalOperator.EQUAL_TO,"3");//等于
247
    //cond.put(MongoConstant.ConditionalOperator.EQUAL_TO,"3");//等于
254
    cond.put(MongoConstant.ConditionalOperator.NOT_EQUAL_TO, "3");//不等于
248
    //cond.put(MongoConstant.ConditionalOperator.NOT_EQUAL_TO, "3");//不等于
255
    //cond.put(MongoConstant.ConditionalOperator.LESS_THAN,"3");//小于
249
    //cond.put(MongoConstant.ConditionalOperator.LESS_THAN,"3");//小于
256
    //cond.put(MongoConstant.ConditionalOperator.LESS_THAN_OR_EQUAL,"2");//小于等于
250
    //cond.put(MongoConstant.ConditionalOperator.LESS_THAN_OR_EQUAL,"2");//小于等于
257
    JSONObject param = new JSONObject();
251
    JSONObject param = new JSONObject();
262
  }
256
  }
263

257

264
  /**
258
  /**
265
   * in
266
   * not in
267
   *
259
   * 查询collection里符合范围条件的数据
260
   * 操作符:in,not in
268
   * @throws Exception
261
   * @throws Exception
269
   */
262
   */
270
  public void testExecuteSelect5() throws Exception {
263
  public void testExecuteSelectWithScope() throws Exception {
271
    JSONObject cond = new JSONObject();
264
    JSONObject cond = new JSONObject();
272
    String[] data = {"4", "5"};
265
    String[] data = {"4", "5"};
273
    //cond.put(MongoConstant.ConditionalOperator.IN,data);//in
266
    //cond.put(MongoConstant.ConditionalOperator.IN,data);//in
280
  }
273
  }
281

274

282
  /**
275
  /**
283
   * and
284
   * or
285
   *
276
   * 查询collection里符合条件的数据
277
   * 操作符:and, or
286
   * @throws Exception
278
   * @throws Exception
287
   */
279
   */
288
  public void testExecuteSelect6() throws Exception {
280
  public void testExecuteSelectWithAndOr() throws Exception {
289
    JSONObject param = new JSONObject();
281
    JSONObject param = new JSONObject();
290
    JSONObject cond1 = new JSONObject();
282
    JSONObject cond1 = new JSONObject();
291
    JSONObject cond2 = new JSONObject();
283
    JSONObject cond2 = new JSONObject();
306
  }
298
  }
307

299

308
  /**
300
  /**
309
   * not
301
   * 查询collection里符合条件的数据
302
   * 操作符:not
310
   *
303
   *
311
   * @throws Exception
304
   * @throws Exception
312
   */
305
   */
313
  public void testExecuteSelect7() throws Exception {
306
  public void testExecuteSelectWithNot() throws Exception {
314
    JSONObject param = new JSONObject();
307
    JSONObject param = new JSONObject();
315
    JSONObject cond = new JSONObject();
308
    JSONObject cond = new JSONObject();
316
    cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN, "2");
309
    cond.put(MongoConstant.ConditionalOperator.GREATOR_THAN, "2");
334
   * order by sumAge asc
327
   * order by sumAge asc
335
   * @throws Exception
328
   * @throws Exception
336
   */
329
   */
337
  public void testExecuteSelect11() throws Exception {
330
  public void testExecuteSelectWithManyCondition() throws Exception {
338
    JSONObject cond = new JSONObject();
331
    JSONObject cond = new JSONObject();
339
    cond.put("name", "{ $ne : \"mike\" }");
332
    cond.put("name", "{ $ne : \"mike\" }");
340
    JSONObject sort = new JSONObject();
333
    JSONObject sort = new JSONObject();

+ 22 - 35
ipu-mongodb-example/src/test/java/com/ai/ipu/example/mongodb/MongodbExampleWithSql.java

45
   * 使用标准sql插入一条数据
45
   * 使用标准sql插入一条数据
46
   *@throws Exception
46
   *@throws Exception
47
   */
47
   */
48
  public void testExecuteInsert2() throws Exception {
48
  public void testExecuteInsert() throws Exception {
49
    JSONObject param = new JSONObject();
49
    JSONObject param = new JSONObject();
50
    param.put("name", "test");
50
    param.put("name", "test");
51
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
51
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
74
   * 使用标准SQL条件查询mongodb(分页)
74
   * 使用标准SQL条件查询mongodb(分页)
75
   * @throws Exception
75
   * @throws Exception
76
   */
76
   */
77
  public void testExecuteSelect1() throws Exception {
77
  public void testExecuteSelectWithPage() throws Exception {
78
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 limit 0,5"));
78
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 limit 0,5"));
79
  }
79
  }
80
80
83
   * 简单的汇总计数
83
   * 简单的汇总计数
84
   * @throws Exception
84
   * @throws Exception
85
   */
85
   */
86
  public void testExecuteSelect2() throws Exception {
86
  public void testExecuteSelectWithCount() throws Exception {
87
    log.debug("查询结果为:" + noSql.executeSelect("select count(1) totalNum from mycol1 where name != 'mike' "));
87
    log.debug("查询结果为:" + noSql.executeSelect("select count(1) totalNum from mycol1 where name != 'mike' "));
88
  }
88
  }
89
89
90
  /**
90
  /**
91
   * 使用标准SQL查询mongodb
91
   * 使用标准SQL查询mongodb
92
   * 普通查询 升序,降序
92
   * 排序符:asc,desc
93
   * @throws Exception
93
   * @throws Exception
94
   */
94
   */
95
  public void testExecuteSelect3() throws Exception {
96
    //log.debug("升序查询结果为:" + noSql.executeSelect("select * from mycol1 where name != 'mike' order by age asc"));
97
    log.debug("降序查询结果为:" + noSql.executeSelect("select * from mycol1 where name != 'mike' order by age desc"));
95
  public void testExecuteSelectWithSort() throws Exception {
96
    log.debug("升序查询结果为:" + noSql.executeSelect("select * from mycol1 where name != 'mike' order by age asc"));
97
    //log.debug("降序查询结果为:" + noSql.executeSelect("select * from mycol1 where name != 'mike' order by age desc"));
98
  }
98
  }
99
99
100
  /**
100
  /**
101
   * 使用标准SQL查询mongodb
101
   * 使用标准SQL查询mongodb
102
   * 条件:大于,小于,等于
102
   * 比较符: >,>=,=,<=,<,!=
103
   * @desc 当前age列插入的是数值型,如果是字符型,以大于条件为例:where age > '20';
103
   * @throws Exception
104
   * @throws Exception
104
   */
105
   */
105
  public void testExecuteSelect4() throws Exception {
106
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age > 20"));
107
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age = 20"));
106
  public void testExecuteSelectWithCompare() throws Exception {
107
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age > 20"));
108
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age = 20"));
108
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age < 20"));
109
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age < 20"));
109
  }
110
111
  /**
112
   * 使用标准SQL查询mongodb
113
   * 条件:大于等于,小于等于
114
   * @throws Exception
115
   */
116
  public void testExecuteSelect5() throws Exception {
117
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age >= 20"));
110
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age >= 20"));
118
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age <= 20"));
111
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age <= 20"));
112
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age != 20"));
119
  }
113
  }
120
114
121
  /**
115
  /**
122
   * 使用标准SQL查询mongodb
116
   * 使用标准SQL查询mongodb
123
   * 条件:in,not in
117
   * 操作符号:in,not in,and,or
118
   * @desc 当前age列插入的是数值型,如果是字符型,以IN操作符为例: in('10','20');
124
   * @throws Exception
119
   * @throws Exception
125
   */
120
   */
126
  public void testExecuteSelect6() throws Exception {
127
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age in (10,20)"));
128
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age not in (20)"));
129
  }
130
131
  /**
132
   * 使用标准SQL查询mongodb
133
   * 条件:and,or
134
   * @throws Exception
135
   */
136
  public void testExecuteSelect7() throws Exception {
121
  public void testExecuteSelectWithScope() throws Exception {
122
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age in (10,20)"));
123
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age not in (20)"));
137
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age >= 20 and age < 21"));
124
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age >= 20 and age < 21"));
138
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age >= 20 or age < 21"));
125
    //log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 where age >= 20 or age < 21"));
139
  }
126
  }
140
127
141
  /**
128
  /**
142
   * 使用标准SQL查询mongodb
129
   * 使用标准SQL多条件查询mongodb
143
   * 条件:group by,having,order by
130
   * 条件:group by,having,order by
144
   * 分类汇总
131
   * 分类汇总
145
   * @throws Exception
132
   * @throws Exception
146
   */
133
   */
147
  public void testExecuteSelect8() throws Exception {
134
  public void testExecuteSelectWithManyCondition() throws Exception {
148
    log.debug("查询结果为:" + noSql.executeSelect("select name,nickName,sum(age) sumAge,count(1) totalNum from mycol1 where name != 'mike' group by name,nickName  having sumAge>=10 order by sumAge desc"));
135
    log.debug("查询结果为:" + noSql.executeSelect("select name,nickName,sum(age) sumAge,count(1) totalNum from mycol1 where name != 'mike' group by name,nickName  having sumAge>=10 order by sumAge desc"));
149
  }
136
  }
150
}
137
}