MapleStory Finger Point

🟤 JAVA/🟤 Spring

[SpringBoot / 실습] 메모 기능 (등록,조회,유효성,비밀글)

HYEJU01 2024. 8. 26. 22:04
memoWrite 메모 등록
memoList 메모 조회
@Valid  유효성검사
secret 에 ('y' / 'n') 으로 저장 공개글/비밀글

 

 

 

 

 


◈ MySQL 테이블 생성

CREATE TABLE MEMO_TEST(
MNO INT PRIMARY KEY AUTO_INCREMENT,
MEMO VARCHAR(200) NOT NULL,
PHONE VARCHAR(30),
PW VARCHAR(4),
SECRET CHAR(1) CHECK(SECRET IN ('y', 'n'))
);

 

 

◈ Controller

package com.simple.basic.controller;

import com.simple.basic.command.MemoVO;
import com.simple.basic.memo.MemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.ArrayList;

@Controller
@RequestMapping("/memo")
public class MemoController {
    
    //insert기능 - 유효성검사
    //select기능 - 화면처리

    @Autowired
    @Qualifier("memoService")
    MemoService memoService;


    @GetMapping("/memoWrite")
    public String memoWrite(Model model) {
        model.addAttribute("vo", new MemoVO());
        return "memo/memoWrite";
    }
    
    //step.1 - 폼요청처리
    @PostMapping("/memoForm")
    public String memoForm(@Valid @ModelAttribute("vo") MemoVO vo, BindingResult binding) {

        if(binding.hasErrors()) {
            return "memo/memoWrite"; //유효성 검사 실패인 경우
        }

        //insert처리................
        memoService.insert(vo);

        return "redirect:/memo/memoList"; //목록화면으로
    }

    //step-2 목록화면
    @GetMapping("/memoList")
    public String memoList(Model model) {

        //select
        ArrayList<MemoVO> list = memoService.getList();
        model.addAttribute("list", list);

        return "memo/memoList";
    }


}

 

 

◈ MemoVo.java 

+ 유효성 검사

package com.simple.basic.command;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class MemoVO {

    private Long mno;
    @Size(min = 5, message = "최소 5글자 이상 입니다")
    private String memo;
    @Pattern(regexp = "[0-9]{3}-[0-9]{4}-[0-9]{4}", message = "000-0000-0000형식 입니다")
    private String phone;
    @Pattern(regexp = "[0-9]{4}", message = "숫자 4자리 입니다")
    private String pw;
    private String secret = "n";

}

 

 

◈ Mapper .java

package com.simple.basic.memo;


import com.simple.basic.command.MemoVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;

@Mapper //이게 붙은 인터페이스를 마이바티스가 인식함
public interface MemoMapper {
    public String hello();
    public void insert(MemoVO vo);
    public ArrayList<MemoVO> getList();
}

 

 

◈ Mapper .xml

<?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="com.simple.basic.memo.MemoMapper">

    <select id="hello" resultType="string">
        select now()
    </select>

    <insert id="insert" parameterType="MemoVO">
        insert into memo(memo, phone, pw, secret)
        values( #{memo}, #{phone}, #{pw}, #{secret} )
    </insert>

    <select id="getList" resultType="MemoVO">
        select * from memo
    </select>


</mapper>

 

 

◈ Service.java

package com.simple.basic.memo;

import com.simple.basic.command.MemoVO;

import java.util.ArrayList;

public interface MemoService {
    public void insert(MemoVO vo);
    public ArrayList<MemoVO> getList();
}

 

 

◈ServiceImpl.java

package com.simple.basic.memo;

import com.simple.basic.command.MemoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service("memoService")
public class MemoServiceImpl implements MemoService {

    @Autowired
    MemoMapper memoMapper; //매퍼 구현체를 넣어줌...

    @Override
    public void insert(MemoVO vo) {
        memoMapper.insert(vo);
    }

    @Override
    public ArrayList<MemoVO> getList() {
        return memoMapper.getList();
    }
}

 

 

유효성 검사  (validVo.java)

hasErrors로 유효성 검사 에러가 나면 화면을 제어

    @PostMapping("/memoForm")
    public String memoForm(@Valid @ModelAttribute("vo") MemoVO vo, BindingResult binding) {

        if(binding.hasErrors()) {
            return "memo/memoWrite"; //유효성 검사 실패인 경우
        }

        //insert처리................
        memoService.insert(vo);

        return "redirect:/memo/memoList"; //목록화면으로
    }