본문 바로가기
서버/SPRING

[Boostcourse] 이클립스와 MongoDB Backend - Project1 명함만들기

by HDobby 2022. 10. 2.

https://www.boostcourse.org/web326/project/205/content/164 

 

https://www.boostcourse.org/web326/project/205/content/164

 

www.boostcourse.org

pom.xml

더보기
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.edwith.webbe</groupId>
    <artifactId>cardmanager</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
		    <groupId>org.springframework.data</groupId>
		    <artifactId>spring-data-mongodb</artifactId>
		    <version>3.4.3</version>
		  </dependency>
		<dependency>
        <groupId>org.mongodb</groupId>
	        <artifactId>mongodb-driver-sync</artifactId>
	        <version>4.7.1</version>
	    </dependency>
    </dependencies>


    <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

BusinessCardManagerDao.java

더보기
package org.edwith.webbe.cardmanager.dao;

import org.edwith.webbe.cardmanager.dto.BusinessCard;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

public class BusinessCardManagerDao {
String uri = "mongodb://localhost:27017/JDBC_EXAM";
	
    public List<BusinessCard> searchBusinessCard(String keyword){
    	List list = new ArrayList<BusinessCard>();
    	
    	try {
    		SimpleMongoClientDatabaseFactory simpleMongoDbFactory = new SimpleMongoClientDatabaseFactory(uri);
    		MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
    		
    		Criteria regax = Criteria.where("name").regex(".*"+keyword+".*","i");
    		Query query = new Query(regax);
    		
    		list = mongoTemplate.find(query, BusinessCard.class, "BusinessCard");
    	} catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	return list;
    }

    public BusinessCard addBusinessCard(BusinessCard businessCard){
    	try {
    		SimpleMongoClientDatabaseFactory simpleMongoDbFactory = new SimpleMongoClientDatabaseFactory(uri);
    		MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
    		
    		mongoTemplate.insert(businessCard, "BusinessCard");
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	return businessCard;
    }
}

초기 상태

1개 삽입

추가 삽입

like 검색

728x90

댓글