나는 또한이 기사에 대해 생각했는데, 우선, 내 기술은 좋지 않습니다. 그러나 나는 무언가를 공부하는 것을 좋아합니다. 많은 친구들 이이 전에 비슷한 친구들을 썼기 때문에 많은 친구들을 읽었지만 그 중 많은 사람들을 읽었지만 설명은 충분히 심도 없습니다. 일부 친구들이 제기 한 질문에 대한 답은 없습니다. 여기서 나는 현재의 능력을 기반으로 분류합니다. 그리고 마침내 성공적으로 실행됩니다.
테스트 중에 문제가 발생했습니다.
1. org/springframework/data/mapping/context/mappingcontextaware
2. SRC- 재산 : 이름 '저장소 : 저장소'라는 이름을 (n) '유형 정의'로 해결할 수 없습니다.
위의 모든 것은 버전 불일치로 인해 발생합니다. 특히 두 번째 오류는 JPA를 언급 한 솔루션 중 일부를 보았지만 여기서는 JPA를 사용하지 않았습니다. 나중에 Spring-Data-Commons 패키지를 교체했지만 나타나지 않았습니다.
내 개발 환경에 대해 먼저 이야기하겠습니다.
Myeclipse 6.5
Mongodb 2.0.8
봄 3.0.4
마지막으로, 다음 두 가지가 있습니다 (이 두 버전이 잘못되면 다양하고 복잡한 문제가 발생할 가능성이 높습니다). 여기서 내가 사용한 버전을 줄 것입니다
스프링-데이터-문서
스프링-다타 공동
모든 버전을 변경해야합니다. 다음은 JAR 다운로드 주소입니다
http://www.springsource.org/spring-data/mongodb
http://www.springsource.org/spring-data/commons
다운로드 된 버전은 다음과 같습니다.
스프링-다타-커먼즈 -Dist-1.4.0.m1
스프링-다타 문서 -1.0.0.m2.zip
내 프로젝트의 사진은 다음과 같습니다
그런 다음 개발 여행을 시작하십시오!
먼저 새 Application.xml 구성 파일을 만듭니다
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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://wwwww.sprampromegt. http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <mongo : mongo host ="192.168.0.138 "port ="27017 ""/> mong "" <생성자-arg ref = "mongo"/> <생성자-arg name = "databaseName"value = "db"/> <생성자-arg name = "defaultCollectionName"value = "person"/> </bean> <bean id = "Personrepositor"> <property name = "mongotemplate"</property = "mongotemplate" <컨텍스트 : 주석 -Config /> < /beans>
그런 다음 MongoDB를 작동하는 인터페이스를 작성하십시오
/ ** * AbstracTrepository.java */ package com.mongo.dao; Java.util.list 가져 오기; com.mongo.bean.person import; /** * todo * @author culan * @version todo */public Interface Abstractrepository {/** * * <b> 함수 : </b> 객체 추가 * @author culan * @Createdate 2012-12-12 11:41:30 */public void Insert (Person Perient); /** * * <b> 함수 : </b> id * @author culan * @createdate 2012-12 11:41:41 */public person findone (String id); /** * * <b> 함수 : </b> Query all * @author culan * @createdate 2012-12-12 16:26:06 */public list <person> findall (); 공개 목록 <person> findByRegex (String Regex); /** * * <b> 함수 : </b> 지정된 ID 객체를 삭제 * @Author Culan * @Createdate 2012-12 16:26:16 */public void removeOne (String ID); /** * * <b> 함수 : </b> 모든 삭제 * @author culan * @Createdate 2012-12-12 16:25:40 */public void removeall (); /** * id * <b> 기능으로 찾아서 수정합니다. } 해당 인터페이스 구현 클래스 작성 :
/ ** * personrepository.java */ package com.mongo.dao.impl; Java.util.list 가져 오기; java.util.regex.pattern import; org.springframework.data.document.mongodb.mongotemplate; org.springframework.data.document.mongodb.query.criteria import; org.springframework.data.document.mongodb.query.update; org.springframework.data.document.mongodb.query.update; com.mongo.bean.person import; import com.mongo.dao.abstractrepository; / ** * todo * @author culan * @version todo */ public class personrepositority 구현 Abstractrepository {private mongotemplate mongotemplate; / * (nonjavadoc) * @see com.mongo.dao.abstractrepository#findall () */ @override public list <person> findall () {// todo 자동 생성 메소드 스터브 리턴 getMongotemplate (). find (new query (), person.class); } / * (nonjavadoc) * @see com.mongo.dao.bstractrepository#findandmodify (java.lang.string) * /@override public void findandmodify (string id) {// new query ( "id"), new (id)).) getMongotemplate (). updateFirst (new Query (incritia.where ( "id"). is (id)), new update (). Inc ( "age", 3)); } / * (nonjavadoc) * @see com.mongo.dao.bstractrepository#findByRegex (java.lang.string) * / @override public list <person> findByRegex (String regex) {// to auto-generated method 패턴 패턴 = PatternPile (Regex, Pattern.Scase); 기준 기준 = 새로운 기준 ( "이름"). regex (pattern.toString ()); let } / * (nonjavadoc) * @see com.mongo.dao.abstractrepository#findone (java.lang.string) * / @override public person findone (string id) {// todo auto-enterated method stub return getMongotemplate (). } / * (nonjavadoc) * @see com.mongo.dao.abstractrepository#insert (com.mongo.bean.person) * / @override public void insert (Person Person) {// a auto-auto-rened method stub getmongotemplate (). insert (person); } / * (nonjavadoc) * @see com.mongo.dao.bstractrepository#removeall () * / @override public void removeall () {// todo auto-auto-renated method stub list = this.findall (); if (list! = null) {for (person : list) {getMongotemplate (). 제거 (person); }}} / * (nonjavadoc) * @see com.mongo.dao.abstractrepository#removeon (java.lang.string) * / @override public void removeon (String id) {// auto-auto-regended method criteria = criteria. ( "id"); if (criteria == null) {query query = new query (criteria); if (query! = null && getMongotemplate (). findone (query, person.class)! = null) getMongoTemplate (). 제거 }} / ** * @Mongotemplate * / public mongotemplate getMongotemplate () {return mongotemplate; } / ** * @param mongotemplate * / public void setMongotemplate (mongotemplate mongotemplate) {this.mongotemplate = mongotemplate; }} 해당 개인 객체 코드도 여기에 제공됩니다
/ ** * person.java */ package com.mongo.bean; java.io.serializable import; / ** * todo * @author culan * @version todo */ public class personements serializable {/ ** */ private static final long serialversionuid = 3617931430808763429L; 개인 문자열 ID; 개인 문자열 이름; 사적인 int 연령; 공개 사람 () {super (); } public person (문자열 ID, 문자열 이름, int age) {super (); this.id = id; this.name = 이름; this.age = age; } / ** * @return id * / public string getId () {return id; } / ** * @param id 설정에 대한 ID * / public void setId (String id) {this.id = id; } / ** * @return 이름 * / public string getName () {return name; } / ** * @param 이름 설정 * / public void setName (문자열 이름) {this.name = name; } / ** * @Age * / Public int getage () {return age; } / ** * @Param Age는 설정 할 연령 * / public void setage (int age) {this.age = age; } / ** * * @param name * @param age * / public person (문자열 이름, int age) {super (); this.name = 이름; this.age = age; } public String toString () {return "person [id ="+id+", name ="+name+", age ="+age+"]; }} 마지막으로 테스트 클래스를 작성하고 테스트를 시작하십시오
/ ** * mongotest.java */ package com.mongo.test; Java.util.list 가져 오기; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.context.applicationcontext; import org.springframework.context.support.classPathXmlApplicationContext; com.mongo.bean.person import; import com.mongo.dao.abstractrepository; com.mongo.dao.impl.personrepository import; / ** * todo * @author culan * @version todo */ public class mongotest {private static log = logfactory.getLog (mongotest.class.getName ()); 개인 Abstractrepository pr = null; /** * * <b> 함수 : </b> * @Author Culan * @Createdate 2012-12-12 16:08:02 */public void init () {log.debug ( "시작 시작"); ApplicationContext CTX = 새 ClassPathXmlApplicationContext ( "ApplicationContext.xml"); pr = (PersonRepository) ctx.getBean ( "PersonRepository"); }/** * * <b> 함수 : </b> add * @author culan * @createdate 2012-12-12 16:11:01 */public void insert () {person p = new Person ( "curian", 27); pr.insert (p); log.debug ( "성공적으로 추가"); }/** * * <b> 함수 : </b> 입력 ID * @Author Culan * @Createdate 2012-12-12 16:24:10 */public void findOne () {String id = "50C83CB552C2C2C2CEB0463177D6"; Person P = Pr.Findone (ID); log.debug (p); }/** * * <b> 함수 : </b> Query all * @Author Culan * @Createdate 2012-12 16:08:54 */public void listall () {list <person> list = pr.findall (); log.debug ( "쿼리 결과는 다음과 같습니다."); for (person p : list) {log.debug (p.tostring ()); }}/** * * <b> 함수 : </b> 테스트 방법 * @Author Culan * @Createdate 2012-12-12 16:11:37 */public void start () {init (); //끼워 넣다(); // listAll (); findone (); }/** * <b> 함수 : </b> 주요 함수 * @author culan * @createdate 2012-12 11:54:30 */public static void main (String [] args) {// todo auto-auto-renated method stub mongotest t = new mongotest (); t.start (); }}실행 후 로그가 나타나면 아무런 문제가 없습니다.
2012-12 16:23:59:DEBUG com.mongo.test.MongoTest - Start Start 2012-12-12 16:23:59:INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@253498: startup date [Wed Dec 12 16:23:59 CST 2012]; 컨텍스트의 루트 2012-12 16 : 23 : 59 : info org.springframework.beans.beans.xml.xml.xml.xmlbeandefinitionReader- 클래스 경로 리소스에서 XML bean 정의로드 [ApplicationContext.xml] 2012-12-12 16 : 24 : 00 : info org.springframework.bean.support.spraultlector. org.springframework.bean.beans.support.defaultlistablebeanfactory@12a0f6c의 싱글 톤 전 싱글 톤 : 콩 정의 [Mongo, Mongotemplate, Personreepository, org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalautowi RedAnnotationProcessor, org.springframework.context.annotation.internalrequiredannotationProcessor, org.springframework.context.annotation.internalcommonAnnotationProcessor]; 공장 계층의 근본 2012-12-12 16 : 24 : 00 : Debug com.mongo.test.mongotest- 사람 [ID = 50C83CB552C2C2C2C2C2C2C2C2C2C2C2C2C2C2C20463177D6, 이름 = Cuiran, Age = 27]
데모 소스 코드를 여기에 첨부하십시오 : 데모
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.