본문 바로가기
강의

Spring Lec04

by IT새내기IM 2023. 4. 28.

■ web.xml : 추가 ( UTF-8 처리 필터 등록 : 한글 깨짐 방지 )

. web.xml 추가

 

<filter>

<filter-name>encoding</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

 

<filter-mapping>

<filter-name>encoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

== 설명 ==

UTF-8을 사용하는 경우는 스프링에서 제공하는 필터를 등록해 주는 것이 좋습니다.

캐릭터셋을 이용해도 한글이 깨진다면 꼭 추가해 줄것.

 

 

== 오류 발생 시 ( 경로 수정 ) ==

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://Java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


■ servlet-context.xml ( resources 절대경로 설정 )

. == 기존 ==

 

. servlet-context.xml

 <resources mapping="/resources/**" location="/resources/" />

. home.jsp

 <img src="/resources/img/logo.jpg">

 

. == 수정 ==

 

. servlet-context.xml

 <resources mapping="/**" location="/resources/" />

. home.jsp

 <img src="/img/logo.jpg">


● 게시판 MVC ( ① 모델 및 mapper 구현 , 테스트 )


모든 게시물 관리의 시작 형태는 등록(create) , 조회(read) , 수정(update) , 삭제(delete)를 기준으로 한다. 
 
여기에 반드시 하나가 더 추가되는 것이 화면상에 데이터의 목록을 보여주는 리스트 기능이라고 할 수 있다.
 
 
방식 URI 설명
get  /board/register   게시물의 등록 페이지를 보여준다. 
post  /board/register   게시물을 실제로 등록한다.
get  /board/read?bno=xxx  특정 번호의 게시물을 조회, 보여준다.
get  /board/mod?bno=xxx  게시물의 수정 화면으로 이동한다.
post  /board/mod  게시물을 수정한다.
post  /board/remove  게시물을 삭제한다.
get  /board/list  게시물의 목록을 보여준다.

 


https://dancepkt.cafe24.com/bbs/board.php?bo_table=spring&wr_id=70&page=5 

 

BoardVO.java > SPRING | ∴ dancePKT

사이트 내 전체검색

dancepkt.cafe24.com

https://dancepkt.cafe24.com/bbs/board.php?bo_table=spring&wr_id=71&page=5 

 

BoardDAO.java ( interface ) > SPRING | ∴ dancePKT

사이트 내 전체검색

dancepkt.cafe24.com

 

 

<!-- <![CDATA[ ]] 쓰는 이유 -->
<!-- sql 구문안에 비교 연산자가 들어갈 경우(특수문자) 문자열로 인식하게 하기 위해서 ... -->


<![CDATA[
select * from tbl_board where bno > 0 order by bno desc
]]>

 

https://dancepkt.cafe24.com/bbs/board.php?bo_table=spring&wr_id=73&page=5 

 

BoardDAOImpl.java > SPRING | ∴ dancePKT

사이트 내 전체검색

dancepkt.cafe24.com


※ mybitis-config.xml ( <typeAliases> 추가 )

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE configuration

PUBLIC "-//mybatis.org//DTD Config 3.0//En"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

 

<configuration>

<typeAliases>

<package name="org.zerock.domain"/>

</typeAliases>

</configuration>

 

== 설명 ==

XML Mapper를 이용하는데 있어서 매번 parameterType이나 resultType을 패키지까지 포함된 클래스명을 작성하는 일이 번거롭다면 MyBatis의 설정 파일인 mybatis-config.xml을 사용하여 <typeAliases>를 작성해 줄 수 있습니다.

 

<typeAliases>는 하위 요소로 <package>와 <typeAlias> 요소를 가질 수 있습니다. 

이 요소들을 이용하면 매번 parameterType이나 resultType에 사용하는 클래스의 이름을 'org.zerock.domain'은 생략한 채로 표현하는 것이 가능합니다.

 


■ 계층별 구현 ( 설명 참조 ) ( 보통 Transaction 처리 할때 사용)

 비즈니스 계층은 고객의 요구사항이 반영되는 영역이다.

 비즈니스 영역에 만들어지는 클래스나 인터페이스는 반드시 요구사항과 일치하도록 설계돼야 한다.

 

 스프링에서 비즈니스 영역은 일반적으로 서비스(이하 Service)라는 이름을 칭한다.

 

[ 개발 순서 ]

1) 요구사항을 메소드로 정리해서 xxxService 인터페이스를 정의

2) xxxServiceImpl 이라는 구현 객체를 만들어 주는 순서로 진행

 

이후의 모든 예제에서 Service 객체라고 하면 구현 객체를 의미한다.

컨트롤러와 DAO 사이의 접착제 역할을 한다.

중간에 비즈니스 영역을 구분해서 개발하면 개발의 양이 늘어나는 것은 사실이다.

그럼에도, 몇 가지 이유가 있기 때문에 굳이 게층을 분리해서 개발한다.

 

 비즈니스 계층은 고객마다 다른 부분을 처리할 수 있는 완충장치 역할을 한다.

 각 회사마다 다른 로직이나 규칙을 데이터베이스에 무관하게 처리할 수 있는 완충 영역으로 존재할 필요가 있다.

 컨트롤러와 같은 외부 호출이 영속 계층에 종속적인 상황을 막아준다.

 만일 컨트롤러가 직접 영속 계층의 데이터베이스를 이용하게 되면 트랜젝션의 처리나 예외의 처리 등 모든 로직이 컨트롤러에 집중된다.

 비즈니스 계층은 컨트롤러로 하여금 처리해야 하는 일을 분업하게 만들어 준다.

 

 비즈니스 계층의 구현은 로직에 필요한 데이터베이스 관련 객체들을 모아서 자신이 원하는 일을 처리하는 용도이다.


◆ root-context.xml ( base-package : service 추가 )

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd

http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd

http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

 

<!-- Root Context: defines shared resources visible to all other web components -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<property name="url" value="jdbc:mysql://localhost:3306/dancepkt"></property>

<property name="username" value="root"></property>

<property name="password" value="1111"></property>

</bean>

 

<!-- 추가 구문 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="configLocation" value="classpath:/mybatis-config.xml"></property>

<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property>

</bean>

 

<!-- sql 실제 구문 연동 -->

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">

<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>

</bean>

 

<context:component-scan base-package="org.zerock.persistence"></context:component-scan>

<context:component-scan base-package="org.zerock.service"></context:component-scan>

 

</beans>

 


BoardService.java ( interface )


 

package org.zerock.service;

 

import java.util.List;

 

import org.zerock.domain.BoardVO;

 

public interface BoardService {

 

public void regist(BoardVO board) throws Exception;

public BoardVO read(Integer bno) throws Exception;

public void modify(BoardVO board) throws Exception;

public void remove(Integer bno) throws Exception;

public List<BoardVO> listAll() throws Exception;

 

}


 

 


 

'강의' 카테고리의 다른 글

Spring Lec06  (0) 2023.05.03
Spring Lec05  (0) 2023.05.03
Spring Lec03  (0) 2023.04.28
Spring Lec02  (0) 2023.04.27
Spring Lec 01  (0) 2023.04.27

댓글