<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
# 切换到数据库
use study
# 创建账户
db.createUser({user:"root",pwd:"123456",roles:[{role:"dbOwner",db:"study"}]})
spring.data.mongodb.host=192.168.204.156
spring.data.mongodb.port=27017
spring.data.mongodb.database=study
spring.data.mongodb.password=123456
spring.data.mongodb.username=root
@SpringBootTest(classes = {SpringbootMongodbDemoApplication.class})
public class Main {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void testAddData() {
// 添加
Goods goods = Goods.builder().qty(100).item("hello").status("B").size(new Size(165.0F, 53.4F, "cm")).build();
mongoTemplate.save(goods, "goods");
// 查询
List<Goods> list = mongoTemplate.findAll(Goods.class);
list.forEach(item -> System.out.println(item.toString()));
}
}
@Data
@Builder
public class Goods {
private String id;
private String item;
private Integer qty;
private Size size;
private String status;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Size {
private Float h;
private Float w;
private String uom;
}
import com.example.springbootmongodbdemo.SpringbootMongodbDemoApplication;
import com.example.springbootmongodbdemo.model.Goods;
import com.example.springbootmongodbdemo.model.Size;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@SpringBootTest(classes = {SpringbootMongodbDemoApplication.class})
public class Main {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void addData() {
Goods build = Goods.builder().qty(100).item("hello").status("B").size(new Size(165.0F, 53.4F, "cm")).build();
mongoTemplate.save(build, "goods");
}
@Test
public void findAll() {
List<Goods> all = mongoTemplate.findAll(Goods.class);
all.forEach(item -> System.out.println(item.toString()));
}
@Test
public void findById() {
Goods build = Goods.builder().id("63bbdac2fa9405a15160f476").build();
Query query = new Query(where("id").is(build.getId()));
List<Goods> goods = mongoTemplate.find(query, Goods.class);
System.out.println(goods.toString());
}
@Test
public void findBy() {
// 匹配r结尾的数据
Query query = new Query(where("item").regex("^.*r$"));
List<Goods> goods = mongoTemplate.find(query, Goods.class);
System.out.println(goods.toString());
}
@Test
public void update() {
Goods build = Goods.builder().id("63bbdac2fa9405a15160f476").build();
Query query = new Query(where("id").is(build.getId()));
Update update = Update.update("item", "canvas02");
UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Goods.class);
System.out.println(updateResult);
}
@Test
public void del() {
Goods build = Goods.builder().id("63d0eff1d20d7f153665244d").build();
Query query = new Query(where("id").is(build.getId()));
DeleteResult remove = mongoTemplate.remove(query, Goods.class);
System.out.println(remove);
}
}
同上。
// 关键注解:@Document,@Id
@Data
@Builder
@Document("goods")
public class Goods {
@Id
private String id;
private String item;
private Integer qty;
private Size size;
private String status;
}
public interface GoodsRepository extends MongoRepository<Goods, String> {
}
@SpringBootTest(classes = {SpringbootMongodbDemoApplication.class})
public class Main {
@Resource
private GoodsRepository goodsRepository;
@Test
public void testAddData() {
Goods build = Goods.builder().qty(100).item("hello").status("B").size(new Size(165.0F, 53.4F, "cm")).build();
Goods save = goodsRepository.save(build);
System.out.println(save);
}
}