๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JSP/Servlet] Maven (2023.05.22) ๋ณธ๋ฌธ

full stack/Back-End

[JSP/Servlet] Maven (2023.05.22)

Shinjio 2023. 5. 22. 14:10

๐ŸŽˆ Build 

 

 

ํฉ์–ด์ง„ ์ž์›๋“ค์„ WAS๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋กœ ๋งž์ถฐ์ฃผ๋Š” ๊ฒƒ 

ex. ํ™”๋ฉด์— ๋ณด์—ฌ์•ผ ํ•˜๋Š” ๊ฒƒ์€ web-app ๋‚ด๋ถ€์— ๋“ค์–ด๊ฐ€์•ผ ํ•จ. 

WEB-INF๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€ํ•œ ๊ณณ์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ์„ ์ €์žฅ

 

๊ณผ๊ฑฐ์—๋Š” ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ–ˆ์Œ >> ์ž๋™ํ™”๋ฅผ ์œ„ํ•œ ๋งŒ๋“ค์–ด์ง„ ๋„๊ตฌ >> ๋นŒ๋“œ ๋„๊ตฌ

 

๐Ÿ“– ๋นŒ๋“œ๋„๊ตฌ 

โš™ Ant  

์ž๋ฐ” ํ”„๋กœ์ ํŠธ์˜ ๋นŒ๋“œ๋ฅผ ์ž๋™ํ™”ํ•ด์ฃผ๋Š” ๋นŒ๋“œ ํˆด

 

 

โš™ Maven

์ž๋ฐ” ํ”„๋กœ์ ํŠธ์˜ ๋นŒ๋“œ๋ฅผ ์ž๋™ํ™”ํ•ด์ฃผ๋Š” ๋นŒ๋“œ ํˆด

์ปดํŒŒ์ผ๊ณผ ๋นŒ๋“œ๋ฅผ ๋™์‹œ์— ์ˆ˜ํ–‰, ํ…Œ์ŠคํŠธ๋ฅผ ๋ณ‘ํ–‰ํ•˜๊ฑฐ๋‚˜ ์„œ๋ฒ„ ์ž์›์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ ์ œ๊ณต

Ant๋ฅผ ๊ฐ„ํŽธํ™”ํ•œ ๊ฒƒ 

 

- Ant์˜ ๊ฒฝ์šฐ

 

- Maven์˜ ๊ฒฝ์šฐ

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ์„ ์ง์ ‘ ๋‹ค์šด๋ฐ›์•„ ๋„ฃ์–ด์ฃผ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ pom.xml์— ์ ๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ

 

 

๐ŸŽˆ Maven ์‹ค์Šต

๐Ÿ“– Maven ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์œ„์น˜ ์„ค์ •

 

 

 

Group Id : ์‹๋ณ„ ์•„์ด๋”” (๊ฒฝ๋กœ)

artifact id : ํ”„๋กœ์ ํŠธ ์ด๋ฆ„

 

โš™ ์ƒ์„ฑ๋œ Maven ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

 

 

pom.xml ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด ์ž‘์„ฑํ•˜๋Š” ํŒŒ์ผ

 

๐Ÿ“– pom.xml์—์„œ JDK ๋ฒ„์ „๊ณผ Maven-Servlet ์—ฐ๊ฒฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

โš™ jdk ๋ฒ„์ „ ๋ณ€๊ฒฝ

ํ˜„์žฌ 1.7 jdk ๋ฒ„์ „

 

11 ๋ฒ„์ „์œผ๋กœ ๋ณ€๊ฒฝ

 

ํŒŒ์ผ ์ €์žฅ

 

 

โš™ Maven-Servlet ์—ฐ๊ฒฐ

 

 

Maven ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ ์˜ค๋ฅ˜๋œจ๋Š” ์ด์œ  : mavenํ”„๋กœ์ ํŠธ์—์„œ๋Š” java ์‚ฌ์šฉ ๋ถˆ๊ฐ€ >> ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ

 

pom์— ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ : 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ €์žฅ์†Œ >> https://mvnrepository.com/

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ค‘์•™ ์ €์žฅ์†Œ์— ๊ฐœ๋ฐœํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋ฉ”์ด๋ธ ์ค‘์•™ ์ €์žฅ์†Œ(Repository)์—์„œ ๋‚ด๋ ค๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. 

 

