《springBoot系列(集成mongodb)》要点:
本文介绍了springBoot系列(集成mongodb),希望对您有用。如果有疑问,可以联系我们。
《springBoot系列(集成mongodb)》是否对您有启发,欢迎查看更多与《springBoot系列(集成mongodb)》相关教程,学精学透。编程之家PHP学院为您提供精彩教程。
一、添加对应的依赖
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
二、配置属性文件application.properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27018
spring.data.mongodb.database=mongodb_test
三、创建实体类
package com.example.demo.pojo;
import java.util.Date;
public class Person {
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 "Person{" +
"id=" + id +
",name='" + name + '\'' +
",createTime=" + createTime +
'}';
}
}
四、使用MongoTemplate实现
package com.example.demo.utils.component;
import com.example.demo.pojo.Person;
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(Person p) {
mongoTemplate.insert(p);
}
public void deleteById(int id) {
Criteria criteria = Criteria.where("id").in(id);
Query query = new Query(criteria);
mongoTemplate.remove(query,Person.class);
}
public void updateById(Person p) {
Criteria criteria = Criteria.where("id").in(p.getId());
Query query = new Query(criteria);
Update update = new Update();
update.set("name",p.getName());
update.set("createTime",p.getCreateTime());
mongoTemplate.updateMulti(query,update,Person.class);
}
public Person selectById(int id) {
Criteria criteria = Criteria.where("id").in(id);
Query query = new Query(criteria);
return mongoTemplate.findOne(query,Person.class);
}
}
package com.example.demo;
import com.example.demo.pojo.Person;
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() {
Person p = new Person();
p.setId(1);
p.setName("哈哈");
p.setCreateTime(new Date());
mongodbComponent.insert(user);
}
@Test
public void select() {
System.out.println(mongodbComponent.selectById(1));
}
@Test
public void update() {
Person p = new Person();
p.setId(1);
p.setName("李四");
p.setCreateTime(new Date());
mongodbComponent.updateById(p);
System.out.println(mongodbComponent.selectById(1));
}
@Test
public void delete() {
mongodbComponent.deleteById(1);
}
}