-------------------------------------------------------------------------------------
<?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="movie">
</mapper>
-------------------------------------------------------------------------------------
6. DAO▷ /src/movie/MovieDAO.java
-------------------------------------------------------------------------------------
package movie;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MovieDAO {
private SqlSessionFactory factory;
public MovieDAO() {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); // bin
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 등록
* @param title 영화명
* @param good 평점
* @param name 주연
*/
public int create(MovieVO movieVO) { // Call By Reference
int cnt = 0;
SqlSession sqlSession = factory.openSession();
sqlSession.commit();
sqlSession.close();
return cnt;
}
/**
* 목록
*/
public List<MovieVO> list() {
List<MovieVO> list = null;
SqlSession sqlSession = factory.openSession();
sqlSession.commit();
sqlSession.close();
return list;
}
/**
* 조회
* @param movieno 조회할 번호
*/
public MovieVO read(int movieno) {
MovieVO movieVO = new MovieVO();
SqlSession sqlSession = factory.openSession();
sqlSession.commit();
sqlSession.close();
return movieVO;
}
/**
* 수정
* @param movieno 영화번호
* @param title 영화명
* @param good 평점
* @param name 주연
*/
public int update(MovieVO movieVO) {
int cnt = 0;
SqlSession sqlSession = factory.openSession();
sqlSession.commit();
sqlSession.close();
return cnt;
}
/**
* 삭제
* @param movieno 영화번호
*/
public int delete(int movieno) {
int cnt = 0;
SqlSession sqlSession = factory.openSession();
sqlSession.commit();
sqlSession.close();
return cnt;
}
}
-------------------------------------------------------------------------------------
7. Process▷ /src/movie/MovieProcess.java
-------------------------------------------------------------------------------------
package movie;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MoveAction;
public class MovieProc {
MovieDAO movieDAO = null;
Scanner in = null;
public MovieProc() {
movieDAO = new MovieDAO();
in = new Scanner(System.in);
}
public void menu() {
int menu = 0; // 지역 변수
while(true) {
System.out.println();
System.out.println("Movie chart(Mybatis ver 4.0)");
System.out.println("─────────────");
System.out.println(" 1. 등록");
System.out.println(" 2. 목록");
System.out.println(" 3. 조회");
System.out.println(" 4. 수정");
System.out.println(" 5. 삭제");
System.out.println(" 9. 종료");
System.out.println("─────────────");
System.out.print("메뉴 입력: ");
menu = Integer.parseInt(in.nextLine());
System.out.println();
if (menu == 1) {
create();
} else if (menu == 2) {
list();
} else if (menu == 3) {
read();
} else if (menu == 4) {
update();
} else if (menu == 5) {
delete();
} else if (menu == 9) {
System.out.println("즐거운 하루되세요~~");
System.exit(0);
}
}
}
public void create() {
String title = "";
double good = 0.0;
String name = "";
System.out.print("제목: " );
title = in.nextLine();
System.out.print("추천(0.0 ~ 10.0): ");
good = Double.parseDouble(in.nextLine());
System.out.print("주연: ");
name = in.nextLine();
// movieDAO.create(title, good, name);
MovieVO movieVO = new MovieVO();
// movieVO.setMovieno(movieno);
movieVO.setTitle(title);
movieVO.setGood(good);
// movieVO.setRdate(rdate);
movieVO.setName(name);
movieDAO.create(movieVO);
}
public void list() {
List<MovieVO> list = movieDAO.list();
int count = list.size(); // 레코드 갯수 산출
for (int i=0; i < count; i++) {
MovieVO movieVO = list.get(i);
System.out.print(movieVO.getMovieno() + ". ");
System.out.print(movieVO.getTitle() + ": ");
System.out.print(movieVO.getName());
System.out.print("(" + movieVO.getGood() +") ");
System.out.print(movieVO.getRdate().substring(0, 10));
System.out.println();
System.out.println("───────────────────────────────");
}
}
public void read() {
System.out.print("조회할 번호: ");
int movieno = Integer.parseInt(in.nextLine());
MovieVO movieVO = movieDAO.read(movieno);
System.out.print(movieVO.getMovieno() + ". ");
System.out.print(movieVO.getTitle() + ": ");
System.out.print(movieVO.getName());
System.out.print("(" + movieVO.getGood() +") ");
System.out.print(movieVO.getRdate().substring(0, 10));
System.out.println();
System.out.println("───────────────────────────────");
}
public void update() {
// public void update() {
int movieno = 0;
String title = "";
double good = 0.0;
String name = "";
System.out.print("영화번호: ");
movieno = Integer.parseInt(in.nextLine());
System.out.print("제목: " );
title = in.nextLine();
System.out.print("추천(0.0 ~ 10.0): ");
good = Double.parseDouble(in.nextLine());
System.out.print("주연: ");
name = in.nextLine();
// movieDAO.update(movieno, title, good, name);
MovieVO movieVO = new MovieVO();
movieVO.setMovieno(movieno);
movieVO.setTitle(title);
movieVO.setGood(good);
// movieVO.setRdate(rdate);
movieVO.setName(name);
movieDAO.update(movieVO);
}
public void delete() {
int movieno = 0;
System.out.print("삭제할 영화번호: ");
movieno = Integer.parseInt(in.nextLine());
movieDAO.delete(movieno);
}
}
-------------------------------------------------------------------------------------
8. Main class
- 실행 방법C:
CD\
CD 201812_java\ws_java\movieMybatis\bin
java -cp %CLASSPATH%;C:/201812_java/mysql-connector.jar;C:/201812_java/mybatis-3.4.1.jar movie.MovieProcUse
- JAR 실행
java -jar movieMybatis.jar
▷ /src/movie/MovieProcessUse.java
-------------------------------------------------------------------------------------
package movie;
public class MovieProcUse {
public static void main(String[] args) {
MovieProc movieProc = new MovieProc();
movieProc.menu();
}
}
-------------------------------------------------------------------------------------