java

mvc 패턴 구성요소 이해하기

Gyona 2024. 7. 18. 10:30

Java mvc 패턴 애플리케이션의 모듈화, 재사용성, 유지보수성을 높일 수 있다.

기존에 만들어져있는 구조를 잘 활용하여 새로운 메뉴를 만들거나 기능을 추가하는데 도움이된다.

 

 

기본작업 >     DB생성하기

CREATE TABLE TB_IM_DAILYPROCESSING(
IDX INT AUTO_INCREMENT PRIMARY KEY,
REGIST_DATE DATETIME,
BRANCH VARCHAR(255),
ISSUE_YN CHAR(1),
TASK_YN CHAR(1),
REGIST_DEPARTMENT VARCHAR(255),
REGIST_POSITION VARCHAR(255),
REGIST_NAME VARCHAR(255),
REQUEST_CONTENT TEXT,
RESOLUTION_CONTENT TEXT,
RESOLUTION_RESULT VARCHAR(255),
ASSIGNEE VARCHAR(255),
NOTES TEXT
);

 

DB쿼리는 상황에 맞게 적절히 생성하면 된다.

 

 

1.     Mvc 패턴 구성 

VO -> service -> serviceimp -> dao -> sqlmap -> sqlmap연결-> controller -> jsp 생성

 

1)     VO

DB 컬럼명따서 클래스의 속성명을 정해준다.

카멜표기법으로 _뒤는 대문자로 표시하고 그외는 소문자이다.(개발환경에 따라 표기법은 다를수 있다)

 

public class DailyProcessingVO {
    private String idx;
    private String registDate;

    public String getIdx() {
        return idx;
    }

    public void setIdx(String idx) {
        this.idx = idx;
    }

 클래스

DailyProcessingVO

 

속성 or 필드

idx,registDate

 

 

 

2)     Service

Insert, update, select 등 메서드를 구현해준다.

반환타입 String인지 int인지 잘 설정해주고

매개변수와 예외를 설정해준다.

 

public interface DailyProcessingService {
    String insertDailyProcessing(DailyProcessingVO vo) throws Exception;
    void updateDailyProcessing(DailyProcessingVO vo) throws Exception;
}

 

메서드

insertDailyProcessing

 

반환타입

String

 

매개변수

DailyProcessingVO vo

 

예외선언

throws Exception

 

 

3)     impl

인터페이스 구현 클래스인 impl은 기능과 역할을 정할수 있다.

클래스와 상속, 애너테이션, 필드, 메서드를 적절하게 설정해준다.

@Service("DailyProcessingService")
public class DailyProcessingServiceImpl extends AbstractServiceImpl implements DailyProcessingService {
	
    @Resource(name="DailyProcessingDAO")
    private DailyProcessingDAO dailyProcessingDAO;

    public String insertDailyProcessing(DailyProcessingVO vo) throws Exception {
    	log.debug(vo.toString());
    	dailyProcessingDAO.insertDailyProcessing(vo);    	
        return null;
    }

 

애너테이션

@Service("DailyProcessingService")

 

클래스

DailyProcessingServiceImpl

 

상속

AbstractServiceImpl

 

인터페이스

DailyProcessingService

 

메서드

public String insertDailyProcessing(DailyProcessingVO vo) throws Exception

 

비즈니스 메서드

insertDailyProcessing

 

객체

DailyProcessingVO

 

 

 

4)     DAO

Data Access Object 의 약자로 데이터 접근 로직을 설정한다.

DAO메서드, CRUD(create,read,update,delete)메서드, 메서드 시그니처, 구현을 해주면서 SQL과 연결한다. SQL.xml의 매핑이름을 잘 설정해야함

 

@Repository("DailyProcessingDAO")
public class DailyProcessingDAO extends EgovAbstractDAO {


    public String insertDailyProcessing(DailyProcessingVO vo) throws Exception {
        return (String)insert("dailyProcessingDAO.insertDailyProcessing", vo);
    }

  
    public DailyProcessingVO selectDailyProcessing(DailyProcessingVO vo) throws Exception {
        return (DailyProcessingVO) selectByPk("dailyProcessingDAO.selectDailyProcessing", vo);
    }

   
    public List selectDailyProcessingList(DailyProcessingDefaultVO searchVO) throws Exception {
        return list("dailyProcessingDAO.selectDailyProcessingList", searchVO);
    }

   
    public int selectDailyProcessingListTotCnt(DailyProcessingDefaultVO searchVO) {
        return (Integer)getSqlMapClientTemplate().queryForObject("dailyProcessingDAO.selectDailyProcessingListTotCnt", searchVO);
    }

 

 

5)     Sqlmap (경로 src/main/resources/egoveframework/sqlmap)

