List

목요일, 12월 10, 2015

Mybatis 환경에서 답변형 게시판 구축 - 1


전체 project 구조

  • Package - message 
    • 에러 메세지 혹은 다국어 처리를 위해서
    • .properties file
  • Package - resource
    • BoardMapper.xml 
    • 모든 sql문들의 저장소
    • resultMap 은 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<mapper namespace="springboard.repository.BoardDao">
    <!-- resultMap선언
    getBoard1 boardlist
     -->
    <resultMap type="board" id="boardRst"/>
    <resultMap type="restore" id="restoreRst"/>
    
    <select id ="getBoard1"
        resultMap="boardRst" parameterType="int">
        select * from boardsjh 
        where no = #{no}
        </select>
    <select id="boardlist" resultMap="boardRst">
        select * from boardsjh
        order by no asc
    </select>    
    
    <insert id="insBoard" parameterType="board">
    
        <!-- 자동 증가 처리하는 부분을 변수로 지정함
            keyProperty : 해당 key 메서드 setNo설정
            order: Before - sql 문 사용되기 전에 활용
         -->
        <selectKey resultType="int" keyProperty="no" order="BEFORE">
              SELECT  board_sjh_seq.nextval FROM DUAL
        </selectKey>
        
         INSERT INTO BOARDSJH VALUES ( #{no}, #{title} , 
#{content} ,
          #{writer}, sysdate, sysdate, #{refno}, 0)
          
    </insert>
    <insert id="insRestore" parameterType="restore">
        INSERT INTO RESTORE_SJH VALUES(res_sjh_seq.nextval, 
board_sjh_seq.currval, 
        #{fold}, #{fname}, sysdate, #{etc})
    </insert>
    
     <select id="getRestoreList" parameterType="int" resultMap="restoreRst">
         select * from RESTORE_SJH where no=#{no}
     </select>
  
</mapper>
cs

  • selectKey keyProperty="키값"
  • "키값"에 해당 sql문의 결과를 할당
  • order = BEFORE   - 쿼리문이 실행되기전에 실행
1
2
3
<selectKey resultType="int" keyProperty="no" order="BEFORE">
         SELECT  board_sjh_seq.nextval FROM DUAL
</selectKey>
keyProperty 로 선언한 no가
#{no}형식으로 사용됨
cs


  • VO 패키지단
    • BoradVO
    • file upload를 위해서 MultiparFile 속성값을 선언
    • Serializable 상속은 필수 !!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class BoardVO implements Serializable{
    private int no;
    private String title;
    private String content;
    private String writer;
    private Date regdte;
    private Date updte;
    private int refno;
    private int readcnt;
    //파일 등록 처리를 위하여
    private MultipartFile[] files;
    public MultipartFile[] getFiles() {
        return files;
    }
    public void setFiles(MultipartFile[] files) {
        this.files = files;
    }
cs



  • Service 단
    • ApplicationContextAware 상속 필요
    • WebApplicationContext 를 선언
    • Method override 필요
1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class BoardService implements ApplicationContextAware{
    private WebApplicationContext context= null;
@Override
    public void setApplicationContext(ApplicationContext aca)
 throws BeansException {
        // TODO Auto-generated method stub
        this.context=(WebApplicationContext)aca;
    }
    
cs

  • 파일 업로드 처리
    • service 단에 필요한 method
    • cpath로 처리해야 (리얼 path)로 처리해야 다운로드가능

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    private void upload(MultipartFile[] mpf){
        //어느 서버로 가던지 파일의 현재 주소를 유지하기위해서
        
        //물리적으로 Stream 파일 업로드
        //웹서버를 구동해서 웹서버 안에 특정한 경로를 지정
        String cpath="C:/Bootcamp/es/springboard/WebContent/z01_upload";
        String path=context.getServletContext().getRealPath("z01_upload");
        File f;
        RestoreVO res;
        for( MultipartFile m : mpf){
            //OriginalFilename= "진짜배기 파일이름"
            f= new File(path,m.getOriginalFilename());
            //stream 형식의 파일 실제파일로 전환
            res=new RestoreVO(cpath, m.getOriginalFilename(), "file");
            
            //stream 형식의 파일 ==> 실제파일로 전환하면 저장됨
            try {
                
                m.transferTo(f);
                
                //eclipse 기반 톰캣 을 가동할시만 처리 필요
                Files.copy(Paths.get(path+"/"+m.getOriginalFilename()), 
                        Paths.get(cpath+"/"+m.getOriginalFilename()),
                         StandardCopyOption.REPLACE_EXISTING);
                
                //file info 입력
                dao.insRestore(res);
                
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


댓글 없음:

댓글 쓰기