Bladeren bron

规范Mongodb示例方法名称

miaozy 4 jaren geleden
bovenliggende
commit
7788d573c3

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

@ -47,27 +47,10 @@ public class MongodbExample extends TestCase {
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 51
   * @throws Exception
69 52
   */
70
  public void testExecuteInsert2() throws Exception {
53
  public void testExecuteInsert() throws Exception {
71 54
    JSONObject param = new JSONObject();
72 55
    param.put("name", "mongo");
73 56
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
@ -157,32 +140,45 @@ public class MongodbExample extends TestCase {
157 140
    log.debug("符合条件记录数:" + noSql.takeRecordNum(cond.toJSONString()));
158 141
  }
159 142

143

160 144
  /**
161
   * 查询符合条件的首记录(升序)
145
   * 查询collection里符合条件的所有记录
162 146
   *
163 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 161
  public void testExecuteSelectOne() throws Exception {
166 162
    JSONObject cond = new JSONObject();
167
    cond.put("nickName", "miaozy");
163
    cond.put("nickName", "tom");
168 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 170
   * @throws Exception
175 171
   */
176
  public void testExecuteSelectOne2() throws Exception {
172
  public void testExecuteSelectFirst() throws Exception {
177 173
    JSONObject cond = new JSONObject();
178
    cond.put("nickName", "苗子阳");
174
    cond.put("nickName", "tom");
179 175
    JSONObject selectField = new JSONObject();
180 176
    selectField.put("name", 1);//1,返回。0不返回
181 177
    log.debug("查询结果为:" + noSql.executeSelectOne(cond.toJSONString(), selectField.toJSONString(), "{\"_id\":1}"));
182 178
  }
183 179

184 180
  /**
185
   * 查询符合条件的最后一条记录
181
   * 返回collection里符合条件的最后一条记录
186 182
   *
187 183
   * @throws Exception
188 184
   */
@ -195,11 +191,11 @@ public class MongodbExample extends TestCase {
195 191
  }
196 192

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

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

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

241 237

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

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

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

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

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

@ -45,7 +45,7 @@ public class MongodbExampleWithSql extends TestCase {
45 45
   * 使用标准sql插入一条数据
46 46
   *@throws Exception
47 47
   */
48
  public void testExecuteInsert2() throws Exception {
48
  public void testExecuteInsert() throws Exception {
49 49
    JSONObject param = new JSONObject();
50 50
    param.put("name", "test");
51 51
    log.debug("before insert, num is :" + noSql.takeRecordNum(param.toJSONString()));
@ -74,7 +74,7 @@ public class MongodbExampleWithSql extends TestCase {
74 74
   * 使用标准SQL条件查询mongodb(分页)
75 75
   * @throws Exception
76 76
   */
77
  public void testExecuteSelect1() throws Exception {
77
  public void testExecuteSelectWithPage() throws Exception {
78 78
    log.debug("查询结果为:" + noSql.executeSelect("select * from mycol1 limit 0,5"));
79 79
  }
80 80
@ -83,68 +83,55 @@ public class MongodbExampleWithSql extends TestCase {
83 83
   * 简单的汇总计数
84 84
   * @throws Exception
85 85
   */
86
  public void testExecuteSelect2() throws Exception {
86
  public void testExecuteSelectWithCount() throws Exception {
87 87
    log.debug("查询结果为:" + noSql.executeSelect("select count(1) totalNum from mycol1 where name != 'mike' "));
88 88
  }
89 89
90 90
  /**
91 91
   * 使用标准SQL查询mongodb
92
   * 普通查询 升序,降序
92
   * 排序符:asc,desc
93 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 101
   * 使用标准SQL查询mongodb
102
   * 条件:大于,小于,等于
102
   * 比较符: >,>=,=,<=,<,!=
103
   * @desc 当前age列插入的是数值型,如果是字符型,以大于条件为例:where age > '20';
103 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 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 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 116
   * 使用标准SQL查询mongodb
123
   * 条件:in,not in
117
   * 操作符号:in,not in,and,or
118
   * @desc 当前age列插入的是数值型,如果是字符型,以IN操作符为例: in('10','20');
124 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 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 130
   * 条件:group by,having,order by
144 131
   * 分类汇总
145 132
   * @throws Exception
146 133
   */
147
  public void testExecuteSelect8() throws Exception {
134
  public void testExecuteSelectWithManyCondition() throws Exception {
148 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
}