기록해! 정리해!

자바 - board 인터페이스 본문

JAVA

자바 - board 인터페이스

zsuling 2022. 7. 7. 17:59

인터페이스의 목적 : 다용성

1. BoardVo 생성

 

  private String id;
  private String pwd; 

--private 을 받으면 꼭 getter and setter 추가

--to String 추가 : 리턴하기 전에 값이 정상적으로 넘어오는지 확인하기위해서 사용

package test0707;

public class BoardVo {
  private String id;
  private String pwd;
  
  private String ch1;
  private String ch2;
  
public String getCh1() {
	return ch1;
}
public void setCh1(String ch1) {
	this.ch1 = ch1;
}
public String getCh2() {
	return ch2;
}
public void setCh2(String ch2) {
	this.ch2 = ch2;
}
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getPwd() {
	return pwd;
}
public void setPwd(String pwd) {
	this.pwd = pwd;
}
@Override
public String toString() { 
	return "BoardVo [id=" + id + ", pwd=" + pwd + "]";
}
  
  
}

2. 인터페이스 Board 생성

: 메소드를 정의하는 곳

 

해설정리)

코드)

package board;

import java.util.List;

public interface Board {
   // C-insert R-select U-update D-delete  select만 return있어서 제일 마지막에 씀
   
   public void insert(String str1, String str2); //매개변수 하나씩 기입해서 받음
   public void insert(BoardVo vo); //BoardVo에 있는 매개변수를 한번에 받을 수 있음
   public int insert2(BoardVo vo); //int로 받는 경우는 성공하면 1을 찍어서 확인 할 때
   
   public void update(Board vo);
   
   public void delete(String str);//삭제할때는 pk를 기준으로 하나만받아서 삭제하기때문에 String하나로만 나타내는게 일반적
   public int delete2(String str);
   
   
   public BoardVo selectOne(String str); //리턴 하나만 값 받을때
   public List<BoardVo> selectAll(BoardVo vo); //여러값 받을때 리스트형으로
   public List<BoardVo> selectAll(String str1,String str2); //검색의 ch1,ch2받아서 출력했던 것 처럼

   public int count();
   


}

 

3. SchoolBoard 클래스 생성

 

public class SchoolBoard implements Board{ 

하고 메소드 추가하기

 

이제 여기서 관련 메소드에 대해 코딩을 하면 됨

 

4. BoardMain 생성

package board;

public class BoardMain {

   public static void main(String[] args) {

5. 매개변수에 값 저장하기 / vo를 이용해 값 저장하기

1) SchoolBoard 

@Override
   public void insert(String str1, String str2) {

      System.out.println("아이디:"+str1+ "   비밀번호:"+str2 +"   저장완료");
   }

   @Override
   public void insert(BoardVo vo) {
      System.out.println("BoardVo vo 이용하기");
      System.out.println("아이디:"+vo.getId()+ "   비밀번호:"+vo.getPwd()+"   저장완료");
   }

1-1) BoardMain

      //매개변수에 값 저장하기
      Board s = new SchoolBoard();
      s.insert("김", "최");
      
      // Vo를 이용해 값 저장하기
      BoardVo vo = new BoardVo();
      vo.setId("영희");
      vo.setPwd("asdf");
      s.insert(vo);

6. vo리턴받기

2) SchoolBoard 

 @Override
   public BoardVo selectOne(String str) {
      String str1 = "둘리";
      String str2 = "abc";
      
      BoardVo vo = new BoardVo();
      vo.setId(str + "-" + str1);
      vo.setPwd(str2);
      
      return vo;
   }

2-1) BoardMain

 // VO 리턴받기
      BoardVo m = s.selectOne("1001");
      System.out.println("아이디:" + m.getId() + " ,암호:" + m.getPwd());

7.  List

3) SchoolBoard 

@Override
   public List<BoardVo> selectAll(BoardVo vo) {
      List<BoardVo> li = new ArrayList<BoardVo>();
	   
      BoardVo m1 = new BoardVo();
      m1.setId("영심이");
      m1.setPwd("1234");
      li.add(m1);
      
      BoardVo m2 = new BoardVo();
      m2.setId("하늘이");
      m2.setPwd("1212");
      li.add(m2);
      
      BoardVo m3 = new BoardVo();
      m3.setId("지효");
      m3.setPwd("3333");
      li.add(m3);
      
      return li;
   }

 

