1.配置文件 application.yml
server: port: 8086 spring: thymeleaf: cache: false data: mongodb: host: 182.61.10.117 port: 27017 database: test
2.实体类
@Data @Document public class User implements Serializable { @Id private String id; private String username; private Integer age; private double[] position;//位置信息 public User(String id, String username, Integer age) { this.id = id; this.username = username; this.age = age; } public User(String id,String username,Integer age, double lon, double lat) { this.id = id; this.username = username; this.age = age; double[] p = new double[]{lon, lat}; this.position = p; } }
3.默认方法jpa
public interface UserRepository extends MongoRepository<User,String> { /** * Like(模糊查询) * {"username" : name} ( name as regex) * */ List<User> findByUsernameLike(String username); /** * Like(模糊查询) * {"username" : name} * */ List<User> findByUsername(String username); /** * GreaterThan(大于) * {"age" : {"$gt" : age}} * */ List<User> findByAgeGreaterThan(int age); /** * LessThan(小于) * {"age" : {"$lt" : age}} * */ List<User> findByAgeLessThan(int age); /** * Between(在...之间) * {{"age" : {"$gt" : from, "$lt" : to}} * */ List<User> findByAgeBetween(int from, int to); /** * IsNotNull, NotNull(是否非空) * {"username" : {"$ne" : null}} * */ List<User> findByUsernameNotNull(); /** * IsNull, Null(是否为空) * {"username" : null} * */ List<User> findByUsernameNull(); /** * Not(不包含) * {"username" : {"$ne" : name}} * */ List<User> findByUsernameNot(String name); /** * Near(查询地理位置相近的) * {"location" : {"$near" : [x,y]}} * */ // findByLocationNear(Point point) /** * Within(在地理位置范围内的) * {"location" : {"$within" : {"$center" : [ [x, y], distance]}}} * */ /* List<User> findByLocationWithin(Circle circle);*/ /** * Within(在地理位置范围内的) * {"location" : {"$within" : {"$box" : [ [x1, y1], x2, y2]}}} * */ /* List<User> findByLocationWithin(Box box);*/ @Query("{\"username\":{\"$regex\":?0}, \"age\": ?1}") Page<User> findByNameAndAgeRang(String name, int age, Pageable page); }
4.测试
@RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class SpringBootMongoApplicationTests { @Autowired private UserRepository userRepository; @Autowired private MongoTemplate mongoTemplate; @Autowired private LocationRepository locationRepository; @Test public void contextLoads() { userRepository.save(new User("2L", "mama", 40)); userRepository.save(new User("3L", "kaka", 50)); userRepository.save(new User("4L", "didi2", 30)); userRepository.save(new User("5L", "mama", 40)); userRepository.save(new User("6L", "kaka2", 50)); userRepository.save(new User("7L", "kaka", 50)); userRepository.save(new User("8L", "kao", 50)); userRepository.save(new User("9L", "ekakae", 50)); userRepository.save(new User("10L", "kaka5", 50)); userRepository.save(new User("11L", "", 50)); userRepository.save(new User("12L", null, 50)); } @Test public void test(){ List<User> list = userRepository.findByUsernameLike("kaka"); log.info("名称是{}",list.toString()); } @Test public void test1(){ Pageable pageable = new PageRequest(0,5); Page<User> page = userRepository.findByNameAndAgeRang("kaka",50,pageable); log.info("名称是{}",page.getTotalElements()); } @Test public void init(){ mongoTemplate.indexOps(Location.class).ensureIndex(new GeospatialIndex("position")); // 初始化数据 mongoTemplate.save(new Location("天安门", 116.4041602659, 39.9096215780)); mongoTemplate.save(new Location("东单", 116.4244857287, 39.9144951360)); mongoTemplate.save(new Location("王府井", 116.4177807251, 39.9175129885)); mongoTemplate.save(new Location("西单", 116.3834863095, 39.9133467579)); mongoTemplate.save(new Location("复兴门", 116.3631701881, 39.9129554253)); mongoTemplate.save(new Location("复兴门", 116.3631701881, 39.9129554253)); mongoTemplate.save(new Location("西四", 116.3799838526, 39.9299098531)); mongoTemplate.save(new Location("菜市口", 116.3809950293, 39.8952009239)); mongoTemplate.save(new Location("东四", 116.4239387439, 39.9306126797)); } @Test public void test111(){ List<Location> list = locationRepository.findCircleNear(new Point(116.4041602659, 39.9096215780),3/111); list.forEach(location -> { System.out.println(location.toString()); log.info(location.toString()); }); } /** * 查找左下角是菜市口,右上角是东四,这个方形区域内的所有点 */ @Test public void findBoxNearTest() { Point point1 = new Point(116.3809950293, 39.8952009239); Point point2 = new Point(116.4239387439, 39.9306126797); List<Location> locations = locationRepository.findBoxWithin(point1, point2); locations.forEach(location -> { System.out.println(location.toString()); log.info(locations.toString()); }); } }