Namespace, Type Alias는 기존꺼 그대로 사용하고 Result Map을 컬럼명에 맞게 변경해준다.

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="DailyProcessing">
	
	<typeAlias  alias="egovMap" type="egovframework.rte.psl.dataaccess.util.EgovMap"/>
	<typeAlias  alias="dailyProcessingSearchVO" type="egovframework.home.im.dailyProcessing.service.DailyProcessingDefaultVO"/>
	
	<resultMap id="dailyProcessing" class="egovframework.home.im.dailyProcessing.service.DailyProcessingVO">
		<result property="idx" column="IDX" columnIndex="1"/>
        <result property="registDate" column="REGIST_DATE" columnIndex="2"/>
        <result property="branch" column="BRANCH" columnIndex="3"/>
        <result property="issueYn" column="ISSUE_YN" columnIndex="4"/>
        <result property="taskYn" column="TASK_YN" columnIndex="5"/>
        <result property="registDepartment" column="REGIST_DEPARTMENT" columnIndex="6"/>
        <result property="registPosition" column="REGIST_POSITION" columnIndex="7"/>
        <result property="registName" column="REGIST_NAME" columnIndex="8"/>
        <result property="requestContent" column="REQUEST_CONTENT" columnIndex="9"/>
        <result property="resolutionContent" column="RESOLUTION_CONTENT" columnIndex="10"/>
        <result property="resolutionResult" column="RESOLUTION_RESULT" columnIndex="11"/>
        <result property="assignee" column="ASSIGNEE" columnIndex="12"/>
        <result property="notes" column="NOTES" columnIndex="13"/>
	
	</resultMap>
	
	<insert id="dailyProcessingDAO.insertDailyProcessing">
    <![CDATA[
    INSERT INTO TB_IM_DAILYPROCESSING 
        (  REGIST_DATE
        , BRANCH
        , ISSUE_YN
        , TASK_YN
        , REGIST_DEPARTMENT
        , REGIST_POSITION
        , REGIST_NAME
        , REQUEST_CONTENT
        , RESOLUTION_CONTENT
        , RESOLUTION_RESULT
        , ASSIGNEE
        , NOTES
        )
    VALUES 
        ( SYSDATE()
        , #branch#
        , #issueYn#
        , #taskYn#
        , #registDepartment#
        , #registPosition#
        , #registName#
        , #requestContent#
        , #resolutionContent#
        , #resolutionResult#
        , #assignee#
        , #notes#
        )
    ]]>
</insert>


	
	<update id="dailyProcessingDAO.updateDailyProcessing">
    <![CDATA[
    UPDATE TB_IM_DAILYPROCESSING
    SET
    IDX = #idx#   
    	,REGIST_DATE = SYSDATE()
        , BRANCH = #branch#
        , ISSUE_YN = #issueYn#
        , REQUEST_CONTENT = #requestContent#
        , RESOLUTION_CONTENT = #resolutionContent#
        , RESOLUTION_RESULT = #resolutionResult#
        , ASSIGNEE = #assignee#
        , NOTES = #notes#
    WHERE IDX = #idx#
    ]]>
