MongoDB学习(四) --- MongoDB 整合Spring Data
短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
1、环境搭建
-
步骤一:修改pom文件,更新依赖
org.springframework.boot spring-boot-starter-data-mongodb org.projectlombok lombok -
步骤二:修改yml文件,配置 mongo连接字符串
spring: data: mongodb: uri: mongodb://root:1234@localhost:27017/admin
-
步骤三:编写 JavaBean,配置文档对应集合
package com.tqyl.domain; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; @Data @Document(collection = "teacher") public class Teacher { @Id private String teacherId; @Field("username") private String username; private String password; private Integer age; public Teacher() { } public Teacher(String teacherId, String username, String password, Integer age) { this.teacherId = teacherId; this.username = username; this.password = password; this.age = age; } }
-
步骤四:编写 dao,继承MongoRepository
package com.tqyl.dao; import com.tqyl.domain.Teacher; import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; public interface TeacherDao extends MongoRepository
{ public List findByUsername(String username); } -
步骤五:编写启动类,使用@ComponentScan扫描 dao
package com.tqyl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.tqyl.dao") public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class,args); } }
-
步骤六:编写测试类,获得 dao 实现类
@RunWith(SpringRunner.class) @SpringBootTest(classes = TestApplication.class) public class TestRepository { @Resource private TeacherDao teacherDao; }
2、基础操作
2.1、查询所有
@Test
public void testFindAll(){
//查询
List list = teacherDao.findAll();
System.out.println(list);
}
2.2、排序查询
@Test
public void testDemo01(){
//排序
//List list = teacherRepository.findAll(Sort.by("password"));
List list = teacherDao.findAll(Sort.by(Sort.Order.desc("password")));
for (Teacher teacher : list) {
System.out.println(teacher);
}
}
2.3、分页
- 分页基本操作
@Test
public void testDemo02(){
//分页
int page = 0; //从0开始
int size = 2;
Page teacherPage = teacherDao.findAll(PageRequest.of(page, size));
//处理分页数据
//1 获得分页内容
List list = teacherPage.getContent();
//2 获得总记录数
long total = teacherPage.getTotalElements();
System.out.println("总条数:" + total);
for (Teacher teacher : list) {
System.out.println(teacher);
}
}
- 分页 jdk8 ForEach
@Test
public void testDemo03() {
//分页
int page = 0; //从0开始
int size = 2;
Page teacherPage = teacherDao.findAll(PageRequest.of(page, size));
//遍历数据
// JDK8提供forEach,使用箭头函数进行遍历
teacherPage.forEach(teacher -> System.out.println(teacher) );
// 打印简化版
teacherPage.forEach(System.out::println);
}
2.5、添加
@Test
public void testDemo04(){
//准备数据
Teacher teacher = new Teacher();
teacher.setUsername("王五");
teacher.setPassword("666777");
teacher.setAge(30);
//插入数据
teacherDao.insert( teacher );
}
2.6、通过ID查询
@Test
public void testDemo05(){
//通过id查询
Optional optional = teacherDao.findById("5ee83ccf9d1c6904d8768ca8");
//Optional 用于统一规范空指针异常处理
if(optional.isPresent()){ // teacher != null
Teacher teacher = optional.get();
System.out.println(teacher);
} else {
System.out.println("没有数据");
}
}
2.7、更新
- 更新采用save方法
- 如果数据存在,将发生更新操作
- 如果数据不存在,将发生添加操作
@Test
public void testDemo06(){
//更新操作
//1 查询
Optional optional = teacherDao.findById("5ee83ccf9d1c6904d8768ca8");
if(optional.isPresent()) {
//2 修改数据
Teacher teacher = optional.get();
teacher.setUsername("王武");
//3 更新
teacherDao.save(teacher);
}
}
2.8、删除
- deleteById() :通过id删除
- deleteAll() :删除所有
@Test
public void testDemo07(){
//删除
teacherDao.deleteById("5ee83ccf9d1c6904d8768ca8");
}
2.9、自定义dao
- 在dao中,可以通过约定进行条件查询
- 前缀:findBy
- 查询条件:字段名
- 关键字:And、Or、Like 等
- TeacherRepository
public List findByUsername(String username);
@Test
public void testDemo08(){
//自定义查询
List list = teacherRepository.findByUsername("tom");
System.out.println(list);
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341