servlet-api ๊ฒ€์ƒ‰

 

์ €์žฅ ํ›„ index.jsp ๋“ค์–ด๊ฐ€์„œ ๋‚ด๋ถ€ ๋‚ด์šฉ ์ž˜๋ผ๋‚ด๊ธฐ ํ›„ ๋‹ค์‹œ ๋ถ™์—ฌ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜ ์‚ฌ๋ผ์ง

 

 

์ž๋™์œผ๋กœ ๋‹ค์šด๋กœ๋“œ ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ €์žฅ์†Œ : Maven Dependencies

 

jsp ํŒŒ์ผ์— ์ง€์‹œ์ž ์“ฐ๊ธฐ ๊ท€์ฐฎ์€๋‹ˆ indexํŒŒ์ผ ์ง€์šด ํ›„ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์ƒˆ๋กœ ์ƒ์„ฑ

 

 

๐Ÿ“– ๊ธฐํƒ€ ์„ค์ •

โš™ java ํด๋” ์ƒ์„ฑํ•˜๊ธฐ

 

 

 

โš™ url mapping (๋˜์–ด์žˆ์Œ!)

annotation ๋ฐฉ์‹ ๋Œ€์‹  web.xml์— ์ž‘์„ฑ

 

๐ŸŽˆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐํ•˜๊ธฐ - MyBatis ์‚ฌ์šฉ

 

 

ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์žฅ์ ์ด์ž ๋‹จ์  : ์ฒ˜์Œ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ณต๋ถ€ํ•ด์•ผ ํ•จ. ๊ทธ๋Ÿฌ๋‚˜ ์ต์ˆ™ํ•ด์ง€๋ฉด ์‚ฌ์šฉ์„ฑ์ด ์ข‹์Œ

 

 

2, 3๋ฒˆ์€ ์Šคํ”„๋ง ๋•Œ ์‚ฌ์šฉ

Maven์—์„œ๋Š” 1๋ฒˆ ๋ฐฉ์‹์œผ๋กœ 

 

MyBatis์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜

DB์—์„œ ๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌด์กฐ๊ฑด ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์•ผ ํ•จ 

๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ค„ ๋•Œ ๋˜ํ•œ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์•ผ ํ•จ

VO ํด๋ž˜์Šค๊ฐ€ ๊ผญ ํ•„์š”ํ•จ

 

๐Ÿ“– ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

โš™ MyBatis, ojdbc ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ 

- pom.xml์—์„œ ์ˆ˜ํ–‰

 

โš™ mybatis - ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์œ„ํ•œ ํŒŒ์ผ ์ž‘์„ฑ

- mybatis-config.xml

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ํŒŒ์ผ์€ ๋ณ„๋„ package ๋งŒ๋“ค์–ด์„œ ์ €์žฅ

 

์ฐธ๊ณ ์ž๋ฃŒ :

https://mybatis.org/mybatis-3/ko/getting-started.html

 

MyBatis – ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค 3 | ์‹œ์ž‘ํ•˜๊ธฐ

 

mybatis.org

 

์•„๋ž˜ ๋‚ด์šฉ ๋ณต๋ถ™ 

 

xml ํŒŒ์ผ ํ˜•์‹

>> ํŒŒ์ผ๋ช… 

 

 

์ž‘์„ฑ๋‚ด์šฉ

 

enviroment : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ™˜๊ฒฝ์ •๋ณด 

๋ณดํ†ต์€ db๋ฅผ 2~3๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉ (๊ฐœ๋ฐœ, ํ…Œ์ŠคํŠธ, ๋ฐฐํฌ์šฉ)

๊ฐœ๋ฐœ์ค‘ ์‚ฌ์šฉํ•˜๋Š” db >> id = "development" (์ด๋ฆ„์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ)

property ์˜ value ๊ฐ’์„ ์ง€์ •ํ•ด์ค˜์•ผ ํ•จ >> ๋‹ค๋ฅธ ํŒŒ์ผ์— ์ €์žฅํ•ด๋‘๊ณ  ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ์Œ

 

- mybatis-config.xml ๋‚ด๋ถ€์— db์˜ property value๋ฅผ ๋ณ„๋„๋กœ ์ €์žฅํ•ด๋‘” ํŒŒ์ผ ์ž‘์„ฑ

 

