今天我们来学习一下SpringData操作MongoDB。 项目环境:IntelliJ IDEA2017+maven3.5.0+MongoDB 3.2+JDK1.7+spring4.3.8
推荐网站(适合学习各种知识的基础):http://www.runoob.com/
mongo安装请参考:http://www.runoob.com/mongodb/mongodb-window-install.html
1. 创建maven工程 首相创建maven工程,New project:
这里不使用idea自带maven插件,改用下载好的3.5.0版maven;
由于最近osChina的maven仓库挂掉,推荐大家使用阿里的镜像,速度快的飞起maven
配置:D:\apache-maven-3.5.0\conf\setting.xml
中找到mirrors
:
1 2 3 4 5 6 7 8 <mirrors > <mirror > <id > alimaven</id > <name > aliyun maven</name > <url > http://maven.aliyun.com/nexus/content/groups/public/</url > <mirrorOf > central</mirrorOf > </mirror > </mirrors >
项目结构如下图,手动创建相关文件夹及文件,并设置文件夹的对应属性:
至此,maven工程创建完毕。
2. 访问mongodb数据方式 这里dao
与mongoDao
分别为mongoDB
的两种查询方式:
(1) dao
为JPA
的查询方式(请参考springdataJPA
);
(2) mongoDao
使用mongoTemplate
,类似于关系型数据库使用的jdbcTemplate
。
3. 详细代码 先看配置文件 spring-context.xml为最基本的spring
配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?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" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:annotation-config /> <context:component-scan base-package ="com.lewis.mongo" > <context:include-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <import resource ="spring-mongo.xml" /> </beans >
spring-web.xml为springmvc
的基本配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <?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:mvc ="http://www.springframework.org/schema/mvc" xmlns:context ="http://www.springframework.org/schema/context" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" > <context:annotation-config /> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="viewClass" value ="org.springframework.web.servlet.view.JstlView" /> <property name ="prefix" value ="/WEB-INF/views/" /> <property name ="suffix" value =".jsp" /> </bean > <context:component-scan base-package ="com.lewis.mongo.controller" /> <mvc:view-controller path ="/" view-name ="redirect:/hi/hello" /> </beans >
spring-mongo.xml为mongo配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?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.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd" > <context:property-placeholder location ="classpath*:mongo.properties" /> <mongo:mongo-client replica-set ="${mongo.hostport}" id ="mongo" > <mongo:client-options connections-per-host ="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier ="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout ="${mongo.connectTimeout}" max-wait-time ="${mongo.maxWaitTime}" socket-timeout ="${mongo.socketTimeout}" /> </mongo:mongo-client > <mongo:db-factory id ="mongoDbFactory" dbname ="mongoLewis" mongo-ref ="mongo" /> <bean id ="mongoTemplate" class ="org.springframework.data.mongodb.core.MongoTemplate" > <constructor-arg name ="mongoDbFactory" ref ="mongoDbFactory" /> </bean > <mongo:repositories base-package ="com.lewis.mongo" /> </beans >
mongo.properties:
1 2 3 4 5 6 7 8 9 10 11 12 mongo.hostport =10.10.16.234:27017 mongo.connectionsPerHost =8 mongo.threadsAllowedToBlockForConnectionMultiplier =4 mongo.connectTimeout =1000 mongo.maxWaitTime =1500 mongo.autoConnectRetry =true mongo.socketKeepAlive =true mongo.socketTimeout =1500 mongo.slaveOk =true
pom.xml,这里要注意的是junit
版本需要4.12以上,不然idea会报错;spring-data-mongodb
版本要1.10.1以上;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <properties > <spring.version > 4.3.8.RELEASE</spring.version > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > </properties > <groupId > com.liu</groupId > <artifactId > mongo</artifactId > <version > 1.0-SNAPSHOT</version > <packaging > war</packaging > <name > mongo Maven Webapp</name > <url > http://maven.apache.org</url > <dependencies > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > <scope > test</scope > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > 1.7.12</version > </dependency > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-core</artifactId > <version > 1.1.1</version > </dependency > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-classic</artifactId > <version > 1.1.1</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.22</version > <scope > runtime</scope > </dependency > <dependency > <groupId > jstl</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.5.4</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-core</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-asm</artifactId > <version > 3.1.4.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-beans</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > org.springframework.data</groupId > <artifactId > spring-data-mongodb</artifactId > <version > 1.10.1.RELEASE</version > </dependency > <dependency > <groupId > org.mongodb</groupId > <artifactId > mongo-java-driver</artifactId > <version > 3.2.2</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-tx</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aop</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-test</artifactId > <version > 4.3.8.RELEASE</version > </dependency > <dependency > <groupId > commons-collections</groupId > <artifactId > commons-collections</artifactId > <version > 3.2.2</version > </dependency > <dependency > <groupId > commons-fileupload</groupId > <artifactId > commons-fileupload</artifactId > <version > 1.3.2</version > </dependency > <dependency > <groupId > commons-codec</groupId > <artifactId > commons-codec</artifactId > <version > 1.10</version > </dependency > <dependency > <groupId > javax</groupId > <artifactId > javaee-api</artifactId > <version > 7.0</version > <scope > provided</scope > </dependency > </dependencies > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-framework-bom</artifactId > <version > ${spring.version}</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > net.sf.ehcache</groupId > <artifactId > ehcache-core</artifactId > <version > 2.6.9</version > </dependency > </dependencies > </dependencyManagement > <build > <finalName > mongo</finalName > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 3.3</version > <configuration > <source > 1.6</source > <target > 1.6</target > </configuration > </plugin > </plugins > </build > </project >
两个实体类:
Address.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package com.lewis.mongo.entity;public class Address { private String city; private String street; private int num; public Address () { } public Address (String city, String street, int num) { this .city = city; this .street = street; this .num = num; } public String getCity () { return city; } public void setCity (String city) { this .city = city; } public String getStreet () { return street; } public void setStreet (String street) { this .street = street; } public int getNum () { return num; } public void setNum (int num) { this .num = num; } @Override public String toString () { return "Address{" + "city='" + city + '\'' + ", street='" + street + '\'' + ", num=" + num + '}' ; } }
Person.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 package com.lewis.mongo.entity;import java.io.Serializable;import org.bson.types.ObjectId;import org.springframework.data.annotation.Id;import org.springframework.data.mongodb.core.mapping.Document;@Document(collection = "person") public class Person implements Serializable { @Id private ObjectId id; private String name; private int age; private Address address; public Person () { } public Person (String name, int age, Address address) { this .name = name; this .age = age; this .address = address; } public ObjectId getId () { return id; } public void setId (ObjectId id) { this .id = id; } 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 Address getAddress () { return address; } public void setAddress (Address address) { this .address = address; } @Override public String toString () { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", address=" + address + '}' ; } }
JPA的dao,注意这里只要继承MongoRepository不用写注解spring就能认识这是个Repository,MongoRepository提供了基本的增删改查,不用实现便可直接调用,例如testMongo的personDao.save(persons);
PersonDao.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.lewis.mongo.dao;import com.lewis.mongo.entity.Person;import org.bson.types.ObjectId;import org.springframework.data.mongodb.repository.MongoRepository;import org.springframework.data.mongodb.repository.Query;import java.util.List;public interface PersonDao extends MongoRepository <Person , ObjectId > { @Query(value = "{'age' : {'$gte' : ?0, '$lte' : ?1}, 'name' : ?2}", fields = "{'name' : 1, 'age' : 1}") List<Person> findByAge (int age1, int age2, String name) ; }
mongoTemplate的dao,PersonMongoDao.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.lewis.mongo.mongoDao;import com.lewis.mongo.entity.Person;import java.util.List;public interface PersonMongoDao { List<Person> findAll () ; void insertPerson (Person user) ; void removePerson (String userName) ; void updatePerson () ; List<Person> findForRequery (String userName) ; }
PersonMongoImpl.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.lewis.mongo.mongoDao;import com.lewis.mongo.entity.Person;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.stereotype.Repository;import javax.annotation.Resource;import java.util.List;@Repository("personMongoImpl") public class PersonMongoImpl implements PersonMongoDao { @Resource private MongoTemplate mongoTemplate; @Override public List<Person> findAll () { return mongoTemplate.findAll(Person.class, "person" ); } @Override public void insertPerson (Person person) { mongoTemplate.insert(person, "person" ); } @Override public void removePerson (String userName) { mongoTemplate.remove(Query.query(Criteria.where("name" ).is(userName)), "person" ); } @Override public void updatePerson () { mongoTemplate.updateMulti(Query.query(Criteria.where("age" ).gt(3 ).lte(5 )), Update.update("age" , 3 ), "person" ); } @Override public List<Person> findForRequery (String userName) { return mongoTemplate.find(Query.query(Criteria.where("name" ).is(userName)), Person.class); } }
JPA查询的测试类,PersonDaoTest.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import com.lewis.mongo.dao.PersonDao;import com.lewis.mongo.entity.Address;import com.lewis.mongo.entity.Person;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;import java.util.ArrayList;import java.util.List;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring/spring-context.xml", "classpath:spring/spring-mongo.xml"}) public class PersonDaoTest { @Resource private PersonDao personDao; @Test public void testMongo () { List<Person> persons = new ArrayList<Person>(); for (int i = 0 ; i < 10 ; i++) { persons.add(new Person("name" + i, i, new Address("广州市" , "天河区" , i))); } personDao.save(persons); } @Test public void findMongo () { System.out.println(personDao.findByAge(2 , 8 , "name6" )); } }
mongoTemplate查询的测试类,MongoTemplateTest.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import com.lewis.mongo.entity.Address;import com.lewis.mongo.entity.Person;import com.lewis.mongo.mongoDao.PersonMongoImpl;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring/spring-context.xml", "classpath:spring/spring-mongo.xml"}) public class MongoTemplateTest { @Resource private PersonMongoImpl personMongo; @Test public void testMongoTemplate () { personMongo.removePerson("name3" ); personMongo.updatePerson(); System.out.println(personMongo.findAll()); System.out.println(personMongo.findForRequery("Lewis" )); } }
注意测试前请先通过PersonDaoTest.java
中的testMongo()
方法向数据库中插入数据。