一、添加依赖
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
二、配置application.properties
###########################mongodb开始####################################
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=db_test
###########################mongodb结束####################################
三、创建实体类
package com.example.demo.pojo;
import java.util.Date;
public class User {
private int id;
private String name;
private Date createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", createTime=" + createTime +
'}';
}
}
四、使用MongoTemplate实现
MongodbComponent.java
package com.example.demo.utils.component;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
@Component
public class MongodbComponent {
@Autowired
private MongoTemplate mongoTemplate;
public void insert(User user) {
mongoTemplate.insert(user);
}
public void deleteById(int id) {
Criteria criteria = Criteria.where("id").in(id);
Query query = new Query(criteria);
mongoTemplate.remove(query, User.class);
}
public void updateById(User user) {
Criteria criteria = Criteria.where("id").in(user.getId());
Query query = new Query(criteria);
Update update = new Update();
update.set("name", user.getName());
update.set("createTime", user.getCreateTime());
mongoTemplate.updateMulti(query, update, User.class);
}
public User selectById(int id) {
Criteria criteria = Criteria.where("id").in(id);
Query query = new Query(criteria);
return mongoTemplate.findOne(query, User.class);
}
}
MongoTemplateTest.java(测试)
package com.example.demo;
import com.example.demo.pojo.User;
import com.example.demo.utils.component.MongodbComponent;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
public class MongoTemplateTest extends ApplicationTests {
@Autowired
private MongodbComponent mongodbComponent;
@Test
public void insert() {
User user = new User();
user.setId(1);
user.setName("张三");
user.setCreateTime(new Date());
mongodbComponent.insert(user);
}
@Test
public void select() {
System.out.println(mongodbComponent.selectById(1));
}
@Test
public void update() {
User user = new User();
user.setId(1);
user.setName("李四");
user.setCreateTime(new Date());
mongodbComponent.updateById(user);
System.out.println(mongodbComponent.selectById(1));
}
@Test
public void delete() {
mongodbComponent.deleteById(1);
}
}
五、使用接口继承MongoRepository
UserDao.java
package com.example.demo.dao;
import com.example.demo.pojo.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface UserDao extends MongoRepository<User, Integer> {
List<User> findByName(String name);
Page<User> findByName(String name, Pageable pageable);
}
测试: