搭建项目
创建Maven项目java-mongdb
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 https://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.6.3</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>java-mongdb</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!--mongodb--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--springMVC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
application.yml
server: port: 8080 spring: data: mongodb: host: 192.168.66.101 port: 27017 username: root password: root database: local authentication-database: admin
主启动类
@SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication.run(Main.class,args); } }
操作mongdb
录入数据
db.orders.insertMany( [ { _id: 0, name: "Pepperoni", size: "small", price: 19, quantity: 10, date: ISODate( "2030-03-13T08:14:30Z" ) }, { _id: 1, name: "Pepperoni", size: "medium", price: 20, quantity: 20, date : ISODate( "2030-03-13T09:13:24Z" ) }, { _id: 2, name: "Pepperoni", size: "large", price: 21, quantity: 30, date : ISODate( "2030-03-17T09:22:12Z" ) }, { _id: 3, name: "Cheese", size: "small", price: 12, quantity: 15, date : ISODate( "2030-03-13T11:21:39.736Z" ) }, { _id: 4, name: "Cheese", size: "medium", price: 13,quantity:50, date : ISODate( "2031-01-12T21:23:13.331Z" ) }, { _id: 5, name: "Cheese", size: "large", price: 14, quantity: 10, date : ISODate( "2031-01-12T05:08:13Z" ) }, { _id: 6, name: "Vegan", size: "small", price: 17, quantity: 10, date : ISODate( "2030-01-13T05:08:13Z" ) }, { _id: 7, name: "Vegan", size: "medium", price: 18, quantity: 10, date : ISODate( "2030-01-13T05:10:13Z" ) } ] )
MongoTemplate写法
public interface OrdersDao { //根据订单名称查询订单集合 List<Orders> findOrderByName(String name); }
@Repository public class OrdersDaoImpl implements OrdersDao { @Autowired protected MongoTemplate mongoTemplate; @Override public List<Orders> findOrderByName(String name) { Query query = new Query(); query.addCriteria(Criteria.where("name").is(name)); return mongoTemplate.find(query, Orders.class); } }
MongoRepository写法
public interface OrdersRepository extends MongoRepository<Orders,String> { List<Orders> findOrderBySize(String size); }
实体类
@Data @NoArgsConstructor public class Orders { private String _id; private String name; private String size; private Integer price; private Integer quantity; private Date date; }
服务层
public interface OrderService { //根据订单名称查询订单集合 List<Orders> getOrdersByName(String name); //根据大小查询订单集合 List<Orders> getOrdersBySize(String size); }
@Service public class OrderServiceImpl implements OrderService { @Autowired private OrdersDao ordersDao; @Autowired private OrdersRepository ordersRepository; @Override public List<Orders> getOrdersByName(String name) { return ordersDao.findOrderByName(name); } @Override public List<Orders> getOrdersBySize(String size) { return ordersRepository.findOrderBySize(size); } }
控制层
@RestController public class TestController { @Autowired private OrderService orderService; @GetMapping("/template/getOrdersByName") public List<Orders> getOrdersByName(String name) { return orderService.getOrdersByName(name); } @GetMapping("/repository/getOrdersBySize") public List<Orders> getOrdersBySize(String size) { return orderService.getOrdersBySize(size); } }
测试
MongoTemplate写法【dao】
http://localhost:8080/template/getOrdersByName?name=Pepperoni
编辑
MongoRepository写法【respository】
http://localhost:8080/repository/getOrdersBySize?size=large
编辑