3-1) BoardMain

//List<BoardVo>
      System.out.println();
      System.out.println("======[List<BoardVo>출력하기]======");
    		 
      List<BoardVo> li = s.selectAll(null);
      for(int i=0; i<li.size(); i++) {
    	 BoardVo m1= li.get(i);   
      System.out.println(m1.getId()+ ":" +m1.getPwd());
      }


º 복습

Board

package board;

import java.util.List;

public interface Board {
   void insert(BoardVo vo);
   BoardVo content(int k);
   List<BoardVo> selectAll();
   
}

BoardImpl

package board;

import java.util.List;

public class BoardImpl implements Board {

	@Override
	public void insert(BoardVo vo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BoardVo content(int k) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoardVo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}
	

}

BoardService

package board;

import java.util.List;

public interface BoardService {

void insert(BoardVo vo);
   
   BoardVo content(int k);
   
   List<BoardVo> selectAll();
}

BoardServiceImpl

package board;

import java.util.List;

public class BoardServiceImpl implements  BoardService{

	@Override
	public void insert(BoardVo vo) {
      // TODO Auto-generated method stub
		return null;
	}

	@Override
	public BoardVo content(int k) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoardVo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

}

BoardVo

package board;

public class BoardVo {

	private int idx;
	private String title;
	private String name;
	public int getIdx() {
		return idx;
	}
	public void setIdx(int idx) {
		this.idx = idx;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "BoardVo [idx=" + idx + ", title=" + title + ", name=" + name + "]";
	}
}

MainTest

package board;

public class MainTest {

	public static void main(String[] args) {

		BoardService service = new BoardServiceImpl();
		BoardVo vo = new BoardVo();
		vo.setIdx(0);
		vo.setName("영심이");
        vo.setTitle("자바 공부하는 중");
		service.insert(vo);
	}

}

1. main은 service를 부르고service는 board를 부르기

--MainTest

        BoardService service = new BoardServiceImpl();
		BoardVo vo = new BoardVo();
		vo.setIdx(0);
		vo.setName("영심이");
        vo.setTitle("자바 공부하는 중");
		service.insert(vo);

--BoardServiceImpl

private Board board = null; //전역변수
	
	public BoardServiceImpl() { //생성자
	 board = new BoardImpl(); // 필드 
	}
	
	@Override
	public void insert(BoardVo vo) {
      System.out.println("BoardSeviceImpl(insert) -->" + vo);	
      board.insert(vo); 
	}

--BoardImpl

	@Override
	public void insert(BoardVo vo) {
		 System.out.println("BoardImpl(insert) -->" + vo);				 
	}

2. 트랜잭션

--Board

 void transactionBegin();
   void transactionEnd();

--BoardImpl

	@Override
	public void transactionBegin() {
        System.out.println("=====> 트랜잭션 시작");		
	}

	@Override
	public void transactionEnd() {
        System.out.println("=====> 트랜잭션 끝");				
	}

--BoardServiceImpl

	@Override
	public void insert(BoardVo vo) {
	  
      System.out.println("BoardSeviceImpl(insert) -->" + vo);	
	  board.transactionBegin();
      board.insert(vo); 
      board.transactionEnd();
	}

3. 종단에 있는 값을 MainTest에 리턴받아서 출력해보기

 

 /*1004입력
학번 : 1004
이름 : 하늘이
제목 : 함수 공부하는 중*/

 

--종단(BoardImpl)

@Override
	public BoardVo content(int k) {

		BoardVo vo = new BoardVo();
		vo.setIdx(k);
		vo.setName("하늘이");
		vo.setTitle("함수 공부하는 중");
		
		return vo;
	}

--MainTest

        BoardVo m = service.content(1004);
		int idx = m.getIdx();
		String name = m.getName();
		String title = m.getTitle();
		
		System.out.println("학번: "+idx);
		System.out.println("이름: "+name);
		System.out.println("제목: "+title);

--BoardServiceImpl

@Override
	public BoardVo content(int k) {
		
			return board.content(k);
	}

 

4. MainTest에 찍어보기

 

/*
1001 , 둘리1, 열심히 공부 중1
1002 , 둘리2, 열심히 공부 중2
1003 , 둘리3, 열심히 공부 중3
*/

 

