导入pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mr</groupId>
<artifactId>springboot-mongo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mongo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springboot mongo-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
#如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
#mongodb设置了密码,这样配置:
#spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname
server.port=80
创建实体类
@Data
public class Customer {
@Id
private String id;
private String name;
private Integer price ;
public Customer() {}
public Customer(String name, Integer price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, name='%s', price='%s']",
id, name, price);
}
第一种方式:
1. dao层继承 MongoRepository<实体类,主键类型>
public interface CustomerRepository extends MongoRepository<Customer, String> {
}
2.controller层
@Controller
public class MongoController {
@Autowired
private CustomerRepository repository;
//新增
@RequestMapping("add")
@ResponseBody
public String add() {
repository.insert(new Customer("zxf",100));
return "success-add";
}
//查询
@ResponseBody
@RequestMapping("find")
public List query(){
List<Customer> all = repository.findAll();
return all;
}
}
第二种方式(直接注入MongoTemplate)
@Controller
public class TemplateController {
@Autowired
MongoTemplate mongoTemplate;
//根据条件查询
@RequestMapping("query")
@ResponseBody
public List<Customer> query() {
Query query = new Query();
Criteria where = new Criteria();
where.and("name").is("zxf");
List<Customer> customers = mongoTemplate.find(query, Customer.class);
return customers;
}
//根据条件删除
@RequestMapping("del")
@ResponseBody
public String del(){
Query query = new Query();
Criteria where = new Criteria();
where.and("name").is("zxf");
/*where.all(new Customer());*/
query.addCriteria(where);
mongoTemplate.remove(query,Customer.class);
return "success-del";
}
}