MongoDB 整合 Spring 简单demo
添加配置文件:spring-mongodb.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:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<!--连接mongodb-->
<mongo:mongo id="mongo" host="127.0.0.1" port="27017"></mongo:mongo>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"></constructor-arg>
<constructor-arg name="databaseName" value="test"></constructor-arg>
</bean>
</beans>
添加 pom.xml
<properties>
<spring.version>4.2.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
简单的demo测试
创建一个实体类Car
@Data
public class Car {
private String name;
private Date time;
private Integer price;
}
创建一个测试类
public class Demo {
private MongoTemplate mongoTemplate;
@Before
public void before(){
ClassPathXmlApplicationContext mo = new ClassPathXmlApplicationContext("classpath:spring-mongodb.xml");
mongoTemplate=(MongoTemplate) mo.getBean("mongoTemplate");
}
/**
* 新增
*/
@Test
public void test2(){
Car a = new Car();
a.setName("法拉利");
a.setTime(new Date());
a.setPrice(78);
mongoTemplate.save(a);
}
@Test
public void test(){
Car a = new Car();
a.setName("黑马");
a.setTime(new Date());
a.setPrice(66);
mongoTemplate.save(a);
}
/**
* 查询加分页
*/
@Test
public void test6(){
int page=1; //当前页
int rows=3; //每页条数
Query query = new Query();
query.skip((page-1)*rows).limit(rows);
List<Car> cars = mongoTemplate.find(query, Car.class);
System.out.println(cars);
}
/**
* 单条件查询
*/
@Test
public void test4(){
Query query = new Query();
Criteria where = new Criteria();
// where.and("price").lt(78); //查询价格小于78
//where.and("price").is(78);//查询价格是78
where.and("price").lte(78);//查询小于等于78
query.addCriteria(where);
List<Car> cars = mongoTemplate.find(query, Car.class);
System.out.println(cars);
}
/**
* 多条件查询
*/
@Test
public void test5(){
Query query = new Query();
Criteria where1= new Criteria();
where1.and("price").lte(78);//查询小于等于78
Criteria where2 = new Criteria();
where2.and("price").gte(50);//大于等于50
query.addCriteria(new Criteria().andOperator(where1,where2));
List<Car> cars = mongoTemplate.find(query, Car.class);
System.out.println(cars);
}
/**
* 修改
*/
@Test
public void test7(){
Query query = new Query();
Criteria where = new Criteria();
where.and("price").is(66);//修改价格等于66的
query.addCriteria(where);
Update update = new Update();
update.set("price",10);//重新赋值为10
mongoTemplate.updateFirst(query,update,Car.class);
//修改第一条符合的数据
// mongoTemplate.updateMulti(query,update,Car.class);//修改所有符合的数据
}
@Test
public void test8(){
Query query = new Query();
Criteria where = new Criteria();
where.and("price").is(10);//删除价格等于10 的
query.addCriteria(where);
mongoTemplate.remove(query,Car.class);
}
}