Springboot集成MongoDB,及MongoTemplate的简单使用

一、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

二、配置MongoDB链接

spring:
	data:
	    mongodb:
	      host: 127.0.0.1
	      port: 27017
	      database: test

三、实体类

@Document(collection = "student") //该实体类对应集合名称
public class Student implements Serializable {
    @Id
    private String id;
    private String name;
    private String address;
    private int sex;
	
	/**getter and setter**/
}

四、Service类

@Service
public class StudentService {
private MongoTemplate mtl;

    @Autowired
    public StudentService(MongoTemplate mtl) {
        this.mtl = mtl;
    }

	//C(增)
	//单个增
	public void saveStudent(Student student){
		mtl.save(student); //mtl.insert(student);也可以
		//若新增数据的主键已存在:save会对存在数据更新、insert不会保存
	}
	//批量增
	public void saveStudents(List<Student> studentlist){
		mtl.insert(studentlist);
		//若新增数据的主键已经存在,则会抛异常提示主键重复,不保存当前数据。
	}

	//U(改)
	public void updateStudent(Student student){
		mtl.save(student);
	}

	//R(查)
	//查询所有
	public List<Student> findAllStudent(){
		return mtl.findAll(Student.class);
	}
	//根据ID查询
	public Student findStudentById(String id){
		return mtl.findById(id,Student.class);
	}
	//根据姓名准确查询
	public List<Student> findStudentByName(String name){
		Query query = new Query(Criteria.where("name").is(name));
		return mtl.find(query, Student.class);
	}
	//根据地址模糊查询
	public List<Student> findStudentByAddress(String address){
		Pattern pattern=Pattern.compile("^.*"+address+".*$", Pattern.CASE_INSENSITIVE);
		Query query = new Query(Criteria.where("name").regex(pattern));
		return mtl.find(query, Student.class);
	}
	//多条件查询
	public List<Student> findMaleStudentByName(String name){
		Query query = new Query(new Criteria().andOperator(Criteria.where("name").is(name), Criteria.where("sex").is(0)));
		//所给例为与条件、若为或条件,将andOperator改为orOperator
		return mtl.find(query, Student.class);
	}
	//分页查询
	public List<Student> findStudentByNameWithPage(String name, int nowPage){
		Query query = new Query(Criteria.where("name").is(name));
		Pageable mypage = PageRequest.of(nowPage-1,10); //Springboot 2.x 10为每页10个
		//Pageable mypage = new PageRequest(page-1,10); //Springboot 1.x 10为每页10个
		query.with(mypage);
		return mtl.find(query,Student.class);
	}

	//D(删)
	public void deleteStudent(Student student){
		mtl.remove(student);
	}
}

五、Query的使用方法

Query:
精准条件:Criteria.where(“key”).is(“条件”)
模糊条件:Criteria.where(“key”).regex(“条件”)
判断大小:gt:大于 lt:小于 gte:大于等等 lte:小于等于
或条件:new Criteria().orOperator(“条件”)
与条件:new Criteria().andOperator(“条件”)//仅能有一个
封装条件:query.addCriteria(criteria)
分页:Pageable mypage = PageRequest.of(nowPage-1,10); //Springboot 2.x 10为每页10个
	//Pageable mypage = new PageRequest(page-1,10); //Springboot 1.x 10为每页10个
	query.with(mypage);
排序:query.with(Sort.by(Sort.Order.desc("creatTime")));
	query.with(Sort.by(Sort.Order.asc("creatTime")));
Tips:进行日期查询时只需将JAVA中得到的日期作为条件即可,而不用像其他教程中所说的转换后在查询。
具体原因作为小白,我也不是很清楚。但是查询出的结果确实准确,按其他教程来程序并不能得到结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值