<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
spring.data.mongodb.uri=mongodb://192.168.43.59:27017/yygh_hosp
spring.data.mongodb.auto-index-creation=false
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("User")//对应mongo中的集合
@Data
public class User {
@Id
private String id;
private String name;
private Integer age;
private String email;
private String createDate;
}
常用方法:
mongoTemplate.findAll(User.class): 查询User文档的全部数据
mongoTemplate.findById(, User.class): 查询User文档id为id的数据
mongoTemplate.find(query, User.class);: 根据query内的查询条件查询
mongoTemplate.upsert(query, update, User.class): 修改
mongoTemplate.remove(query, User.class): 删除
mongoTemplate.insert(User): 新增
Query对象
1、创建一个query对象(用来封装所有条件对象),再创建一个criteria对象(用来构建条件)
2、 精准条件:criteria.and(“key”).is(“条件”)
模糊条件:criteria.and(“key”).regex(“条件”)
3、封装条件:query.addCriteria(criteria)
4、大于(创建新的criteria):Criteria gt = Criteria.where(“key”).gt(“条件”)
小于(创建新的criteria):Criteria lt = Criteria.where(“key”).lt(“条件”)
5、Query.addCriteria(new Criteria().andOperator(gt,lt));
6、一个query中只能有一个andOperator()。其参数也可以是Criteria数组。
7、排序 :query.with(new Sort(Sort.Direction.ASC, “age”). and(new Sort(Sort.Direction.DESC, “date”)))
void add() {
User user = new User();
user.setAge(20);
user.setEmail("2991470294.com");
user.setName("刘思元");
User user1 = mongoTemplate.insert(user);
System.out.println(user1);
}
//删除
@Test
void deleteUser(){
User user = new User();
user.setId("622dade8ef1e5ff9b7b58a85");
Query query = new Query(Criteria.where("id").is(user.getId()));
DeleteResult remove = mongoTemplate.remove(query, User.class);
}
//修改
@Test
void updateUser(){
//根据id查询
User userById = mongoTemplate.findById("622dad67ef1e5ff9b7b58a84", User.class);
//设置修改值
userById.setName("寸思思");
userById.setEmail("[email protected]");
//调用修改方法
Query query = new Query(Criteria.where("id").is(userById.getId()));
Update update = new Update();
update.set("name", userById.getName());
update.set("email",userById.getEmail());
UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
long modifiedCount = upsert.getModifiedCount();
System.out.println(modifiedCount);
}
查询所有数据
//查询所有数据
@Test
void findAll() {
List<User> userList = mongoTemplate.findAll(User.class);
System.out.println(userList);
}
根据id查询
//根据id查询
@Test
void findById() {
User byId = mongoTemplate.findById("622dad67ef1e5ff9b7b58a84", User.class);
System.out.println(byId);
}
条件查询操作
//条件查询操作
@Test
void findQuery(){
Query query = new Query(Criteria.where("name").is("刘思元").and("age").is(20));
List<User> users = mongoTemplate.find(query, User.class);
System.out.println(users);
}
模糊查询
//模糊查询
@Test
void findLike(){
String name = "思";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Query query = new Query(Criteria.where("name").regex(pattern));
List<User> users = mongoTemplate.find(query, User.class);
System.out.println(users);
}
分页查询
//分页查询
@Test
void findPage(){
//当前页 每页显示的记录数
int pageNO = 1;
int pageSize = 2;
//条件查询
String name = "";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Query query = new Query(Criteria.where("name").regex(pattern));
//分页构造
long count = mongoTemplate.count(query, User.class);
//分页 skip表示跳过的记录数,当前页为1则跳过0条,为2则跳过10条(跳过第一页的十条数据)
List<User> users = mongoTemplate.find(query.skip((pageNO - 1) * pageSize).limit(pageSize), User.class);
System.out.println(count);
System.out.println(users);
}
springData提供了对MongoDB数据访问的支持,我们只需要继承MongoRepository类,按照SpringData规范就可以
SpringData方法定义规范
import com.lsy.mongo.entity.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User,String> {
}
添加一个数据
@Test
void add() {
User user = new User();
user.setAge(20);
user.setEmail("2991470294.com");
user.setName("刘思元and寸思思");
User save = userRepository.save(user);
System.out.println(save);
}
查询数据
//查询所有数据
@Test
void findAll() {
List<User> all = userRepository.findAll();
System.out.println(all);
}
//根据id查询
@Test
void findById() {
User user = userRepository.findById("622dad67ef1e5ff9b7b58a84").get();
System.out.println(user);
}
//条件查询操作
@Test
void findQuery(){
User user = new User();
user.setAge(20);
user.setEmail("2991470294.com");
user.setName("刘思元and寸思思");
Example<User> userExample = Example.of(user);
List<User> all = userRepository.findAll(userExample);
System.out.println(all);
}
//模糊查询
@Test
void findLike(){
//设置模糊查询的匹配规则
ExampleMatcher matcher = ExampleMatcher.matching()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase(true);
User user = new User();
user.setName("思");
Example<User> userExample = Example.of(user,matcher);
List<User> all = userRepository.findAll(userExample);
System.out.println(all);
}
//分页查询
@Test
void findPage(){
//分页 0代表第一页
Pageable pageable = PageRequest.of(0, 3);
User user = new User();
Example<User> userExample = Example.of(user);
Page<User> all = userRepository.findAll(userExample, pageable);
System.out.println(all);
}
修改
//修改
@Test
void updateUser(){
User user = userRepository.findById("622dad67ef1e5ff9b7b58a84").get();
user.setAge(100);
userRepository.save(user);
}
删除
//删除
@Test
void deleteUser(){
userRepository.deleteById("");
}