最近工作很忙,有时间简单整理了一下:
进入正题:此文章主要讲述spring-data-mongodb的配置和开发
第一步:新建一个web项目(如果只是跑后台程序,java项目也是可以的)。新建项目不在说明:
第二步:去http://www.springsource.org/spring-data/mongodb官网下载所需要的JAR,然后倒入到你项目的lib目录下。
第三步:“spring-mongo.xml”配置文件,放在项目的/src/下面,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- modify you mongodb IP' -->
<mongo:mongo host="10.10.22.254" port="27017" />
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="LCDB" />
<constructor-arg name="defaultCollectionName" value="LCCollection" />
</bean>
<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
<context:annotation-config />
</beans>
第四步:编写代码(通过spring获取mongodb实例),可以运行这个代码,如果可以获取到实例的内存地址,就可以正常连接到mongodb了
package com.lc.mongodb.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoTemplate;
/**
*
* @Date 2013-7-25 上午08:52:03
* @Author lichao
* @Project SpringMongodbExample/com.lc.mongodb.util.MongodbDBUtil.java
*
*
* @ModifyDate 2013-7-25 上午08:52:03
* @ModifyUser lichao
* @Description TODO
*
*/
public class MongodbDBUtil {
private static MongoTemplate mongoTemplate;
static {
ApplicationContext ctx = new GenericXmlApplicationContext(
"spring-mongo.xml");
mongoTemplate = (MongoTemplate) ctx.getBean("mongoTemplate");
}
/**
*
*@Date 2013-7-25-上午08:52:33
*@user lichao
*@Description TODO
*@return
*/
public static MongoTemplate getMongoTemplate() {
return mongoTemplate;
}
public static void main(String[] args) {
System.out.println("通过springMVC获取到mongodb实例********** "+MongodbDBUtil.getMongoTemplate());
}
}
第五步:编写测试类
package com.lc.mongodb.test;
import java.util.List;
import org.springframework.data.document.mongodb.MongoTemplate;
import com.lc.mongodb.util.MongodbDBUtil;
class Student {
private String name;
private int age;
private String code;
private double score;
public Student() {
}
public Student(String name, int age, String code, double score) {
this.name = name;
this.age = age;
this.code = code;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
String str = "姓名:"+this.name+"\n"+"年龄:"+this.age+"\n"+"学号:"+this.code+"\n"+"成绩:"+this.score;
return str;
}
}
public class TestSpringMongodb {
public static void main(String[] args) {
MongoTemplate mongoTemplate = MongodbDBUtil.getMongoTemplate();
Student student = null;
for (int i = 0; i < 10; i++) {
student = new Student("lichao"+i,23+i,"code"+i,i);
//this first param is this "spring-mongo.xml" config <constructor-arg name="defaultCollectionName" value="LCCollection" />
mongoTemplate.save("LCCollection", student);
//mongoTemplate.save( student);
}
List<Student> allStudent = mongoTemplate.getCollection("LCCollection",Student.class);
for (Student stu : allStudent) {
System.out.println(stu);
System.out.println("********************华丽的分割线******************");
}
}
}
通过MongoVue查看结果
/* 0 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d0"),
"age" : 23,
"name" : "lichao0",
"score" : 0.0,
"code" : "code0"
}
/* 1 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d1"),
"age" : 24,
"name" : "lichao1",
"score" : 1.0,
"code" : "code1"
}
/* 2 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d2"),
"age" : 25,
"name" : "lichao2",
"score" : 2.0,
"code" : "code2"
}
/* 3 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d3"),
"age" : 26,
"name" : "lichao3",
"score" : 3.0,
"code" : "code3"
}
/* 4 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d4"),
"age" : 27,
"name" : "lichao4",
"score" : 4.0,
"code" : "code4"
}
/* 5 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d5"),
"age" : 28,
"name" : "lichao5",
"score" : 5.0,
"code" : "code5"
}
/* 6 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d6"),
"age" : 29,
"name" : "lichao6",
"score" : 6.0,
"code" : "code6"
}
/* 7 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d7"),
"age" : 30,
"name" : "lichao7",
"score" : 7.0,
"code" : "code7"
}
/* 8 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d8"),
"age" : 31,
"name" : "lichao8",
"score" : 8.0,
"code" : "code8"
}
/* 9 */
{
"_id" : ObjectId("51f07b0eb969db26cee687d9"),
"age" : 32,
"name" : "lichao9",
"score" : 9.0,
"code" : "code9"
}
说明mongodb操作成功,关于springMongodb就讲解到这里,后期讲述mongodb处理大数据的案例和维护。(讲述当中有错误的,希望大家提出,一起学习进步)