1、Mongodb概述
Mongodb是面向文档的数据库,与关系型数据库有很大不同,它不能在表中存储数据,而是以文档的形式来存储数据,MongoDB 没有结构化语言。如果你想创建一个新的文档类型,你不用做任何事来告诉数据库关于这些数据的结构,而仅仅是存到数据库中即可。MongoDB 的 API 是 JSON 对象和 JavaScript 函数的本地混合物,所以mongodb的数据处理方式类似于javascript或php,也就是说其数据库是灵活的弱类型。
2、Mongodb的安装
首先我们从官网上http://www.mongodb.org/downloads下载 mongodbmongodb-win32-i386-2.4.3.zip
我下载的是windows-32位的,将其解压到E盘下,然后在E盘新建data文件夹,在data文件夹中创建db文件夹和log文件夹,在log文件夹中创建Mongo.log用来记录日志,接着打开dos命令窗口,进入到E盘下解压后的mongodb文件夹下的bin目录,执行:
mongod -dbpath "E:\data\db"
执行此命令即将mongodb的数据库文件创建到E:\data\db 目录,不出意外的会看到命令最后一行sucess的成功提示
此时数据库就已启动,该界面为Mongo的启动程序,关闭后可直接双击bin下的mongod.exe (注意是d,这个是启动程序)
启动程序开启后,再运行mongo.exe 程序(注意没有d) ,然后我们就可以再mongo.exe启动后的界面操作数据库了。这里就不做简单的测试了,我们直接整合spring和Mongodb。
3、spring和Mongodb的整合
我们使用maven来管理所需的jar文件,pom.xml代码如下:
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
这样我们就导入了所有所需的jar包,创建spring.xml配置文件来实现他们的整合
spring.xml
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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.1.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<mongo:mongo host="${mongodb.host}" port="${mongodb.port}" id="mongoDb">
<mongo:options connections-per-host="200"
threads-allowed-to-block-for-connection-multiplier="5"
connect-timeout="5000"
max-wait-time="5000"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="5000"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<mongo:db-factory id="mongoDbFactory" dbname="${db.name}" mongo-ref="mongoDb"/>
<!-- <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="userName"/>
<constructor-arg name="password" value="password"/>
</bean> -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg index="0" ref="mongoDbFactory"/>
</bean>
</beans>
database.properties
mongodb.host=127.0.0.1
mongodb.port=27017
db.name=mkk
admin.java类
package com.dxy.model;
public class Admin {
private String name ;
private String password ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
AdminDaoI.java接口
package com.dxy.dao;
public interface AdminDaoI {
public void add();
public void find();
public void update();
public void delete();
}
AdminDaoImpl.java类
package com.dxy.dao.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.dxy.dao.AdminDaoI;
import com.dxy.model.Admin;
public class AdminDaoImpl implements AdminDaoI {
private static ApplicationContext ac = null;
private static MongoTemplate mongoTemplate = null;
static{
ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
mongoTemplate = (MongoTemplate)ac.getBean("mongoTemplate");
}
@Override
public void add() {
Admin admin = new Admin();
admin.setName("sdfsdf");
admin.setPassword("sssss");
mongoTemplate.save(admin, "admin");
}
@Override
public void find() {
Admin admin = mongoTemplate.findOne(new Query(Criteria.where("name").is("sdfsdf")), Admin.class, "admin");
System.out.println(admin.getPassword());
}
@Override
public void update() {
mongoTemplate.updateFirst(new Query(Criteria.where("name").is("sdfsdf")),Update.update("password", "password") , Admin.class);
Admin admin = mongoTemplate.findOne(new Query(Criteria.where("name").is("sdfsdf")), Admin.class, "admin");
System.out.println(admin.getPassword());
}
@Override
public void delete() {
long ab = mongoTemplate.count(new Query(Criteria.where("name").is("sdfsdf")), "admin");
System.out.println(ab);
mongoTemplate.remove(new Query(Criteria.where("name").is("sdfsdf")), "admin");
long a = mongoTemplate.count(new Query(Criteria.where("name").is("sdfsdf")), "admin");
System.out.println(a);
}
}
接下来创建junit测试类AdminDaoImplTest.java
package com.dxy.dao.impl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dxy.dao.AdminDaoI;
public class AdminDaoImplTest {
ApplicationContext ac = null;
AdminDaoI adminDao = null;
@Before
public void testBefore() {
ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
adminDao = (AdminDaoI)ac.getBean("adminDao");
}
@Test
public void testAdd() {
adminDao.add();
}
@Test
public void testFind() {
adminDao.find();
}
@Test
public void testUpdate() {
adminDao.update();
}
@Test
public void testDelete() {
adminDao.delete();
}
}
进行增删改查的测试,搞定,注意在测试的时候要启动mongodb数据库,最后还有一些问题,就是在spring.xml文件中只要使用<mongo:mongo>标签,那么首行就会出现一个红叉,说什么xsd有问题,搞了半天都没搞定,最后又重新安装了一遍myeclipse还是没搞定,不过不影响程序的运行。听我们出去实习的同学说他们用Mongodb,就简单的尝试一下,因为快要实习了,得赶紧准备面试了,估计也就这么看一下,以后有时间再仔细研究吧。
最后再分享一个关于解析Mongodb源码的博客:http://www.iteye.com/blogs/subjects/MongoDBJavaDriver