一、项目结构
二、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> <groupId>org.example</groupId> <artifactId>A02springboot_mongodb</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.9</source> <target>1.9</target> </configuration> </plugin> </plugins> </build> </project>
三、application.yml
server: port: 8080 spring: application: name: MongoDBServer data: mongodb: host: 127.0.0.1 database: student
四、启动类
package com.wuxi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class); } }
五、bean
package com.wuxi.bean; import lombok.Data; import org.springframework.data.annotation.Id; @Data public class Person { @Id private String _id; private String name; private Integer age; }
六、dao
package com.wuxi.dao; import com.wuxi.bean.Person; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; public interface PersonDao extends MongoRepository<Person, String> { Page<Person> findByNameLikeAndAgeBetween(String name, Integer begin, Integer end, Pageable pageable); }
七、service
package com.wuxi.service; import com.wuxi.bean.Person; import com.wuxi.dao.PersonDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; 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.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class PersonService { @Autowired private PersonDao personDao; @Autowired private MongoTemplate mongoTemplate; public Page<Person> pagefind(String name, Integer begin, Integer end, Pageable pageable) { return personDao.findByNameLikeAndAgeBetween(name, begin, end, pageable); } public Person findById(String id) { return personDao.findById(id).get(); } public void save(Person person) { personDao.save(person); } public void update(Person person) { personDao.save(person); } public void updateAge(String _id) { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(_id)); Update update = new Update(); update.inc("age", 1); mongoTemplate.updateFirst(query, update, "person"); } public void deleteById(String id) { personDao.deleteById(id); } }
八、controller
package com.wuxi.controller; import com.wuxi.bean.Person; import com.wuxi.service.PersonService; import com.wuxi.vo.Msg; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.*; import java.util.Map; import java.util.UUID; @RestController @CrossOrigin @RequestMapping("/mongo") public class PersonController { @Autowired private PersonService personService; @RequestMapping("/pagefind") public Msg pagefind(@RequestBody Map<String, Object> map) { PageRequest page = PageRequest.of(((Integer) map.get("pageNum")) - 1, (Integer) map.get("pageSize")); Page<Person> pageInfo = personService.pagefind( map.get("name") == null ? "" : (String) map.get("name"), map.get("begin") == null ? 0 : (Integer) map.get("begin"), map.get("end") == null ? 100 : (Integer) map.get("end"), page); return Msg.success().add("data", pageInfo); } @RequestMapping("/findById/{id}") public Msg findById(@PathVariable("id") String id) { return Msg.success().add("data", personService.findById(id)); } @RequestMapping("/save") public Msg save(@RequestBody Person person) { person.set_id(UUID.randomUUID().toString()); personService.save(person); return Msg.success().add("data", "添加成功"); } @RequestMapping("/update") public Msg update(@RequestBody Person person) { personService.update(person); return Msg.success().add("data", "修改成功"); } @RequestMapping("/updateAge/{id}") public Msg updateAge(@PathVariable("id") String _id) { personService.updateAge(_id); return Msg.success().add("data", "修改成功"); } @RequestMapping("/delete/{id}") public Msg delete(@PathVariable("id") String id) { personService.deleteById(id); return Msg.success().add("data", "删除成功"); } }
九、vo
package com.wuxi.vo; import java.util.HashMap; import java.util.Map; public class Msg { //状态吗 200-成功 400-失败 private int code; //提示信息 private String msg; //用户要返回给游览器的数据 private Map<String, Object> extend = new HashMap<String, Object>(); public static Msg success() { Msg result = new Msg(); result.setCode(200); result.setMsg("处理成功!"); return result; } public static Msg fail() { Msg result = new Msg(); result.setCode(400); result.setMsg("处理失败!"); return result; } //链式操作返回信息 public Msg add(String key, Object value) { this.getExtend().put(key, value); return this; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Map<String, Object> getExtend() { return extend; } public void setExtend(Map<String, Object> extend) { this.extend = extend; } }