环境 springboot 结合 monggodb
一、pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
配置:
spring.data.mongodb.uri=mongodb://localhost:27017/pglyon
二、实体类 Users.java
import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.index.CompoundIndex; import org.springframework.data.mongodb.core.index.CompoundIndexes; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection="users") @CompoundIndexes({ @CompoundIndex(name = "age_idx", def = "{'name': 1, 'age': -1}") }) public class Users implements Serializable{ private static final long serialVersionUID = 1L; @Indexed private String uid; private String name; private int age; @Transient private String address; public Users(String uid, String name, int age) { super(); this.uid = uid; this.name = name; this.age = age; }
注解说明
@Document
标注在实体类上,与hibernate异曲同工。
@Document(collection="users") public class Users implements Serializable{ private static final long serialVersionUID = 1L; ...省略代码
@CompoundIndex
复合索引,加复合索引后通过复合索引字段查询将大大提高速度。
@Document(collection="users") @CompoundIndexes({ @CompoundIndex(name = "age_idx", def = "{'name': 1, 'age': -1}") }) public class Users implements Serializable{ private static final long serialVersionUID = 1L; ...省略代码
@Id
MongoDB默认会为每个document生成一个 _id 属性,作为默认主键,且默认值为ObjectId,可以更改 _id 的值(可为空字符串),但每个document必须拥有 _id 属性。
当然,也可以自己设置@Id主键,不过官方建议使用MongoDB自动生成。
@Indexed
声明该字段需要加索引,加索引后以该字段为条件检索将大大提高速度。
唯一索引的话是@Indexed(unique = true)。
也可以对数组进行索引,如果被索引的列是数组时,mongodb会索引这个数组中的每一个元素。
@Indexed private String uid;
@Transient
被该注解标注的,将不会被录入到数据库中。只作为普通的javaBean属性。
@Transient private String address;
生成的集合 以及结构
三、业务接口 UserService.java:
public interface UserService { public void saveUsers(List<Users> users); public void saveUser(Users users); public Users findUserByName(String name); public void removeUser(String name); public void updateUser(String name, String key, String value); public List<Users> listUser(); }
实现类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; 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 com.lyon.mongo.entity.Users; import com.lyon.mongo.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired MongoOperations mongoTemplate; @Override public void saveUser(Users users) { mongoTemplate.save(users); } @Override public Users findUserByName(String name) { return mongoTemplate.findOne( new Query(Criteria.where("name").is(name)), Users.class); } @Override public void removeUser(String name) { mongoTemplate.remove( new Query(Criteria.where("name").is(name)), Users.class); } @Override public void updateUser(String name, String key, String value) { mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class); } @Override public List<Users> listUser() { return mongoTemplate.findAll(Users.class); } @Override public void saveUsers(List<Users> users) { for (Users users2 : users) { mongoTemplate.save(users2); } } }
四、Controller
@RequestMapping("/mongo") @Controller public class MongoController { @Autowired private UserService userService; @GetMapping("/addList") @ResponseBody public R createList(){ List<Users> list = new ArrayList<Users>(); for(int i=20; i<30; i++){ Users users = new Users(i+"","小明"+i, i); users.setAddress("南京市"); list.add(users); } userService.saveUsers(list); return R.success(); } @GetMapping("/add") @ResponseBody public R create(){ Users users = new Users("1","小明", 10); users.setAddress("南京市"); userService.saveUser(users); return R.success(); } @GetMapping("/list") @ResponseBody public List<Users> list(){ List<Users> listUser = userService.listUser(); return listUser; } @GetMapping("/remove/{name}") @ResponseBody public R remove(@PathVariable String name){ userService.removeUser(name); return R.success(); } @GetMapping("/update/{name}") @ResponseBody public R update(@PathVariable String name){ userService.updateUser(name, "age", "90"); return R.success(); } }
分页:
以正序为例,假设已经取到了第一页的数据,前端记录下当前这页数据的最大id,然后点击下一页时将这个id带给后端,后端取数据时先根据id比较找到所有id大于这个id的数据,然后limit