</update>

	
	
	<delete id="dailyProcessingDAO.deleteDailyProcessing">
			<![CDATA[
			DELETE FROM TB_IM_DAILYPROCESSING
			WHERE IDX = #idx#
			]]>
	</delete>
	
	<select id="dailyProcessingDAO.selectDailyProcessing" resultMap="dailyProcessing">
			<![CDATA[
			SELECT
  				IDX
				, date_format(REGIST_DATE,'%Y-%m-%d %H:%i') AS REGIST_DATE
				, BRANCH
				, ISSUE_YN
				, TASK_YN
	        , REGIST_DEPARTMENT
	        , REGIST_POSITION
	        , REGIST_NAME
	        , REQUEST_CONTENT
	        , RESOLUTION_CONTENT
	        , RESOLUTION_RESULT
	        , ASSIGNEE
	        , NOTES
			FROM TB_IM_DAILYPROCESSING 
			WHERE IDX = #idx#
			]]>
	</select>
	
					
	
	<select id="dailyProcessingDAO.selectDailyProcessingList" parameterClass="dailyProcessingSearchVO" resultClass="egovMap">
			SELECT
  				IDX
				, date_format(REGIST_DATE,'%Y-%m-%d %H:%i') AS REGIST_DATE
				, BRANCH
				, ISSUE_YN
				, TASK_YN
	        , REGIST_DEPARTMENT
	        , REGIST_POSITION
	        , REGIST_NAME
	        , REQUEST_CONTENT
	        , RESOLUTION_CONTENT
	        , RESOLUTION_RESULT
	        , ASSIGNEE
	        , NOTES
			FROM TB_IM_DAILYPROCESSING 
			WHERE 1=1
			<isEqual prepend="AND" property="searchCnd" compareValue="0">
				REQUEST_CONTENT LIKE CONCAT('%',#searchWrd#,'%')
			</isEqual>
			<isEqual prepend="AND" property="searchCnd" compareValue="1">
				REGIST_NAME LIKE CONCAT('%',#searchWrd#,'%')
			</isEqual>
			<isEqual prepend="AND" property="searchCnd" compareValue="2">
				RESOLUTION_CONTENT LIKE CONCAT('%',#searchWrd#,'%')
			</isEqual>
	
				ORDER BY IDX DESC
				limit #firstIndex#, #recordCountPerPage#				
	</select>	
	
	<select id="dailyProcessingDAO.selectDailyProcessingListTotCnt" parameterClass="dailyProcessingSearchVO" resultClass="int">
			SELECT COUNT(*) totcnt
			FROM TB_IM_DAILYPROCESSING
			WHERE 1=1
			<isEqual prepend="AND" property="searchCnd" compareValue="0">
				REQUEST_CONTENT LIKE CONCAT('%',#searchWrd#,'%')
			</isEqual>
			<isEqual prepend="AND" property="searchCnd" compareValue="1">
				REGIST_NAME LIKE CONCAT('%',#searchWrd#,'%')
			</isEqual>
			<isEqual prepend="AND" property="searchCnd" compareValue="2">
				RESOLUTION_CONTENT LIKE CONCAT('%',#searchWrd#,'%')
			</isEqual>
	</select>
	
	 
	
</sqlMap>

 

 

Namespace

<sqlMap namespace="DailyProcessing">

 

Type Alias

<typeAlias alias="egovMap" type="egovframework.rte.psl.dataaccess.util.EgovMap"/>
<typeAlias alias="dailyProcessingSearchVO" type="egovframework.home.im.dailyProcessing.service.DailyProcessingDefaultVO"/>

 

Result Map

<resultMap id="dailyProcessing" class="egovframework.home.im.dailyProcessing.service.DailyProcessingVO">
    <result property="idx" column="IDX" columnIndex="1"/>
    <result property="registDate" column="REGIST_DATE" columnIndex="2"/>
    <result property="branch" column="BRANCH" columnIndex="3"/>
    <result property="issueYn" column="ISSUE_YN" columnIndex="4"/>
    <result property="taskYn" column="TASK_YN" columnIndex="5"/>
    <result property="registDepartment" column="REGIST_DEPARTMENT" columnIndex="6"/>
    <result property="registPosition" column="REGIST_POSITION" columnIndex="7"/>
    <result property="registName" column="REGIST_NAME" columnIndex="8"/>
    <result property="requestContent" column="REQUEST_CONTENT" columnIndex="9"/>
    <result property="resolutionContent" column="RESOLUTION_CONTENT" columnIndex="10"/>
    <result property="resolutionResult" column="RESOLUTION_RESULT" columnIndex="11"/>
    <result property="assignee" column="ASSIGNEE" columnIndex="12"/>
    <result property="notes" column="NOTES" columnIndex="13"/>
</resultMap>

 

특수문자 보호

  <![CDATA[]]>

 

 

6)      sqlmap연결하기 (경로src\main\resources\egovframework\sqlmap\config\mysql\sql-map-config-mysql-im.xml)

이단계는 새로운 리소스를 만들시 연결해주면 된다.

<sqlMap resource="egovframework/sqlmap/home/im/dailyProcessing/DailyProcessing_SQL.xml" />

 

 

7)      Controller

애너테이션, 적절한 필드, 메서드,매게변수, 적절한 로직을 설정한다.이것은 페이지 변경에  역할을 한다.

 

@RequestMapping("selectDailyProcessing.do")
    public @ModelAttribute("dailyProcessingVO") DailyProcessingVO selectDailyProcessing(DailyProcessingVO dailyProcessingVO, @ModelAttribute("searchVO") DailyProcessingDefaultVO searchVO) throws Exception {
        return dailyProcessingService.selectDailyProcessing(dailyProcessingVO);
    }  
    /** 일일처리현황 수정 */
    @RequestMapping("updateDailyProcessingView.do")
    public String selectDailyProcessing(@RequestParam("idx") String idx,
    		@ModelAttribute("searchVO") DailyProcessingDefaultVO searchVO,Model model) throws Exception {
    	DailyProcessingVO dailyProcessingVO = new DailyProcessingVO();
    	dailyProcessingVO.setidx(idx);
    	LoginVO loginVO = (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser();
        model.addAttribute("groupId", loginVO.getGroupId());
        model.addAttribute("id", loginVO.getId());
        model.addAttribute(selectDailyProcessing(dailyProcessingVO, searchVO));
        return "egovframework/im/dailyProcessing/DailyProcessingRegist";
    }

 

 

8)      JSP

이것은 프론트 영역에 가깝고 기능과 보이는 화면에 대한 개발을 하면된다.