개발로드

☆KDT 2024-04-01★SpringBoot-Maven-회원CRUD 본문

JAVA

☆KDT 2024-04-01★SpringBoot-Maven-회원CRUD

위대한개발자 2024. 4. 1. 17:55

CREATE


<h1>회원 가입</h1>
<form action="/member/register" method="post" name="memberShip">
    <label for="id">아이디</label>
    <input type="text" name="id" id="id"><br>

    <label for="password">비밀번호</label>
    <input type="password" name="password" id="password"><br>
    <label for="name">이름</label>
    <input type="text" name="name" id="name"><br>

    <input type="submit" value="가입">
    <input type="reset" value="다시 작성">
</form>

 

Controller

/*회원가입 메소드*/
@PostMapping("/register")
public String register(Cust cust, String name, String id, String password, Model model) {
    int rs = 0;/*쿼리 실행 결과값을 받을 정수형 변수*/
    cust = new Cust(id, password, name);//객체에 받은 값 저장

    try {
        rs = service.register(cust);
    } catch (Exception e) {
        e.printStackTrace();
    }//end of try-catch

    model.addAttribute("left", path + "left");
    if (rs == 1) {
        model.addAttribute("center", path + "registerSuccess");
    } else {
        model.addAttribute("center", path + "registerFail");
    }

    return "main";
}//end of register

 

CustMapper

<insert id="insert" parameterType="Cust">
    insert into cust values (#{id},#{pwd},#{name})
</insert>

 

CustService

@Override
public int register(Cust cust) throws Exception {
    return mapper.insert(cust);
}

 

 

Read


 

회원목록


 

<h1>회원목록</h1>
<hr>

<table id="memberTable">
    <thead>
    <tr>
        <th>아이디</th>
        <th>비밀번호</th>
        <th>이름</th>
        <th>관리</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="member : ${memberList}">
        <td th:text="${member.id}"></td>
        <td th:text="${member.pwd}"></td>
        <td th:text="${member.name}"></td>
        <td>
            <button th:onclick="|location.href='@{/member/get(id=${member.id})}'|">보기</button>
        </td>
    </tr>
    </tbody>
</table>

 

 

Controller

/*회원목록으로 가는 메소드*/
@GetMapping("/getAll")
public String memberList(Model model) {
    /*회원목록을 받을 list 객체 생성*/
    List<Cust> memberList = new ArrayList<>();
    try {
        memberList = service.getAll();
    } catch (Exception e) {
        e.printStackTrace();
    }//end of try-catch

    model.addAttribute("memberList", memberList);
    model.addAttribute("left", path + "left");
    model.addAttribute("center", path + "memberList");

    for (Cust c : memberList) {
        System.out.println(c);
    }

    return "main";
}//end of memberList

 

@CustService

@Override
public List<Cust> getAll() throws Exception {
    return mapper.selectAll();
}

 

CustMapper

<select id="selectAll" resultType="Cust">
    select * from cust
</select>

 

 

회원정보


<h1>회원정보</h1>
<hr>
<table id="memberTable">
    <thead>
    <tr>
        <th>아이디</th>
        <th>비밀번호</th>
        <th>이름</th>
        <th>관리</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td th:text="${cust.id}"></td>
        <td th:text="${cust.pwd}"></td>
        <td th:text="${cust.name}"></td>
        <td>
            <button type="button" th:onclick="|location.href='@{/member/modifyView(id=${cust.id})}'|">수정</button>
            <button type="button" th:onclick="|location.href='@{/member/remove(id=${cust.id})}'|">삭제</button>
        </td>
    </tr>
    </tbody>
</table>

 

 

 

Controller

@GetMapping("/get")
public String memberOne(String id, Model model, Cust cust) {

    try {
        cust = service.get(id);
    } catch (Exception e) {
        e.printStackTrace();
    }//end of try-catch

    model.addAttribute("cust", cust);
    model.addAttribute("left", path + "left");
    model.addAttribute("center", path + "memberOne");

    return "main";
}//end of memberOne

 

@CustMapper

<select id="select" parameterType="String" resultType="Cust">
    select * from cust where id=#{id}
</select>

 

@CustService

@Override
public Cust get(String s) throws Exception {
    return mapper.select(s);
}

 

회원수정


<h1>회원정보 수정</h1>
<hr>
<form action="/member/modify" method="post" name="memberShip">
    <label for="id">아이디</label>
    <input type="text" name="id" id="id" th:value="${cust.id}" readonly><br>

    <label for="password">비밀번호</label>
    <input type="password" name="password" id="password" th:value="${cust.pwd}"><br>
    <label for="name">이름</label>
    <input type="text" name="name" id="name" th:value="${cust.name}"><br>

    <input id="update" type="submit" value="수정 완료">
    <input type="reset" value="다시 작성">
</form>

 

Controller

/*회워정보 수정하는 페이지로 가는 메소드*/
@GetMapping("/modifyView")
public String modifyView(Model model, String id, Cust cust) {

    try {
        cust = service.get(id);
    } catch (Exception e) {
        e.printStackTrace();
    }//end of try-catch

    model.addAttribute("cust", cust);
    model.addAttribute("left", path + "left");
    model.addAttribute("center", path + "memberUpdateView");
    return "main";
}//end of modifyView

/*회원정보 수정하는 메소드*/
@PostMapping("/modify")
public String modify(String id, String password, String name, Model model, Cust cust) {

    cust = new Cust(id, password, name);
    try {
        service.modify(cust);
        cust = service.get(id);
    } catch (Exception e) {
        e.printStackTrace();
    }//end of try-catch

    model.addAttribute("cust", cust);
    model.addAttribute("left", path + "left");
    model.addAttribute("center", path + "memberOne");

    return "main";
}//end of modify

 

CustMapper

<update id="update" parameterType="Cust">
    update cust set pwd=#{pwd}, name=#{name} where id=#{id}
</update>

 

CustService

@Override
public void modify(Cust cust) throws Exception {
    mapper.update(cust);
}

 

 

회원삭제


Controller

/*회원정보 삭제하는 메소드*/
@GetMapping("/remove")
public String memberDelete(String id, Model model) {
    List<Cust> memberList = new ArrayList<>();
    try {
        service.remove(id);
        memberList = service.getAll();
    } catch (Exception e) {
        e.printStackTrace();
    }//end of try-catch

    model.addAttribute("memberList", memberList);
    model.addAttribute("left", path + "left");
    model.addAttribute("center", path + "memberList");
    return "main";
}//end of memberDelete

 

CustMapper

<delete id="delete" parameterType="String">
    delete from cust where id=#{id}
</delete>

 

CustService

@Override
public void remove(String s) throws Exception {
    mapper.delete(s);
}

 

 


여유가 되신다면 제 GitHub에 오셔서 좋은 코드들을 구경해주세요!

 

https://github.com/gimpo5975?tab=repositories

 

gimpo5975 - Overview

gimpo5975 has 6 repositories available. Follow their code on GitHub.

github.com