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","//");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin Springboot -- 파트 14 사용 사례 REST로 전환하여 POST로 JSON으로 전환前回 前回 前回 記事 の は は で で で で で で を 使っ 使っ 使っ て て て て て リクエスト を を 受け取り 、 reqeustbody で 、 その リクエスト の ボディ ボディ を を 受け取り 、 関数 内部 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.