136
136
additional information, edit the log4j.properties file to have</para >
137
137
138
138
<programlisting >log4j.category.org.springframework.data.document.mongodb=DEBUG
139
- log4j.appender.stdout.layout.ConversionPattern=%- 5p [%c{3}]: %m%n</programlisting >
139
+ log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} % 5p %40.40c:%4L - %m%n</programlisting >
140
140
141
141
<para >Create a simple Person class to persist</para >
142
142
143
- <programlisting language =" java" >package org.spring.mongodb ;
143
+ <programlisting language =" java" >package org.spring.example ;
144
144
145
145
public class Person {
146
146
147
147
private String id;
148
- private String firstName;
149
- private String lastName;
148
+ private String name;
150
149
private int age;
151
-
152
- public Person(String firstName, String lastName, int age) {
153
- this.firstName = firstName;
154
- this.lastName = lastName;
150
+
151
+ public Person(String name, int age) {
152
+ this.name = name;
155
153
this.age = age;
156
154
}
157
- public String getFirstName() {
158
- return firstName;
159
- }
160
-
161
- public void setFirstName(String firstName) {
162
- this.firstName = firstName;
163
- }
164
-
165
- public String getLastName() {
166
- return lastName;
155
+
156
+ public String getId() {
157
+ return id;
167
158
}
168
-
169
- public void setLastName(String lastName) {
170
- this.lastName = lastName;
159
+ public String getName() {
160
+ return name;
171
161
}
172
-
173
162
public int getAge() {
174
163
return age;
175
164
}
176
-
177
- public void setAge(int age) {
178
- this.age = age;
179
- }
180
-
165
+
181
166
@Override
182
167
public String toString() {
183
- return "Person [id=" + id + ", firstName=" + firstName + ", lastName="
184
- + lastName + ", age=" + age + "]";
168
+ return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
185
169
}
186
-
187
170
188
171
}</programlisting >
189
172
190
173
<para >And a main application to run</para >
191
174
192
- <programlisting language =" java" >package org.spring.mongodb ;
175
+ <programlisting language =" java" >package org.spring.example ;
193
176
194
177
import static org.springframework.data.document.mongodb.query.Criteria.where;
195
178
@@ -199,85 +182,85 @@ import org.springframework.data.document.mongodb.MongoOperations;
199
182
import org.springframework.data.document.mongodb.MongoTemplate;
200
183
import org.springframework.data.document.mongodb.query.Query;
201
184
202
- import org.spring. mongodb.Person ;
185
+ import com. mongodb.Mongo ;
203
186
204
187
public class MongoApp {
205
188
206
189
private static final Log log = LogFactory.getLog(MongoApp.class);
207
190
208
- public static void main(String[] args) {
191
+ public static void main(String[] args) throws Exception {
209
192
210
193
MongoOperations mongoOps = new MongoTemplate(new Mongo(), "database");
211
-
212
- mongoOps.insert(new Person("Joe", "Swanson", 34));
213
194
214
- log.info( mongoOps.findOne(new Query(where("firstName").is("Joe")), Person.class) );
195
+ mongoOps.insert(new Person("Joe", 34));
196
+
197
+ log.info(mongoOps.findOne(new Query(where("name").is("Joe")), Person.class));
215
198
216
199
mongoOps.dropCollection("person");
217
200
}
218
-
219
201
}</programlisting >
220
202
221
203
<para >This will produce the following output</para >
222
204
223
- <programlisting >DEBUG [mongodb.mapping.MongoPersistentEntityIndexCreator]: Analyzing class class org.mongo.demo2.domain.Person for index information.
224
- DEBUG [document.mongodb.MongoTemplate]: insert DBObject containing fields: [_class, lastName, age, firstName] in collection: Person
225
- DEBUG [document.mongodb.MongoTemplate]: findOne using query: { "firstName" : "Joe"} in db.collection: database.Person
226
- INFO [spring.mongodb.MongoApp]: Person [id=4ddb2f629e60ab6a21da5fdb, firstName=Joe, lastName=Swanson, age=34]
227
- DEBUG [document.mongodb.MongoTemplate]: Dropped collection [database.person]</programlisting >
228
-
229
- <note >
230
- <para >If you are not using Maven then you would need to include the
231
- following jars on the classpath for a basic Spring MongoDB
232
- application:</para >
233
-
234
- <para ><itemizedlist >
235
- <listitem >
236
- <para >spring-data-commons-core-1.1.0.M1.jar</para >
237
- </listitem >
238
-
239
- <listitem >
240
- <para >spring-data-mongodb-1.0.0.M3.jar</para >
241
- </listitem >
242
- </itemizedlist >In addition to the above listed Spring Data jars you
243
- need to provide the following dependencies: <itemizedlist >
244
- <listitem >
245
- <para >com.springsource.org.aopalliance-1.0.0.jar</para >
246
- </listitem >
205
+ <programlisting >10:01:32,062 DEBUG apping.MongoPersistentEntityIndexCreator: 80 - Analyzing class class org.spring.example.Person for index information.
206
+ 10:01:32,265 DEBUG work.data.document.mongodb.MongoTemplate: 631 - insert DBObject containing fields: [_class, age, name] in collection: Person
207
+ 10:01:32,765 DEBUG work.data.document.mongodb.MongoTemplate:1243 - findOne using query: { "name" : "Joe"} in db.collection: database.Person
208
+ 10:01:32,953 INFO org.spring.example.MongoApp: 25 - Person [id=4ddbba3c0be56b7e1b210166, name=Joe, age=34]
209
+ 10:01:32,984 DEBUG work.data.document.mongodb.MongoTemplate: 375 - Dropped collection [database.person]</programlisting >
247
210
248
- <listitem >
249
- <para >commons-logging-1.1.1.jar</para >
250
- </listitem >
211
+ <para >The quick brown fox.</para >
251
212
252
- <listitem >
253
- <para >mongo-java-driver-2.5.3.jar</para >
254
- </listitem >
255
-
256
- <listitem >
257
- <para >spring-aop-3.0.5.RELEASE.jar</para >
258
- </listitem >
259
-
260
- <listitem >
261
- <para >spring-asm-3.0.5.RELEASE.jar</para >
262
- </listitem >
263
-
264
- <listitem >
265
- <para >spring-beans-3.0.5.RELEASE.jar</para >
266
- </listitem >
267
-
268
- <listitem >
269
- <para >spring-context-3.0.5.RELEASE.jar</para >
270
- </listitem >
271
-
272
- <listitem >
273
- <para >spring-core-3.0.5.RELEASE.jar</para >
274
- </listitem >
213
+ <section >
214
+ <title >Required Jars</title >
275
215
276
- <listitem >
277
- <para >spring-expression-3.0.5.RELEASE.jar</para >
278
216
</listitem >
279
- </itemizedlist ></para >
280
- </note >
217
+ <listitem >
218
+ <para >spring-data-document-core-1.0.0.M3.jar</para >
219
+ </listitem >
220
+
221
+ <listitem >
222
+ <para >spring-data-mongodb-1.0.0.M3.jar</para >
223
+ </listitem >
224
+ </itemizedlist >In addition to the above listed Spring Data jars
225
+ you need to provide the following dependencies: <itemizedlist >
226
+ <listitem >
227
+ <para >com.springsource.org.aopalliance-1.0.0.jar</para >
228
+ </listitem >
229
+
230
+ <listitem >
231
+ <para >commons-logging-1.1.1.jar</para >
232
+ </listitem >
233
+
234
+ <listitem >
235
+ <para >mongo-java-driver-2.5.3.jar</para >
236
+ </listitem >
237
+
238
+ <listitem >
239
+ <para >spring-aop-3.0.5.RELEASE.jar</para >
240
+ </listitem >
241
+
242
+ <listitem >
243
+ <para >spring-asm-3.0.5.RELEASE.jar</para >
244
+ </listitem >
245
+
246
+ <listitem >
247
+ <para >spring-beans-3.0.5.RELEASE.jar</para >
248
+ </listitem >
249
+
250
+ <listitem >
251
+ <para >spring-context-3I.0.5.RELEASE.jar</para >
252
+ </listitem >
253
+
254
+ <listitem >
255
+ <para >spring-core-3.0.5.RELEASE.jar</para >
256
+ </listitem >
257
+
258
+ <listitem >
259
+ <para >spring-expression-3.0.5.RELEASE.jar</para >
260
+ </listitem >
261
+ </itemizedlist ></para >
262
+ </note ></para >
263
+ </section >
281
264
</section >
282
265
283
266
<section >
0 commit comments