ํ™•์žฅ์ž : file

 

ํŒŒ์ผ๋ช… : db.properties

 

์•„์ด๋”” = value ํ˜•ํƒœ๋กœ ์ž‘์„ฑ

 

 

- db.properties ์ž‘์„ฑ ๋ฐ ์ €์žฅ ํ›„ mybatis-config.xml๊ณผ ํŒŒ์ผ ์—ฐ๊ฒฐ

myBatis์—์„œ db.properties๋ฅผ ๋Œ์–ด๋‹ค ์“ฐ๊ฒ ๋‹ค ์ •์˜ํ•ด์•ผํ•จ

 

 

โš™ SqlSessionManager ํด๋ž˜์Šค ์ƒ์„ฑ

์‹ค์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ, sql ์‹คํ–‰ ๋“ฑ์˜ ์—ญํ•  ํ•ด์ฃผ๋Š” ๋งค๋‹ˆ์ €

์ •๋ณด๋ฅผ ์ฝ์–ด์™€์„œ ์‹ค์ œ๋กœ DB์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์นœ๊ตฌ : SqlSessionManager

- sql ์‹คํ–‰, ์—ฐ๊ฒฐ์„ ์ˆ˜ํ–‰

 

 

ํด๋ž˜์Šค ์ƒ์„ฑ

 

 

package com.smhrd.database;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

//์‹ค์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ, sql ์‹คํ–‰ ๋“ฑ์˜ ์—ญํ•  ํ•ด์ฃผ๋Š” ๋งค๋‹ˆ์ €
public class SqlSessionManager {
	
	//์‹ค์ œ ์—ฐ๊ฒฐ, ์‹คํ–‰ ๋“ฑ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•  session์„ ์ƒ์„ฑํ•ด์ฃผ๋Š” factory
	//DB์—์„œ session์€ ์—ฐ๊ฒฐ๋ถ€ํ„ฐ sql ์‹คํ–‰ํ•˜๊ณ , ์‹คํ–‰ ํ›„ ์—ฐ๊ฒฐ ๋Š๊ธฐ๊นŒ์ง€์˜ ์ผ๋ จ์˜ ๊ณผ์ •์„ session์ด๋ผ๊ณ  ํ•จ
	public static SqlSessionFactory sqlSessionFactory;

   //ํด๋ž˜์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ : ํด๋ž˜์Šค๊ฐ€ ์ฒ˜์Œ ๋กœ๋”ฉ๋  ๋•Œ ๋”ฑ ํ•œ๋ฒˆ๋งŒ ์ˆ˜ํ–‰
	//์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜์ž๋งˆ์ž ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ > ํด๋ž˜์Šค๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๋Š” ์—ญํ• 
   static {
      //์„ค์ •์ •๋ณด ๊ฐ€์ง€๊ณ  ์˜ค๊ธฐ์œ„ํ•œ mybatis-config.xml ๊ฒฝ๋กœ ์ž‘์„ฑ
	   //์—ฐ๊ฒฐํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
      String resource = "com/smhrd/database/mybatis-config.xml";
       //์‹ค์ œ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•˜์—ฌ reader ์‚ฌ์šฉ
       //์ง€์ •ํ•œ ๊ฒฝ๋กœ ์•ˆ์˜ ํŒŒ์ผ ๋‚ด์šฉ์„ ์ฝ์„ ๋•Œ ์‚ฌ์šฉ
      Reader reader;
      //๊ฒฝ๋กœ๊ฐ€ ๋ฌธ์ž์—ด๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์Œ. ์˜คํƒ€ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ ์žˆ์Œ >> ์˜ˆ์™ธ์ฒ˜๋ฆฌ
      try {
         //๋ฌธ์ž์—ด ๊ฒฝ๋กœ๋กœ ํŒŒ์ผ์ฝ๊ธฐ
         reader = Resources.getResourceAsReader(resource);
         //์ฝ์€ ์ •๋ณด๋ฅผ ํ† ๋Œ€๋กœ DB ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ SqlSessionFactory ์ƒ์„ฑ
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
      }catch (IOException e) {
         e.printStackTrace();
      }
   }
   
