MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[Spring / ์‹ค์Šต1] ์ ์ˆ˜ ๋“ฑ๋ก ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ (DB,VO,Mapper)

HYEJU01 2024. 8. 25. 18:32

 

1) Oracle database :: TABLE ์ƒ์„ฑ

 

CREATE TABLE SCORE(
    SNO NUMBER(10),
    NAME VARCHAR2(30),
    KOR VARCHAR2(30),
    ENG VARCHAR2(30),
    MATH VARCHAR2(30)
);
ALTER TABLE SCORE ADD CONSTRAINT SCORE_SNO_PK PRIMARY KEY (SNO); -- PK
CREATE SEQUENCE SCORE_SEQ NOCACHE; --์‹œํ€€์Šค

 

 

 

2) VO ๋งŒ๋“ค๊ธฐ 

๋”๋ณด๊ธฐ
package com.simple.basic.command;

public class ScoreVO {
	
	private int sno;
	private String name;
	private String kor;
	private String eng;
	private String math;
	
	
	
	public ScoreVO() {
	}

	public ScoreVO(int sno, String name, String kor, String eng, String math) {
		super();
		this.sno = sno;
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}

	public int getSno() {
		return sno;
	}

	public void setSno(int sno) {
		this.sno = sno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getKor() {
		return kor;
	}

	public void setKor(String kor) {
		this.kor = kor;
	}

	public String getEng() {
		return eng;
	}

	public void setEng(String eng) {
		this.eng = eng;
	}

	public String getMath() {
		return math;
	}

	public void setMath(String math) {
		this.math = math;
	}
	
	
	
	
}

 

3) Mapper.java / Mapper.xml ๋งŒ๋“ค๊ธฐ

 

๐Ÿ’€root-context.xml์— ์ถ”๊ฐ€

 ํ•ด๋‹น ํŒจํ‚ค์ง€๋ฅผ ์ฝ์–ด์„œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์œ„์น˜๋ฅผ ํ™•์ธ (@Mapper) 

<mybatis-spring:scan base-package="com.simple.basic.*"/>

 

 

๐Ÿ’€mapper.java

  • @Mapper ์–ด๋…ธํ…Œ์ด์…˜ํ†ตํ•ด MyBatis๊ฐ€ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ธ์‹ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.
  • ๋“ฑ๋ก, ์กฐํšŒ, ์‚ญ์ œ๋ฅผ ์œ„ํ•œ ๋ฉ”์„œ๋“œ interface ๋ฅผ ์„ ์–ธํ•ด์ค€๋‹ค.
package com.simple.basic.service.score;

import java.util.ArrayList;

import org.apache.ibatis.annotations.Mapper;

import com.simple.basic.command.ScoreVO;

@Mapper //๋งคํผ์ž„
public interface ScoreMapper {

	public void regist(ScoreVO vo); //๋“ฑ๋ก
	public ArrayList<ScoreVO> getScore(); //์กฐํšŒ
	public void delete(int sno); //์‚ญ์ œ
}

 

 

 

๐Ÿ’€<mapper> namespace

namespace ์†์„ฑ์€ interface ์ด๋ฆ„๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถ˜๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  <mapper namespace="com.simple.basic.service.score.ScoreMapper">
  </mapper>

 

 

๐Ÿ’€ ๋“ฑ๋ก insert

๋งค๊ฐœ๋ณ€์ˆ˜๋Š” 1๊ฐœ์—ฌ์•ผ ํ•จ. voํƒ€์ž…์˜ ๊ฐ์ฒด๋ผ๋ฉด #{getter} ์‚ฌ์šฉํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•จ

SCORE_SEQ.NEXTVAL: SCORE_SEQ๋ผ๋Š” ์‹œํ€€์Šค ๊ฐ์ฒด์—์„œ ๋‹ค์Œ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

  	<insert id="regist" parameterType="ScoreVO">
  		INSERT INTO SCORE(SNO, NAME, KOR, ENG, MATH)
  		VALUES(SCORE_SEQ.NEXTVAL, #{name}, #{kor}, #{eng}, #{math} )
  	</insert>

 

๐Ÿ’€ ์กฐํšŒ select

resultType ์€ ํ•„์ˆ˜๋กœ ์ ์–ด์ค€๋‹ค. select ๊ฐ€ ๋ฐ˜ํ™˜๋ฐ›์„ ํƒ€์ž…์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

  	<select id="getScore" resultType="ScoreVO">
  		select * from SCORE order by sno desc
  	</select>

 

๐Ÿ’€ ์‚ญ์ œ delete

ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 1๊ฐœ์ด๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ช…์„ #{๋งค๊ฐœ๋ณ€์ˆ˜} ๋กœ ์“ฐ๋ฉด ๋œ๋‹ค.

  	<delete id="delete" parameterType="int">
  		delete from SCORE where sno = #{sno}
  	</delete>

 

 

 


(+) DAO ์‚ฌ์šฉ ์‹œ 

 

@Repository("Y") ๋ฆฌํฌ์ง€ํ† ๋ฆฌ(Repository) ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉ (์ฆ‰ DAO ์—ญํ• ์„ ํ•จ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค)

 

 

 

@Qualifier("x")๋Š” ์˜์กด ๊ฐ์ฒด๋ฅผ ์„ ํƒํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.

๊ฐ™์€ ํƒ€์ž…์˜ ์—ฌ๋Ÿฌ ๋นˆ์ด ์žˆ์„ ๋•Œ ( ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์–ด๋–ค ๋นˆ์„ ์ฃผ์ž…ํ•ด์•ผํ•˜๋Š”์ง€ ๋ชฐ๋ผ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.)

์–ด๋–ค ๋นˆ์„ ์ฃผ์ž…ํ•ด์•ผ ํ• ์ง€ ๋ช…ํ™•ํžˆ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด

(@Qualifier ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. "x"๋Š” ์ฃผ์ž…ํ•  ๋นˆ์˜ ์ด๋ฆ„์„ ๋ช…์‹œํ•˜๋Š” ๊ฐ’)

package com.simple.basic.service.score;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.simple.basic.command.ScoreVO;
import com.simple.basic.service.score.impl.ScoreDAO;

@Service("x") //๋ฐ˜๋“œ์‹œ component-scan์— ์˜ํ•˜์—ฌ ์ฝํ˜€์•ผ ํ•จ
public class ScoreServiceImpl implements ScoreService {
	
	@Autowired
	@Qualifier("y")
	ScoreDAO scoreDAO;