赞
踩
1.导入依赖:
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.applicationl.yml文件配置如下:
data:
mongodb:
host: localhost
port: 27017
database: student
# username: student
# password: 123456
1.Domain
/** * @Description:学生信息实体类 * @Author :zks * @Date :9:25 2020/9/15 */ @Data//lombok插件 @Document(collection="studentInfo")//可以省略,如果省略,则默认使用类名小写映射集合 public class StudentInfo { //主键标识,该属性的值会自动对应mongodb的主键字段"_id",如果该属性名就叫“id”,则该注解可以省略,否则必须写 @Id private ObjectId id; /** * 学生姓名 */ //该属性对应mongodb的字段的名字,如果一致,则无需该注解 //@Field("name") private String name; /** * 学生年龄 */ private Integer age; /** * 选修课程列表 */ private List<CourseInfo> chooseCourse; /** * 创建时间 */ private LocalDateTime addTime; /** * 修改时间 */ private LocalDateTime updateTime; /** * 删除标记 */ private Boolean deleted; }
/** * @Description:课程信息实体类 * @Author :zks * @Date :9:29 2020/9/15 */ @Data//lombok插件 @Document(collection="courseInfo")//可以省略,如果省略,则默认使用类名小写映射集合 public class CourseInfo { //主键标识,该属性的值会自动对应mongodb的主键字段"_id",如果该属性名就叫“id”,则该注解可以省略,否则必须写 @Id private ObjectId id; /** * 课程id */ //该属性对应mongodb的字段的名字,如果一致,则无需该注解 //@Field("courseId") private Integer courseId; /** * 课程名称 */ private String Name; /** * 课程描述 */ private String describe; /** * 创建时间 */ private LocalDateTime addTime; /** * 更新时间 */ private LocalDateTime updateTime; /** * 删除标记 */ private Boolean deleted; }
2.Service
/** * @Description:学生Service接口 * @Author :zks * @Date :9:46 2020/9/15 */ public interface IStudentInfoService { /** * 添加学生信息 * @param studentInfo * @return */ public void save(StudentInfo studentInfo); /** * 根据主键id查找学生信息 * @param id * @return */ public StudentInfo getStudentInfoById(ObjectId id); /** * 根据所选课程id查找学生信息列表并分页 * @param courseId * @param page * @param limit * @param sort * @param order * @return */ public List<StudentInfo> getStudentInfoListByCourseId(Integer courseId,Integer page, Integer limit, String sort, String order); /** * 更新学生信息 * @param studentInfo * @return */ public void update(StudentInfo studentInfo); /** * 根据学生id以及课程id删除选修课程 * @param id * @param courseId */ public void deleteCourseByIdAndCourseId(ObjectId id,Integer courseId); /** * 删除学生信息 * @param id * @return */ public void delete(ObjectId id); }
/** * @Description:学生Service实现类 * @Author :zks * @Date :9:59 2020/9/15 */ @Service public class StudentInfoServiceImpl implements IStudentInfoService { @Autowired MongoTemplate mongoTemplate; @Override public void save(StudentInfo studentInfo) { mongoTemplate.save(studentInfo); } @Override public StudentInfo getStudentInfoById(ObjectId id) { StudentInfo studentInfo = mongoTemplate.findById(id,StudentInfo.class); return studentInfo; } @Override public List<StudentInfo> getStudentInfoListByCourseId(Integer courseId, Integer page, Integer limit, String sort, String order) { //查询条件 Query query = new Query(); query.addCriteria(Criteria.where("chooseCourse.courseId").is(courseId).and("chooseCourse.deleted").is(false) .and("deleted").is(false)); // 分页 query.skip( (page - 1) * limit).limit(limit); // 排序 query.with(new Sort(Sort.Direction.fromString(order), sort)); //springboot2.2.1(含)以上的版本Sort已经不能再实例化了,构造方法已经是私有的了! //query.with(Sort.by(Sort.Direction.fromString(order), sort)); ArrayList<StudentInfo> studentInfos= (ArrayList<StudentInfo>) mongoTemplate.find(query,StudentInfo.class); return studentInfos; } @Override public void update(StudentInfo studentInfo) { //查询条件 Query query=Query.query(Criteria.where("_id").is(studentInfo.getId()).and("deleted").is(false)); //需要修改的属性 Update update = new Update(); update.set("name", studentInfo.getName()); update.set("age",studentInfo.getAge()); update.set("updateTime",LocalDateTime.now()); mongoTemplate.updateFirst(query,update,StudentInfo.class); } @Override public void deleteCourseByIdAndCourseId(ObjectId id, Integer courseId) { //查询条件 Query query=new Query(); query.addCriteria(Criteria.where("_id").is(id).and("chooseCourse.courseId").is(courseId)); //需要修改的属性 Update update = Update.update("chooseCourse.$.deleted", true);//注意:在这里因为涉及到数组中的文档操作,需要加位置界定符"$" mongoTemplate.updateFirst(query,update,StudentInfo.class); } @Override public void delete(ObjectId id) { //查询条件 Query query=Query.query(Criteria.where("_id").is(id).and("deleted").is(false)); //需要修改的属性 Update update = Update.update("deleted", true); mongoTemplate.updateFirst(query,update,StudentInfo.class); } }
3.Controller
/** * @Description:学生控制器 * @Author :zks * @Date :10:51 2020/9/15 */ @RestController @RequestMapping("student") public class StudentController { @Autowired private IStudentInfoService studentInfoService; /** * 保存学生信息 * @param studentInfo * @return */ @PostMapping("save") public HttpRespond save(@RequestBody StudentInfo studentInfo){ studentInfoService.save(studentInfo); return HttpRespond.success(); } /** * 根据主键id查找学生信息 * @param id * @return */ @GetMapping("getStudentInfoById") public HttpRespond getStudentInfoById(@RequestParam("id") ObjectId id){ StudentInfo studentInfo=studentInfoService.getStudentInfoById(id); return HttpRespond.success(studentInfo); } /** * 根据所选课程id查找学生信息列表并分页 * @param courseId * @param page * @param limit * @param sort * @param order * @return */ @GetMapping("getStudentInfoListByCourseId") public HttpRespond getStudentInfoListByCourseId(@RequestParam("courseId")Integer courseId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, @Sort @RequestParam(defaultValue = "addTime") String sort, @Order @RequestParam(defaultValue = "desc") String order){ List<StudentInfo> studentInfoList = studentInfoService.getStudentInfoListByCourseId(courseId, page, limit, sort, order); return HttpRespond.success(studentInfoList); } /** * 更新学生信息 * @param studentInfo * @return */ @PostMapping("update") public HttpRespond update(@RequestBody StudentInfo studentInfo){ studentInfoService.update(studentInfo); return HttpRespond.success(); } /** * 根据学生id以及课程id删除选修课程 * @param id * @param courseId * @return */ @GetMapping("deleteCourseByIdAndCourseId") public HttpRespond deleteCourseByIdAndCourseId(@RequestParam("id") ObjectId id,@RequestParam("courseId") Integer courseId){ studentInfoService.deleteCourseByIdAndCourseId(id,courseId); return HttpRespond.success(); } /** * 删除学生信息 * @param id * @return */ @GetMapping("delete") public HttpRespond delete(@RequestParam("id") ObjectId id){ studentInfoService.delete(id); return HttpRespond.success(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。