--BoardImpl

	@Override
	public List<BoardVo> selectAll() {
		List<BoardVo> li = new ArrayList<BoardVo>();
		
		   
		for(int i=1; i<=3; i++) {
			BoardVo m = new BoardVo();
			m.setIdx(1000+i);
			m.setName("둘리"+i);
			m.setTitle("열심히 공부 중"+i);
			
			li.add(m);
		}
	return li;
	}

--BoardServiceImpl

	@Override
	public List<BoardVo> selectAll() {
		
		return board.selectAll();	
		
	}

--MainTest

	System.out.println();
		System.out.println("======[List<BoardVo>출력하기]======");
		
		List<BoardVo> li = service.selectAll();
		for(int i=0; i<li.size(); i++) {
			BoardVo m1 = li.get(i);
			System.out.println("==>"+m1.toString());


º 복습

BoardVo

package board;

public class BoardVo {

	private long idx;
    private String name;
	private String pwd;
    private int age;
    
    public long getIdx() {
		return idx;
	}
	public void setIdx(long idx) {
		this.idx = idx;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "BoardVo [idx=" + idx + ", name=" + name + ", pwd=" + pwd + ", age=" + age + "]";
	}
	
}

Dao

package board;

import java.util.List;

public interface Dao {
	
	void insert(BoardVo vo);
	void delete(long idx);
	BoardVo selectOne(long idx);
	List<BoardVo> selectAll();
	int count();
	
	void transactionBegin();
	void transactionEnd();
}

DaoImpl

package board;

import java.util.List;

public class DaoImpl implements Dao{

	@Override
	public void insert(BoardVo vo) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(long idx) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BoardVo selectOne(long idx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoardVo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void transactionBegin() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void transactionEnd() {
		// TODO Auto-generated method stub
		
	}

}

Service

package board;

import java.util.List;

public interface Service {

	void insert(BoardVo vo);
	void delete(long idx);
	BoardVo selectOne(long idx);
	List<BoardVo> selectAll();
	int count();
	
}

ServiceImpl

package board;

public class ServiceImpl implements Service {

	@Override
	public void insert(BoardVo vo) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(long idx) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BoardVo selectOne(long idx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoardVo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

}

TestBoard

package board;

public class TestBoard {

	public static void main(String[] args) {
       Service service = new ServiceImpl();
       
       service.insert(null); 
       service.delete(0);
       service.selectOne(0);
       service.selectAll();
       service.count();

	}

}

1. TestBoard 변수만들고 return형 만들기

( insert,delete 는 void값이니까 안해도 됨 )

변수의 타입 = 리턴형

  BoardVo m = service.selectOne(0); 
       List<BoardVo> li = service.selectAll();
       int count = service.count();

2. ServiceImpl에서 프린트문 적고 출력하기

3. DaoImpl에서 프린트문 작성하고 출력하기

 

4. TestBoard에 둘리 입력해서 출력해보기

--TestBoard

package board;

import java.security.PublicKey;
import java.util.List;

public class TestBoard {

	public static void main(String[] args) {
       Service service = new ServiceImpl();
       
       //이름 : 둘리 추가
       
       BoardVo m1 = new BoardVo();
       m1.setName("둘리");
       service.insert(m1);
       
       service.delete(0);
       
       BoardVo m = service.selectOne(0); 
       List<BoardVo> li = service.selectAll();
       int count = service.count();

    	
    }
       
	}

--ServiceImpl

private Dao dao = null;
	
	public ServiceImpl(){
		dao = new DaoImpl();
	}
	
	@Override
	public void insert(BoardVo vo) {
     System.out.println("==>public void insert(BoardVo vo) 실행");		
    
     String name = vo.getName();
     String str = name+" 학생";
     vo.setName(str);
     
     dao.insert(vo);
     
	}

--DaoImpl

public class DaoImpl implements Dao{

	@Override
	public void insert(BoardVo vo) {
       System.out.println("====> DAO : insert(BoardVo vo)" + vo.getName());		
	}

5. 100넣으면 삭제되게 만들기

--TestBoard

service.delete(100);

--ServiceImpl

	@Override
	public void delete(long idx) {
		System.out.println("==>public void delete(BoardVo vo)실행");	
		dao.delete(idx);
	}

--DaoImpl

	@Override
	public void delete(long idx) {
		 System.out.println("====> DAO : insert(BoardVo vo)" + idx + "번 삭제");		
	}

Comments