SpringBoot中简单使用mongodb
配置application.properties 应该放最前面
#单机
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
#集群 还没测试
spring.data.mongodb.uri=mongodb://ip1:port1,ip2:port2/database
1. 构建一个entity class: Movie
import org.springframework.data.annotation.Id;
public class Movie {
@Id
private String id;
private String name;
private String title;
public Movie(String id, String name, String title) {
this.id = id;
this.name = name;
this.title = title;
}
public Movie() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Movie{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", title='" + title + '\'' +
'}';
}
}
实体类Movie 映射到mongodb 中某一个Collection中某一个具体Document, 其中
@Id表示 对应Document中的主键,也就是objectId
ref: https://spring.io/guides/gs/accessing-data-mongodb/
MongoDB stores data in collections. Spring Data MongoDB will map the
class Movie into a collection called Movie. If you want to change
the name of the collection, you can use Spring Data MongoDB’s @Document annotation on the class.
2. 在mapper/dao层定义一个接口,访问mongodb
import com.zhou.spring.springboot.sudy1.model.Movie;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface MovieRepository extends MongoRepository<Movie, String> {
public Movie findByName(String name);
@Override
Movie findOne(String s);
}
MongoRepository<T, ID extends Serializable>, T对应实体类, TD表示对应主键id
3. service 层可以再封装一层, 根据具体业务进行进一步处理
创建class MovieService
import com.zhou.spring.springboot.sudy1.mapper.MovieRepository;
import com.zhou.spring.springboot.sudy1.model.Movie;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class MovieService {
@Resource
MovieRepository movRes;
public Movie findByName(String name) {
return movRes.findByName( name );
}
public Movie findById(String id) {
return movRes.findOne(id );
}
public List<Movie> findMoveList( ){
return movRes.findAll();
}
}
4. 测试
import com.zhou.spring.springboot.sudy1.model.Movie;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sudy1ApplicationTests {
@Autowired
private MovieRepository movRes;
@Test
public void test() throws Exception {
// 创建三个User,并验证User总数
movRes.save(new Movie("1", "fast", "fast") );
Movie m2 = new Movie("2", "faster", "faster");
movRes.save( m2 );
//Assert.assertEquals(3, movRes.findAll().size());
Assert.assertEquals( m2.getId(), movRes.findByName( "faster" ).getId());
Assert.assertEquals( m2.getName(), movRes.findByName( "faster" ).getName());
Assert.assertEquals( m2.getTitle(), movRes.findByName( "faster" ).getTitle());
}
@Test
public void test2() throws Exception {
// 创建三个User,并验证User总数
Movie mov = movRes.findOne( "5982e265468ac80e8f771b02") ;
System.out.println( mov );
}
}
5. controller层可定义接口, 通过web端对数据进行远程访问
@RestController
public class QueryMovie {
@Resource
private MovieService movieService;
//http://localhost:8080/api/hello
@RequestMapping("/api/hello")
public String index() {
return "Hello World!!The World is beautiful!!!!!!!";
}
//http://localhost:8080/api/getMovieById?id=1
//curl http://localhost:8080/api/getUserById?id=1&name=xiaohong
@RequestMapping(value = "/api/getMovieById",
method={RequestMethod.POST, RequestMethod.GET} )
public Movie getMoveById(@RequestParam String id ) {
return movieService.findById( id );
}
//http://localhost:8080/api/getMovieByName?name=fast
//curl -d "name=xiaohong&id=1" "http://localhost:8080/api/getUserByName"
@RequestMapping(value = "/api/getMovieByName",
method={RequestMethod.POST, RequestMethod.GET} )
public Movie getMoveByName(@RequestParam String name ) {
return movieService.findByName( name );
}
//http://localhost:8080/api/findAllMovies
@RequestMapping(value = "/api/findAllMovies",
method={RequestMethod.POST, RequestMethod.GET} )
public List<Movie> findAllUsers( ) {
return movieService.findMoveList();
}
}
配置application.properties 应该放最前面
#单机
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
#集群 还没测试
spring.data.mongodb.uri=mongodb://ip1:port1,ip2:port2/database
1. 构建一个entity class: Movie
import org.springframework.data.annotation.Id;
public class Movie {
@Id
private String id;
private String name;
private String title;
public Movie(String id, String name, String title) {
this.id = id;
this.name = name;
this.title = title;
}
public Movie() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Movie{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", title='" + title + '\'' +
'}';
}
}
实体类Movie 映射到mongodb 中某一个Collection中某一个具体Document, 其中
@Id表示 对应Document中的主键,也就是objectId
ref: https://spring.io/guides/gs/accessing-data-mongodb/
MongoDB stores data in collections. Spring Data MongoDB will map the
class Movie into a collection called Movie. If you want to change
the name of the collection, you can use Spring Data MongoDB’s @Document annotation on the class.
2. 在mapper/dao层定义一个接口,访问mongodb
import com.zhou.spring.springboot.sudy1.model.Movie;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface MovieRepository extends MongoRepository<Movie, String> {
public Movie findByName(String name);
@Override
Movie findOne(String s);
}
MongoRepository<T, ID extends Serializable>, T对应实体类, TD表示对应主键id
3. service 层可以再封装一层, 根据具体业务进行进一步处理
创建class MovieService
import com.zhou.spring.springboot.sudy1.mapper.MovieRepository;
import com.zhou.spring.springboot.sudy1.model.Movie;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class MovieService {
@Resource
MovieRepository movRes;
public Movie findByName(String name) {
return movRes.findByName( name );
}
public Movie findById(String id) {
return movRes.findOne(id );
}
public List<Movie> findMoveList( ){
return movRes.findAll();
}
}
4. 测试
import com.zhou.spring.springboot.sudy1.model.Movie;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sudy1ApplicationTests {
@Autowired
private MovieRepository movRes;
@Test
public void test() throws Exception {
// 创建三个User,并验证User总数
movRes.save(new Movie("1", "fast", "fast") );
Movie m2 = new Movie("2", "faster", "faster");
movRes.save( m2 );
//Assert.assertEquals(3, movRes.findAll().size());
Assert.assertEquals( m2.getId(), movRes.findByName( "faster" ).getId());
Assert.assertEquals( m2.getName(), movRes.findByName( "faster" ).getName());
Assert.assertEquals( m2.getTitle(), movRes.findByName( "faster" ).getTitle());
}
@Test
public void test2() throws Exception {
// 创建三个User,并验证User总数
Movie mov = movRes.findOne( "5982e265468ac80e8f771b02") ;
System.out.println( mov );
}
}
5. controller层可定义接口, 通过web端对数据进行远程访问
@RestController
public class QueryMovie {
@Resource
private MovieService movieService;
//http://localhost:8080/api/hello
@RequestMapping("/api/hello")
public String index() {
return "Hello World!!The World is beautiful!!!!!!!";
}
//http://localhost:8080/api/getMovieById?id=1
//curl http://localhost:8080/api/getUserById?id=1&name=xiaohong
@RequestMapping(value = "/api/getMovieById",
method={RequestMethod.POST, RequestMethod.GET} )
public Movie getMoveById(@RequestParam String id ) {
return movieService.findById( id );
}
//http://localhost:8080/api/getMovieByName?name=fast
//curl -d "name=xiaohong&id=1" "http://localhost:8080/api/getUserByName"
@RequestMapping(value = "/api/getMovieByName",
method={RequestMethod.POST, RequestMethod.GET} )
public Movie getMoveByName(@RequestParam String name ) {
return movieService.findByName( name );
}
//http://localhost:8080/api/findAllMovies
@RequestMapping(value = "/api/findAllMovies",
method={RequestMethod.POST, RequestMethod.GET} )
public List<Movie> findAllUsers( ) {
return movieService.findMoveList();
}
}
ref: https://spring.io/guides/gs/accessing-data-mongodb/
http://blog.didispace.com/springbootmongodb/
http://blog.scottlogic.com/2016/11/22/spring-boot-and-mongodb.html
http://www.jianshu.com/p/fe3b9532b852
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html
http://www.ityouknow.com/springboot/2017/05/08/springboot-mongodb.html