      //static ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ์—์„œ ์ƒ์„ฑ๋œ factory๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ
   //๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜๊ณ  ์‹ถ์–ด. ๊ทธ๋Ÿผ get~ ํ˜ธ์ถœํ•˜๋ฉด db์—ฐ๊ฒฐ~๋Š๊ธฐ๊นŒ์ง€ ์ž‘์—…์„ ํ•ด์ฃผ๋Š” factory๋ฅผ ์ƒ์„ฑํ•ด์คŒ
   public static SqlSessionFactory getSqlSessionFactory() {
	   //static : ๊ฐ์ฒด ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉฐ ๋ฐ”๋กœ ์‹คํ–‰๋จ
	   return sqlSessionFactory;
   }
   
}

 

SqlSessionManager ํด๋ž˜์Šค ์ƒ์„ฑ์‹œ ์ฃผ์˜ํ•  ์ !

resource - ibatis.io

 

โš™ DAO์—์„œ SqlSessionManager ์‚ฌ์šฉํ•˜๊ธฐ

 

package com.smhrd.model;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.smhrd.database.SqlSessionManager;

//MyBatis DAO ์ •์˜
public class MyMemberDAO {
	//manager๊ฐ€ factory ์ƒ์„ฑ, factory๊ฐ€ session ์ƒ์„ฑ
	//SqlSessionFactory ์ƒ์„ฑ : DB ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ์„ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์„ธ์…˜์„ ์ƒ์„ฑ
	SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSessionFactory();
	
	//ํšŒ์›๊ฐ€์ž…
	public int join(MyMember member) {
		//Factory๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‹ค์ œ db ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ด์ฃผ๋Š” session์„ ์ƒ์„ฑ
		//true : auto commit - jdbc๋Š” ์ž๋™ commit์ด์—ˆ์Œ. myBatis๋Š” ์ž๋™์ปค๋ฐ‹์ด ์•„๋‹ˆ๋ฏ€๋กœ ์„ค์ •ํ•ด์ค˜์•ผ ํ•จ
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		int cnt = 0;
		try {
			//sqlSession์œผ๋กœ member๋ฅผ db์— ๋„˜๊ฒจ์ค˜์•ผ ํ•จ
			//์‚ฝ์ž… : insert, ์‚ญ์ œ : delete, ํ˜ธ์ถœ : select
			//insert(์‹ค์ œ ์‹คํ–‰ํ•  sql๋ฌธ - xmlํŒŒ์ผ namespace.id , sql ์‹คํ–‰ํ•  ๋•Œ ๋„˜๊ฒจ์ค„ ๊ฐ’ - ?)
			//?์— ์ฑ„์›Œ์งˆ ๊ฐ’์€ member์— ๋‹ด๊ฒจ์žˆ์Œ
			cnt = sqlSession.insert("MyMemberDAO.insert",member);
			//๋ฐ˜ํ™˜๊ฐ’ : sql ์‹คํ–‰ ์„ฑ๊ณต ์—ฌ๋ถ€ (์‚ฝ์ž… ์„ฑ๊ณต - 1, ์‹คํŒจ - 0)
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close(); //์ž์›๋ฐ˜ํ™˜
		}
		return cnt;
	}
}

 

MyMemberDAO ํด๋ž˜์Šค ์ƒ์„ฑ์‹œ ์ฃผ์˜ํ•  ์ !

SqlSessionMager - ์ง์ ‘ ๋งŒ๋“  ํด๋ž˜์Šค ํŒŒ์ผ ์‚ฝ์ž…

 

 

memberDAO์—์„œ sqlSession ์ƒ์„ฑ ํ›„ ์‹ค์ œ sql๋ฌธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” sql๋ฌธ์ด ๋‹ด๊น€ ํŒŒ์ผ์ด ์žˆ์–ด์•ผํ•จ!

>> mappers : ์‹ค์ œ๋กœ sql์„ ์ž‘์„ฑํ•  ํŒŒ์ผ

 

โš™ Mappers.xml ํŒŒ์ผ ์ž‘์„ฑ

 

๋„๊ตฌ ๊ฐ€์ ธ์˜ค๊ธฐ

 

 

 

 

mapper ์ƒ์„ฑ ํ›„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” mybatis-cong.xml ๊ฒฝ๋กœ ์ง€์ •ํ•ด์•ผ ํ•จ!

 

 

