springboot 조작 몬godb 추가 삭제

6132 단어 springbootmongo

springboot 조작 몬godb 추가 삭제


유도jar 패키지
 
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

애플리케이션 구성.yml
spring:
  data:
    mongodb:
      host: 192.168.128.130
      database: studentdb


 

서비스 계층
package com.dl.springboot_mongo.service;
import com.dl.springboot_mongo.pojo.Student;
import java.util.List;
public interface StudentService {
        /**
         *  
         * @return
         */
        List findAll();

        /**
         *  ID 
         * @return
         */
        Student findById(Long id);

        /**
         *  
         * @return
         */
        void modify(Student student);

        /**
         *  
         * @return
         */
        void del(Long id);

        /**
         *  
         * @return
         */
        void save(Student student);

        /**
         *  
         * @return
         */
        List findAllmo(String name,String keyword);
}

실현층
package com.dl.springboot_mongo.service.serviceimlp;
import com.dl.springboot_mongo.pojo.Student;
import com.dl.springboot_mongo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;


import java.util.List;
import java.util.regex.Pattern;


@Component
public class StudentServiceimlp implements StudentService {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     *  
     * @return
     */
    @Override
    public List findAll() {
        return mongoTemplate.findAll(Student.class);
    }


    /**
     *  ID 
     * @return
     */
    @Override
    public Student findById(Long id) {

        return mongoTemplate.findById(id,Student.class);
    }


    /**
     *  
     * @return
     */
    @Override
    public void modify(Student student) {
        Query query = new Query(Criteria.where("_id").is(student.getId()));
        Update update = new Update();
        update.set("name",student.getName());
        update.set("sex",student.getSex());
        update.set("age",student.getAge());
        update.set("gradeId",student.getGradeId());
        mongoTemplate.updateFirst(query,update,Student.class);
    }


    /**
     *  
     * @return
     */
    @Override
    public void del(Long id) {
    Student ById=mongoTemplate.findById(id,Student.class);
    mongoTemplate.remove(ById);
    }


    /**
     *  
     * @return
     */
    @Override
    public void save(Student student) {
    mongoTemplate.save(student);
    }


    /**
     *  
     * @return
     */
    @Override
    public List findAllmo(String name,String keyword) {

        // 
        Pattern pattern = Pattern.compile("^"+name+"$", Pattern.CASE_INSENSITIVE);
        // 
        Pattern pattern1 = Pattern.compile("^.*"+name+"$", Pattern.CASE_INSENSITIVE);
        // 
        Pattern pattern2 = Pattern.compile("^"+name+".*$", Pattern.CASE_INSENSITIVE);
        // 
        Pattern pattern3 = Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE);
        Query query = Query.query(Criteria.where(name).regex(pattern));
        return mongoTemplate.find(query,Student.class);
    }


}

테스트
package com.dl.springboot_mongo;
import com.dl.springboot_mongo.pojo.Grade;
import com.dl.springboot_mongo.pojo.Student;
import com.dl.springboot_mongo.service.GradeService;
import com.dl.springboot_mongo.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMongoApplicationTests {


    @Autowired
    private StudentService studentService;


    @Autowired
    private GradeService gradeService;

    @Test
    public void contextLoads() {
    }

    /**
     * Student 
     */

    @Test
    public void findAll(){
    List students= studentService.findAll();
        System.out.println(students.size());
    }


    /**
     * Student Id 
     */
    @Test
    public void findById(){
       Student student= studentService.findById((long) 1);
        System.out.println(student.getName());
    }


    /**
     * Student Id 
     */
    @Test
    public void del(){
        try {
            studentService.del((long) 5);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    /**
     * Student 
     */
    @Test
    public void save(){
        Student student=new Student();
        student.setId((long) 6);
        student.setName("li");
        student.setSex(" ");
        student.setAge((long) 18);
        student.setGradeId((long) 2);
        try{
            studentService.save(student);
        }catch (Exception e){

        }
    }


    /**
     * Student 
     */

    @Test
    public void modify(){
        Student student=new Student();
        student.setId((long) 6);
        student.setName("lii");
        student.setSex(" ");
        student.setAge((long) 18);
        student.setGradeId((long) 2);
        studentService.modify(student);
    }


    /**
     *Grade 
     */
    @Test
    public void GradefindAll(){
        List grades=gradeService.findAll();
        System.out.println(grades.size());
    }


    @Test
    public void findAllmo(){
        studentService.findAllmo("java","//");
    }
}

좋은 웹페이지 즐겨찾기