近期装了个mongodb,试试。发现很多网上找来的列子不对自己。
首先工程以来的jar包
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.0.3.RELEASE</version> </dependency>
简单的配置文件
<?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"> <mongo:mongo host="10.211.55.10" port="27017"/> <bean id="mongoOps" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg name="databaseName" value="test"/> </bean> </beans>
因为倒入了他自己的xsd文件,所以可以看下mongo的内容实际上是MongoFactoryBean
<xsd:element name="mongo" type="mongoType"> <xsd:annotation> <xsd:documentation source="org.springframework.data.mongodb.core.core.MongoFactoryBean"><![CDATA[ Defines a Mongo instance used for accessing MongoDB'. ]]></xsd:documentation> <xsd:appinfo> <tool:annotation> <tool:exports type="com.mongodb.Mongo"/> </tool:annotation> </xsd:appinfo> </xsd:annotation> </xsd:element>
然后看下MongoTemplate类的构造函数,就清楚了。
public MongoTemplate(Mongo mongo, String databaseName) { this(new SimpleMongoDbFactory(mongo, databaseName), null); }
测试类的基类;
import org.apache.log4j.Logger; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * User: zhangtan * Date: 2012-07-28 * Time: 11:14 */ public class BaseTestCase { protected Logger log = Logger.getLogger(this.getClass()); protected static ApplicationContext appContext; @BeforeClass public static void setUp() throws Exception { try { long start = System.currentTimeMillis(); System.out.println("开始加载配置文件"); appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"}); System.out.println("配置文件加载结束" + (System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); } } protected boolean setProtected() { return false; } @Before public void autoSetBean() { appContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, DefaultListableBeanFactory.AUTOWIRE_BY_NAME, false); } @AfterClass public static void tearDown() throws Exception { } }
增删改查基本操作
package dao; import com.mongodb.DBCollection; import com.mongodb.WriteResult; import com.zt.todo.log.UserLog; import org.junit.Test; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import java.net.UnknownHostException; import static org.springframework.data.mongodb.core.query.Criteria.where; /** * Created by IntelliJ IDEA. * User: zhangtan * Date: 12-7-28 * Time: 11:19 */ public class MongoTest extends BaseTestCase { private MongoTemplate mongoOps; @Test public void insertLog() throws UnknownHostException { UserLog userLog = new UserLog("zt", "吃饭"); mongoOps.insert(new UserLog("zt", "我今天吃饭了")); // mongoOps.dropCollection("person"); //save mongoOps.save(userLog,"userLog"); //find UserLog savedLog = mongoOps.findOne(new Query(where("operater").is("zt")), UserLog.class); System.out.println("savedLog : " + savedLog); //update WriteResult updateResult = mongoOps.updateFirst(new Query(where("operater").is("zt")), Update.update("operater", "qs"), UserLog.class); System.out.println("updateResult : " + updateResult.getN()); //delete mongoOps.remove(new Query(where("operater").is("zt")),"userLog"); //List DBCollection dBCollection = mongoOps.getCollection("userLog"); System.out.println("Number of user = " + dBCollection.getCount()); } public MongoTemplate getMongoOps() { return mongoOps; } public void setMongoOps(MongoTemplate mongoOps) { this.mongoOps = mongoOps; } }