<?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">
<!-- namespace : mapper - dao ๋งค์นญ -->
<mapper namespace="MyMemberDAO">
	<insert id="insert" parameterType="com.smhrd.model.MyMember">
	<!-- ํ•„์ˆ˜ ์†์„ฑ : id(ํƒœ๊ทธ์‹๋ณ„๊ฐ’) -->
		<!-- #{๊ฐ€์ง€๊ณ  ์˜ฌ ํด๋ž˜์Šค์˜ ํ•„๋“œ๋ช…} -->
		insert into mymember values (#{id},#{pw},#{nick})
	</insert>
</mapper>

 

 

 

parametherType์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ ๊ฒฝ๋กœ๋กœ ์ž‘์„ฑํ•ด์•ผํ•จ!

string, int๋ฉด ๊ทธ๋ƒฅ ์ž‘์„ฑํ•ด๋„๋จ!

 

insert๋Š” int๋กœ ๋ฐ˜ํ™˜

 

Servlet์—์„œ DAO ์‚ฌ์šฉํ•˜๊ธฐ 

 

package com.smhrd.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.smhrd.model.MyMember;
import com.smhrd.model.MyMemberDAO;

public class JoinCon extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//์š”์ฒญ ๋ฐ์ดํ„ฐ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ์ง€์ •
		request.setCharacterEncoding("UTF-8");
		
		//id, pw, nick ๋ฐ›๊ธฐ - DBํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๋ช…๊ณผ ์ž๋ฃŒํ˜• ๋งž์ถ”๊ธฐ
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		String nick = request.getParameter("nick");
		
		MyMember member = new MyMember(id, pw, nick);
		
		//๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™ >> ๊ฐ’ ๋„ฃ์–ด์ฃผ๊ธฐ (MyBatis ํ™œ์šฉ)
		MyMemberDAO dao = new MyMemberDAO();
		//DAO ๊ฐ์ฒด ์ƒ์„ฑํ•˜๋Š” ์ˆœ๊ฐ„ SqlSessionFactory ์ƒ์„ฑ
		int cnt = dao.join(member); //ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ
		
		if(cnt>0) {//ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต
			System.out.println("ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต!");
		}else {//ํšŒ์›๊ฐ€์ž… ์‹คํŒจ
			System.out.println("ํšŒ์›๊ฐ€์ž… ์‹คํŒจ!");
		}
	}
}

 

๐Ÿ“– ์˜ค๋ฅ˜ ๋ฐœ์ƒ

1. ๋ถ€์ ํ•ฉํ•œ ์—ด ์œ ํ˜• : SQL๋ฌธ์ด ์ž˜๋ชป๋œ ๊ฒƒ

 

์ฒซ๋ฒˆ์งธ ์˜ค๋ฅ˜๋ฅผ ์ฐพ์•„์„œ ๋ด์•ผ ํ•จ.

 

๋งจ ์œ„ ์˜ค๋ฅ˜

 

id๊ฐ’์ด null ๊ฐ’ > ์ž˜๋ชป ๋ฐ›์•„์™”์Œ

formํƒœ๊ทธ ํ™•์ธ > servlet์—์„œ ์ž˜ ๋ฐ›๋Š”์ง€ > ...

 

formํƒœ๊ทธ : ์ด์ƒ์—†์Œ

 

servlet id ์˜คํƒ€ ๋ฐœ์ƒ

 

2. SQLSyntaxError : SQL ์ž˜๋ชป ์ž‘์„ฑํ•œ ๊ฒƒ  

 

 

mapper ํ™•์ธ

 

values ์˜คํƒ€ ๋ฐœ์ƒ

 

3. NoClassDefFoundError : ํด๋ž˜์Šค๋ฅผ ๋ชป๊ฐ€์ ธ์™”์„ ๋•Œ 

 

 

(SqlSessionManager con-figํŒŒ์ผ์—์„œ ๊ฒฝ๋กœ ์ž˜๋ชป๋จ)

 

 

3. ivalid character

์“ฐ๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ ์ผ์„ ๋•Œ ๋‚˜ํƒ€๋‚จ

 

 

 

 

์„ธ๋ฏธ์ฝœ๋ก  ์ผ์„ ๋•Œ

 

4. namespace ์ž˜๋ชป ์ง€์ •