SpringBoot整合之SpringBoot整合MongoDB的详细步骤_第1页
SpringBoot整合之SpringBoot整合MongoDB的详细步骤_第2页
SpringBoot整合之SpringBoot整合MongoDB的详细步骤_第3页
SpringBoot整合之SpringBoot整合MongoDB的详细步骤_第4页
SpringBoot整合之SpringBoot整合MongoDB的详细步骤_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第SpringBoot整合之SpringBoot整合MongoDB的详细步骤六、创建dao层,这里的dao层有两种写法

(一)dao层写法一

1.编码部分

packagecn.byuan.dao;

importcn.byuan.entity.Student;

importorg.springframework.data.mongodb.repository.MongoRepository;

*dao层写法一

*这里的用法其实和SpringDataJPA相似,可根据需要来自定义方法

*这种写法不需要写实现类

*MongoRepository行对应的对象类型,主键列类型

publicinterfaceStudentDaoTypeOneextendsMongoRepositoryStudent,String{

//可根据需求自己定义方法,无需对方法进行实现

StudentgetAllByStudentName(StringstudentName);

}

2.测试部分

在进行测试之前,我们先查询一下数据库,此时不存在db_student的库

开始测试

packagecn.byuan;

importcn.byuan.dao.StudentDaoTypeOne;

importcn.byuan.entity.Student;

importorg.junit.jupiter.api.Test;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

importjava.util.Date;

importjava.util.List;

@SpringBootTest

classStudentDaoTypeOneTests{

@Autowired

privateStudentDaoTypeOnestudentDaoTypeOne;

@Test

voidaddOneStudent(){

//插入10行

for(Integercount=0;countcount++){

Studentstudent=newStudent()

.setStudentId("student_"+count)//如果自己不去设置id则系统会分配给一个id

.setStudentName("Godfery"+count)

.setStudentAge(count)

.setStudentScore(98.5-count)

.setStudentBirthday(newDate());

studentDaoTypeOne.save(student);

@Test

voiddeleteOneStudentByStudentId(){

//删除id为student_0的学生

studentDaoTypeOne.deleteById("student_0");

@Test

voidupdateOneStudent(){

//修改姓名为Godfery1的Student年龄为22

Studentstudent=studentDaoTypeOne.getAllByStudentName("Godfery1");

student.setStudentAge(22);

studentDaoTypeOne.save(student);

@Test

voidgetOneStudentByStudentId(){

System.out.println(studentDaoTypeOne.findById("student_1"));

@Test

voidgetAllStudent(){

ListStudentstudentList=studentDaoTypeOne.findAll();

studentList.forEach(System.out::println);

我们先来查看一下数据库

进入数据库查看一下数据

(二)dao层写法二

1.编码部分

接口部分

packagecn.byuan.dao;

importcn.byuan.entity.Student;

importjava.util.List;

*dao层写法二

*写法二需要进行实现

publicinterfaceStudentDaoTypeTwo{

//增加一位学生

voidaddOneStudent(Studentstudent);

//根据id删除一位学生

voiddeleteOneStudentByStudentId(StringstudentId);

//修改一位学生的信息

voidupdateOneStudent(Studentstudent);

//根据主键id获取一名学生

StudentgetOneStudentByStudentId(StringstudentId);

//获取全部学生

ListStudentgetAllStudent();

}

实现类

packagecn.byuan.dao.imp;

importcn.byuan.dao.StudentDaoTypeTwo;

importcn.byuan.entity.Student;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.data.mongodb.core.MongoTemplate;

importorg.springframework.stereotype.Repository;

importjava.util.List;

@Repository

publicclassStudentDaoTypeTwoImpimplementsStudentDaoTypeTwo{

//使用MongoTemplate模板类实现数据库操作

@Autowired

privateMongoTemplatemongoTemplate;

//增加一位学生

publicvoidaddOneStudent(Studentstudent){

mongoTemplate.save(student);

//根据id删除一位学生

publicvoiddeleteOneStudentByStudentId(StringstudentId){

Studentstudent=mongoTemplate.findById(studentId,Student.class);

if(student!=null){

mongoTemplate.remove(student);

//修改一位学生的信息

publicvoidupdateOneStudent(Studentstudent){

mongoTemplate.save(student);

//根据主键id获取一名学生

publicStudentgetOneStudentByStudentId(StringstudentId){

returnmongoTemplate.findById(studentId,Student.class);

//获取全部学生

publicListStudentgetAllStudent(){

returnmongoTemplate.findAll(Student.class);

2.测试部分

packagecn.byuan;

importcn.byuan.dao.StudentDaoTypeOne;

importcn.byuan.dao.StudentDaoTypeTwo;

importcn.byuan.entity.Student;

importorg.junit.jupiter.api.Test;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

importjava.util.Date;

importjava.util.List;

@SpringBootTest

classStudentDaoTypeTwoTests{

@Autowired

privateStudentDaoTypeTwostudentDaoTypeTwo;

@Test

voidaddOneStudent(){

//插入10行

for(Integercount=0;countcount++){

Studentstudent=newStudent()

.setStudentId("study_"+count)//如果自己不去设置id则系统会分配给一个id

.setStudentName("Echo"+count)

.setStudentAge(count)

.setStudentScore(98.5-count)

.setStudentBirthday(newDate());

studentDaoTypeTwo.addOneStudent(student);

@Test

voiddeleteOneStudentByStudentId(){

//删除id为study_0的学生

studentDaoTypeTwo.deleteOneStudentByStudentId("study_0");

@Test

voidupdateOneStudent(){

//修改id为study_1的Student年龄为21

Studentstudent=studentDaoTypeTwo.getOneStudentByStudentId("study_1");

student.setStudentAge(21);

studentDaoTypeTwo.updateOneStudent(student);

@Test

voidgetOneStudentByStudentId(){

System.out.println(studentDaoTypeTwo.getOneStudentByStudentId("study_1"));

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论