MongoTemplate 方式
依赖
<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 {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic 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 {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic 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");}@Testpublic void findAll() {List<Goods> all = mongoTemplate.findAll(Goods.class);all.forEach(item -> System.out.println(item.toString()));}@Testpublic 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());}@Testpublic void findBy() {// 匹配r结尾的数据Query query = new Query(where("item").regex("^.*r$"));List<Goods> goods = mongoTemplate.find(query, Goods.class);System.out.println(goods.toString());}@Testpublic 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);}@Testpublic 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);}
}
MongoRepository 方式
依赖&配置
同上。
实体类
// 关键注解:@Document,@Id
@Data
@Builder
@Document("goods")
public class Goods {@Idprivate 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 {@Resourceprivate GoodsRepository goodsRepository;@Testpublic 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);}
}