1.运用spring集成mongodb是,刚开始最大的困难不是怎么操作,而是有关导入jar文件的问题。我在做一个小例子的时候,一直有一个错误就是NoSuchMethod。网上查了好多资料说是spring-data-mongodb与spring-data-commons包版本不一致的问题,下载了一个晚上的不同版本jar文件。后来才发现原来是spring-framework版本太低,于是果断换了3.2.1版本就好了。提醒各位排查错误的时候有时我们需要换个角度来。
2.spring配置mongodb
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <context:property-placeholder location="classpath:mongo.properties" /> <context:component-scan base-package="com.mongo.dao" /> <context:component-scan base-package="com.mongo.service" /> <!-- Default bean name is 'mongo' --> <mongo:mongo host="localhost" port="27017"> <mongo:options connections-per-host="8" threads-allowed-to-block-for-connection-multiplier="4" connect-timeout="1000" max-wait-time="1500" auto-connect-retry="true" socket-keep-alive="true" socket-timeout="1500" slave-ok="true" write-number="1" write-timeout="0" write-fsync="true" /> </mongo:mongo> <!-- 注册MongoDbFactory类 --> <mongo:db-factory id="mongoDbFactory" host="localhost" port="27017" dbname="bylw" username="xiaozhi" password="123" /> <!-- 注册MongoTemplate类 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoDbFactory" /> <!-- <constructor-arg name="databaseName" value="xiaozhi" /> --> </bean> </beans>
?domain类:Student
package com.mongo.domain; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.mongodb.core.mapping.Document; /* * 学生信息集合类 */ @Document public class Student { @Id private String sno; // 学号 private String sname; // 姓名 private String ssex; // 性别 private int sage; // 年龄 private String sdept; // 学院 private String smajor; // 专业 private String s_entrance; // 入学时间 private String s_class; // 班级 private String subject_id = "";// 选择的题目 get{} set{}... }
?Dao类
package com.mongo.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Repository; import com.mongo.domain.Student; @Repository public class GraduateProjectDao { @Autowired private MongoTemplate mongoTemplate; //添加学生 public void addStudent(Student student){ mongoTemplate.insert(student); } }
?
测试:
package com.mongo.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.mongodb.core.MongoTemplate; import com.mongo.dao.GraduateProjectDao; import com.mongo.domain.Student; public class TestMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(ctx.getBean("mongoTemplate",MongoTemplate.class)); St