본문 바로가기

학원/스프링-학원

mvc_board -> standard 형태로 -마이바티스 수정

728x90

controller <-> service <-> serviceImple <-> dao(dto) <-> mapper

 

<controller>

 

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//여기서 서비스 쪽 호출
@Controller
public class BController {
//    @Autowired
//    private SqlSession sqlSession;
    
    @Autowired
    private BService service;
    
    @RequestMapping("/list")
    public String list(Model model) {
        System.out.println("@@@ BController.list() start");
        
//        IBDao dao = sqlSession.getMapper(IBDao.class);
//        model.addAttribute("list",  dao.list());
        ArrayList<BDto> list = service.list();
        model.addAttribute("list", list);
        
        System.out.println("@@@ BController.list() end");
        
        return "list";
    }
    @RequestMapping("/write_view")
    public String write_view() {
        return "write_view";
    }
    
    @RequestMapping("/write")
//    public String write(HttpServletRequest request, Model model) {
    public String write(@RequestParam HashMap<StringString> param, Model model) {
        System.out.println("@@@ BController.list() start");
        
//        IBDao dao = sqlSession.getMapper(IBDao.class);
//        dao.write(request.getParameter("bName")
//                , request.getParameter("bTitle")
//                , request.getParameter("bContent"));
        service.write(param);
        
        System.out.println("@@@ BController.list() end");
        
        return "redirect:list";
    }
    
    @RequestMapping("/content_view")
//    public String content_view(HashMap<String, String> param, Model model) {
    public String content_view(@RequestParam HashMap<StringString> param, Model model) {
        System.out.println("@@@ BController.content_view() start");
        
//        IBDao dao = sqlSession.getMapper(IBDao.class);
//        model.addAttribute("content_view", dao.content_view(request.getParameter("bId")));
        BDto dto =  service.content_view(param);
        model.addAttribute("content_view", dto);
        
        System.out.println("@@@ BController.content_view() end");
        
        return "content_view";
    }
    
    @RequestMapping(value = "/modify")
//    public String modify(HttpServletRequest request, Model model) {
    public String modify(@RequestParam HashMap<StringString> param, Model model) {
        System.out.println("@@@ BController.modify() start");
        
//        IBDao dao = sqlSession.getMapper(IBDao.class);
//        dao.modify(request.getParameter("bId")
//                , request.getParameter("bName")
//                , request.getParameter("bTitle")
//                , request.getParameter("bContent"));
        service.modify(param);
        
        System.out.println("@@@ BController.modify() end");
        
        return "redirect:list";
    }
    
    @RequestMapping("/delete")
//    public String delete(HttpServletRequest request, Model model) {
    public String delete(@RequestParam HashMap<StringString> param, Model model) {
        System.out.println("@@@ BController.delete() start");
 
//        IBDao dao = sqlSession.getMapper(IBDao.class);
//        dao.delete(request.getParameter("bId"));
        service.delete(param);
        
        System.out.println("@@@ BController.delete() end");
        
        return "redirect:list";
    }
}
cs

 

<service>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//컨트롤러 - 서비스 - 다오
public interface BService {
 
    public ArrayList<BDto> list();
 
//    public void write(String bName, String bTitle, String bContent);
    public void write(HashMap<StringString> param);
    
//    public BDto content_view(String strID);
    public BDto content_view(HashMap<StringString> param);
    
//    public void modify(String bId, String bName, String bTitle, String bContent);
    public void modify(HashMap<StringString> param);
    
//    public void delete(String strID);
    public void delete(HashMap<StringString> param);
}
cs

 

