본문 바로가기

학원/스프링-학원

상품관리 문제 - standard+MyBatis

728x90

 

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
package com.javalec.spring_test_item_std.controller;
 
import java.util.ArrayList;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
import com.javalec.spring_test_item_std.dto.ItemDto;
import com.javalec.spring_test_item_std.service.ItemService;
 
@Controller
public class ItemController {
    
    @Autowired
    private ItemService service;
    
    @RequestMapping("/content_view")
    public String list(Model model) {
        
        ArrayList<ItemDto> list =  service.list();
        model.addAttribute("list", list);
        
        return "content_view";
    }
    
    @RequestMapping("/writeResult")
    public String write(@RequestParam HashMap<StringString> param, Model model) {
        
        service.itemWrite(param);
        
        return "writeResult";
    }
    
    //글 작성 페이지로 이동
    @RequestMapping("/itemWrite")
    public String itemWrite() {
        return "itemWrite";
    }
}
cs

 

service

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.javalec.spring_test_item_std.service;
 
import java.util.ArrayList;
import java.util.HashMap;
 
import com.javalec.spring_test_item_std.dto.ItemDto;
 
 
public interface ItemService {
 
    public ArrayList<ItemDto> list();
    
    public void itemWrite(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
package com.javalec.spring_test_item_std.service;
 
import java.util.ArrayList;
import java.util.HashMap;
 
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.javalec.spring_test_item_std.dao.ItemIDao;
import com.javalec.spring_test_item_std.dto.ItemDto;
 
@Service
//@Service("ItemService")
public class ItemServiceImpl implements ItemService{
    
    @Autowired
    private SqlSession sqlSession;
 
    @Override
    public ArrayList<ItemDto> list() {
        ItemIDao dao = sqlSession.getMapper(ItemIDao.class);
        ArrayList<ItemDto> dto = dao.list();
        
        return dto;
    }
 
    @Override
    public void itemWrite(HashMap<StringString> param) {
        ItemIDao dao = sqlSession.getMapper(ItemIDao.class);
        dao.itemWrite(param);
    }
}
cs

 

dao.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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_test_item_std.dao.ItemIDao">
    <select id="list" resultType="com.javalec.spring_test_item_std.dto.ItemDto"
            parameterType="hashmap">
        select name, price, description from test_item order by name
    </select>
    
    <insert id="itemWrite" parameterType="hashmap">
        insert into test_item (name, price, description) values (#{name},#{price},#{description})
    </insert>
 
</mapper>
cs

 

dao

 

1
2
3
4
5
6
7
8
9
10
11
12
package com.javalec.spring_test_item_std.dao;
 
import java.util.ArrayList;
import java.util.HashMap;
 
import com.javalec.spring_test_item_std.dto.ItemDto;
 
public interface ItemIDao {
    public ArrayList<ItemDto> list();
    
    public void itemWrite(HashMap<StringString> param);
}
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
package com.javalec.spring_test_item_std.dto;
 
public class ItemDto {
    private String name;
    private int price;
    private String description;
    
    public ItemDto() {
 
    }
 
    public ItemDto(String name, int price, String description) {
        this.name = name;
        this.price = price;
        this.description = description;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}
cs

 

728x90

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

트랜잭션  (0) 2022.05.23
회원가입, 아이디 체크 - standard, mybatis  (0) 2022.05.23
mvc_board -> standard 형태로 -마이바티스 수정  (0) 2022.05.23
MyBatis  (0) 2022.05.20
스프링 JDBC  (0) 2022.05.19