1.Spring对MongoDB的支持 Spring对MongoDB的支持主要是通过Spring Data MongoDB实现的,Spring Data MongoDB为我们提供了如下功能。 1)对象/文档映射注解 2)MongoTemplate 与JdbcTemplate一样,Spring Data MongoDB也我们提供了一个MongoTemplate,而MongoTemplate为我们提供了数据访问的方法。 3)Repository 类似于Spring Data JPA,Spring Data MongoDB也我们提供了Repository的支持,使用方式和Spring Data JPA一样,示例如下: public interface PersonRepository extends MongoRepository<Person, String>{ }
Spring Boot对MongoDB的自动配置位于org.springframework.boot.autoconfigure.mongo包中。主要配置了数据库连接、MongoTemplate,我们可以在配置文件中使用以“spring.data.mongodb”为前缀的属性来配置MongoDB的相关信息。Spring Boot对MongoDB提供了一些默认属性,如默认端口号为27017、默认服务器为localhost、默认数据库为test、默认无用户名和无密码访问方式,并默认开启了对Repository的支持。因此,我们在Spring Boot应用中,只需引入spring-boot-starter-data-mongodb依赖即可按照默认配置操作MongoDB数据库。
在Spring Boot应用中,对MongoDB数据库的增删改查。 1.创建基于spring-boot-starter-data-mongodb依赖的Spring Boot Web应用ch6_8 2.配置application.properties文件 3.创建领域模型 4.创建数据访问接口 5.创建控制器层 6.运行
server.servlet.context-path=/ch6_8 spring.jackson.serialization.indent-output=true
<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>com.mongo</groupId> <artifactId>SpringbootMongodb</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!-- 声明项目配置依赖编码格式为 utf-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <fastjson.version>1.2.24</fastjson.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
package com.ch.ch6_8.domain; import java.util.ArrayList; import java.util.List; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; @Document public class Person { @Id private String pid; private String pname; private Integer page; private String psex; @Field("plocs") private List<Location> locations = new ArrayList<Location>(); public Person() { super(); } public Person(String pname, Integer page, String psex) { super(); this.pname = pname; this.page = page; this.psex = psex; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public String getPsex() { return psex; } public void setPsex(String psex) { this.psex = psex; } public List<Location> getLocations() { return locations; } public void setLocations(List<Location> locations) { this.locations = locations; } }
package com.ch.ch6_8.domain; public class Location { private String locName; private String year; public Location() { super(); } public Location(String locName, String year) { super(); this.locName = locName; this.year = year; } public String getLocName() { return locName; } public void setLocName(String locName) { this.locName = locName; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } }
package com.ch.ch6_8.repository; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query; import com.ch.ch6_8.domain.Person; public interface PersonRepository extends MongoRepository<Person, String> { Person findByPname(String pname);// 支持方法名查询 @Query("{'psex':?0}") // JSON字符串 List<Person> selectPersonsByPsex(String psex); }
package com.ch.ch6_8.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ch.ch6_8.domain.Location; import com.ch.ch6_8.domain.Person; import com.ch.ch6_8.repository.PersonRepository; @RestController public class TestMongoDBController { @Autowired private PersonRepository personRepository; @RequestMapping("/save") public List<Person> save() { List<Location> locations1 = new ArrayList<Location>(); Location loc1 = new Location("北京", "2019"); Location loc2 = new Location("上海", "2018"); locations1.add(loc1); locations1.add(loc2); List<Location> locations2 = new ArrayList<Location>(); Location loc3 = new Location("广州", "2017"); Location loc4 = new Location("深圳", "2016"); locations2.add(loc3); locations2.add(loc4); List<Person> persons = new ArrayList<Person>(); Person p1 = new Person("陈恒1", 88, "男"); p1.setLocations(locations1); Person p2 = new Person("陈恒2", 99, "女"); p2.setLocations(locations2); persons.add(p1); persons.add(p2); return personRepository.saveAll(persons); } @RequestMapping("/findByPname") public Person findByPname(String pname) { return personRepository.findByPname(pname); } @RequestMapping("/selectPersonsByPsex") public List<Person> selectPersonsByPsex(String psex) { return personRepository.selectPersonsByPsex(psex); } @RequestMapping("/updatePerson") public Person updatePerson(String oldPname, String newPname) { Person p1 = personRepository.findByPname(oldPname); if (p1 != null) p1.setPname(newPname); return personRepository.save(p1); } @RequestMapping("/deletePerson") public void updatePerson(String pname) { Person p1 = personRepository.findByPname(pname); personRepository.delete(p1); } }
package com.ch.ch6_8; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Ch68Application { public static void main(String[] args) { SpringApplication.run(Ch68Application.class, args); } }