<serviceImpl>

 

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@Service("BService"//여기서 dao로 감
public class BServiceImpl implements BService{
    
    @Autowired
    private SqlSession sqlSession;
 
    @Override
    public ArrayList<BDto> list() {
        System.out.println("@@@ BServiceImpl.list() start");
        
        IBDao dao = sqlSession.getMapper(IBDao.class);
        //모델이 아니라 list로 받으면 됨
        ArrayList<BDto> list = dao.list();
 
        System.out.println("@@@ BServiceImpl.list() end");
        
        return list;
    }
 
    @Override
//    public void write(String bName, String bTitle, String bContent) {
    public void write(HashMap<StringString> param) {
        System.out.println("@@@ BServiceImpl.write() start");
        
        IBDao dao = sqlSession.getMapper(IBDao.class);
//        dao.write(request.getParameter("bName")
//                , request.getParameter("bTitle")
//                , request.getParameter("bContent"));
        dao.write(param);
        
        System.out.println("@@@ BServiceImpl.write() end");
    }
 
    @Override
//    public BDto content_view(String strID) {
    public BDto content_view(HashMap<StringString> param) {
        System.out.println("@@@ BServiceImpl.content_view() start");
        
        IBDao dao = sqlSession.getMapper(IBDao.class);
//        model.addAttribute("content_view", dao.content_view(request.getParameter("bId")));
        BDto dto = dao.content_view(param);
        
        System.out.println("@@@ BServiceImpl.content_view() end");
        
        return dto;
    }
 
    @Override
    public void modify(HashMap<StringString> param) {
        System.out.println("@@@ BServiceImpl.modify() start");
        
        IBDao dao = sqlSession.getMapper(IBDao.class);
        
//        dao.modify(request.getParameter("bId")
//                , request.getParameter("bName")
//                , request.getParameter("bTitle")
//                , request.getParameter("bContent"));
        dao.modify(param);
        
        System.out.println("@@@ BServiceImpl.modify() end");
    }
 
    @Override
    public void delete(HashMap<StringString> param) {
        System.out.println("@@@ BServiceImpl.delete() start");
        
        IBDao dao = sqlSession.getMapper(IBDao.class);
//        dao.delete(request.getParameter("bId"));
        dao.delete(param);
        
        System.out.println("@@@ BServiceImpl.delete() end");
    }
}
cs

 

<dao>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public interface IBDao {
    public ArrayList<BDto> list();
    
//    public void write(String bName, String bTitle, String bContent);
    public void write(HashMap<StringString> param);
    
//    public BDto content_view(String strID);
    public BDto content_view(HashMap<StringString> param);
    
//    public void modify(String bId, String bName, String bTitle, String bContent);
    public void modify(HashMap<StringString> param);
    
//    public void delete(String strID);
    public void delete(HashMap<StringString> param);
}
cs

 

<dao.xml - mapper>

 

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
<?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.javalec.spring_mvc_board_std.dao.IBDao">
 
    <select id="list" resultType="com.javalec.spring_mvc_board_std.dto.BDto">
        select bId, bName, bTitle, bContent, bDate, bHit from mvc_board order by bId desc
    </select>
 
<!-- 메서드가 해쉬뱁이기 때문에 파라미터 타입으로 hashmap을 선언 -->
<!-- 해쉬맵 사용하면 직접 필드면 써주면 됨(몇 번째 파라미터인지 안적어도 됨) -->
    <insert id="write" parameterType="hashmap">
        insert into mvc_board (bId, bName, bTitle, bContent, bHit) 
            values(mvc_board_seq.nextval, #{bName},#{bTitle},#{bContent},0)
    </insert>
    
    <select id="content_view" resultType="com.javalec.spring_mvc_board_std.dto.BDto"
            parameterType="hashmap">
        select bId, bName, bTitle, bContent, bDate, bHit from mvc_board where bId=#{bId}
    </select>
    
    <update id="modify" parameterType="hashmap">
        update mvc_board set bName=#{bName}, bTitle=#{bTitle}, bContent=#{bContent} 
            where bId= #{bId}
    </update>
    
    <delete id="delete" parameterType="hashmap">
        delete mvc_board where bId=#{bId}
    </delete>
</mapper>
cs

 

<dto>

 

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
46
47
48
49
50
51
52
53
54
55
56
57
58
public class BDto {
    private int bId;
    private String bName;
    private String bTitle;
    private String bContent;
    private Timestamp bDate;
    private int bHit;
    
    public BDto() {
        
    }
 
    public BDto(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit) {
        this.bId = bId;
        this.bName = bName;
        this.bTitle = bTitle;
        this.bContent = bContent;
        this.bDate = bDate;
        this.bHit = bHit;
    }
    
    public int getbId() {
        return bId;
    }
    public void setbId(int bId) {
        this.bId = bId;
    }
    public String getbName() {
        return bName;
    }
    public void setbName(String bName) {
        this.bName = bName;
    }
    public String getbTitle() {
        return bTitle;
    }
    public void setbTitle(String bTitle) {
        this.bTitle = bTitle;
    }
    public String getbContent() {
        return bContent;
    }
    public void setbContent(String bContent) {
        this.bContent = bContent;
    }
    public Timestamp getbDate() {
        return bDate;
    }
    public void setbDate(Timestamp bDate) {
        this.bDate = bDate;
    }
    public int getbHit() {
        return bHit;
    }
    public void setbHit(int bHit) {
        this.bHit = bHit;
    }
}
cs

 

 

728x90

'학원 > 스프링-학원' 카테고리의 다른 글

회원가입, 아이디 체크 - standard, mybatis  (0) 2022.05.23
상품관리 문제 - standard+MyBatis  (0) 2022.05.23
MyBatis  (0) 2022.05.20
스프링 JDBC  (0) 2022.05.19
MVC 로그인 문제 (JDBC 템플릿 사용 부분도 추가)  (0) 2022.05.19