반응형
https://github.com/chaSunil/FirstProject/tree/practice
GitHub - chaSunil/FirstProject: 1차 프로젝트
1차 프로젝트. Contribute to chaSunil/FirstProject development by creating an account on GitHub.
github.com
0806 Transaction 파일 가져와서 붙여넣는 과정에 도움을 받아보자.
Spring Boot 프로젝트 생성
JSP 설정 pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
application.properties
spring.application.name=demo_transaction
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
그대로 0806 Transaction 프로젝트에서 가져온다.
application.properties에 추가
#Oracle DataSource
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521/xe
spring.datasource.username=test
spring.datasource.password=test
# DB VO
#Mapper Camel & Snake표기법 지원여부 : member_name -> memberName : true
#Mapper Camel & Snake표기법 지원여부 : member_name -> member_name : false
mybatis.configuration.map-underscore-to-camel-case=false
#Model Location
mybatis.type-aliases-package=com.githrd.demo_transaction.vo
#Xml Location
mybatis.mapper-locations=classpath:mappers/*.xml
Mapper 이름 동일시하게 만든다.
Mapper의 namespace는 Dao(Repository)의 경로를 지정한다.
Mapper 전부 id 수정
@Mapper
public interface ProductInMapper {
List<ProductVo> selectList();
ProductVo selectOne(int idx);
int insert(ProductVo vo);
int update(ProductVo vo);
int delete(int idx);
}
Dao와 동일시하게 맞춰주면 된다.
<?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.githrd.demo_transaction.dao.ProductInMapper">
<!-- 전체조회 -->
<select id="selectList" resultType="product">
select * from product_in order by idx
</select>
<!-- idx -> 객체1개 조회 -->
<select id="selectOne" resultType="product" parameterType="int">
select * from product_in where idx=#{ idx }
</select>
<!-- 추가작업 -->
<insert id="insert" parameterType="product">
insert into product_in values(seq_product_in_idx.nextVal,#{ name },#{ cnt },sysdate)
</insert>
<!-- 수정 -->
<update id="update" parameterType="product">
update product_in
set cnt=#{ cnt }, regdate=sysdate
where idx=#{ idx }
</update>
<!-- 삭제 -->
<delete id="delete" parameterType="int">
delete from product_in
where idx=#{ idx }
</delete>
</mapper>
ProductOutMapper 수정하기
Mapper 구현 파일이 없어도 File명이 동일하게 생성하면, Mapper 구조 완성
ProductServiceImpl 수정하기
package com.githrd.demo_transaction.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.githrd.demo_transaction.dao.ProductInMapper;
import com.githrd.demo_transaction.dao.ProductOutMapper;
import com.githrd.demo_transaction.dao.ProductRemainMapper;
import com.githrd.demo_transaction.vo.ProductVo;
@Service
public class ProductServiceImpl implements ProductService {
// 입고,출고,재고에 관련된 Interface
@Autowired
ProductInMapper product_in_dao; // 입고
@Autowired
ProductOutMapper product_out_dao; // 출고
@Autowired
ProductRemainMapper product_remain_dao; // 재고
@Override
public Map<String, List<ProductVo>> selectTotalMap() {
List<ProductVo> in_list = product_in_dao.selectList(); // 입고목록
List<ProductVo> out_list = product_out_dao.selectList(); // 출고목록
List<ProductVo> remain_list = product_remain_dao.selectList(); // 재고목록
Map<String, List<ProductVo>> map = new HashMap<String, List<ProductVo>>();
map.put("in_list", in_list);
map.put("out_list", out_list);
map.put("remain_list", remain_list);
return map;
}
@Override
public int insert_in(ProductVo vo) throws Exception {
int res = 0;
// 1. 입고등록하기
res = product_in_dao.insert(vo);
// 2. 재고등록(수정처리)
ProductVo remainVo = product_remain_dao.selectOneFromName(vo.getName());
if(remainVo==null) {
// 등록상품이 없으니 등록추가
res = product_remain_dao.insert(vo);
}else {
// 상품기등록상태 : 수량수정
// 재고수량 = 기존재고수량 + 추가수량
int cnt = remainVo.getCnt() + vo.getCnt();
remainVo.setCnt(cnt);
res = product_remain_dao.update(remainVo);
}
return res;
}
@Override
public int insert_out(ProductVo vo) throws Exception {
int res = 0;
// 1. 출고등록하기
res = product_out_dao.insert(vo);
// 2. 재고정보 얻어오기
ProductVo remainVo = product_remain_dao.selectOneFromName(vo.getName());
// 현재 insert의 정보는 Service에서 추가한것이기에 트랜잭션 구역에 머물고 있는 상태이다.
if(remainVo==null) {
// 재고목록에 상품이 없을경우(insert된 Method를 Exception을 줘서 rollback 시킨다)
throw new Exception("remain_not");
}else {
// 재고수량 = 원래재고수량 - 출고수량
int cnt = remainVo.getCnt() - vo.getCnt();
if(cnt < 0) {
// 재고수량이 부족한 경우
throw new Exception("remain_lack");
}
// 재고수량 수정
remainVo.setCnt(cnt);
res = product_remain_dao.update(remainVo);
}
return res;
}
@Override
public int delete_in(int idx) throws Exception {
// 0. 취소할 입고상품 정보 얻어오기
ProductVo vo = product_in_dao.selectOne(idx);
// 1. 입고상품삭제
int res = 0;
res = product_in_dao.delete(idx);
// 2. 재고상품수정
ProductVo remainVo = product_remain_dao.selectOneFromName(vo.getName());
if(remainVo==null) {
throw new Exception("remain_not");
} else {
int cnt = remainVo.getCnt() - vo.getCnt();
if(cnt < 0) {
throw new Exception("remain_lack");
}
// 재고수량 설정
remainVo.setCnt(cnt);
res = product_remain_dao.update(remainVo);
}
return res;
}
@Override
public int delete_out(int idx) throws Exception {
// 0. 취소할 출고상품 정보 얻어오기
ProductVo vo = product_out_dao.selectOne(idx);
// 1. 출고상품삭제
int res = 0;
res = product_out_dao.delete(idx);
// 2. 재고상품수정
ProductVo remainVo = product_remain_dao.selectOneFromName(vo.getName());
if(remainVo==null) {
throw new Exception("remain_not");
} else {
int cnt = remainVo.getCnt() + vo.getCnt();
if(cnt < 0) {
throw new Exception("remain_lack");
}
// 재고수량 설정
remainVo.setCnt(cnt);
res = product_remain_dao.update(remainVo);
}
return res;
}
}
Controller 수정하기
package com.githrd.demo_transaction.controller;
import java.util.Map;
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.servlet.mvc.support.RedirectAttributes;
import com.githrd.demo_transaction.service.ProductService;
import com.githrd.demo_transaction.vo.ProductVo;
@Controller
public class ProductController {
@Autowired
ProductService product_service;
@RequestMapping("/product/list.do")
public String list(Model model) {
Map map = product_service.selectTotalMap();
model.addAttribute("map",map);
return "product/product_list";
}
// 입고처리
// /product/insert_in.do?name=TV&cnt=100
@RequestMapping("/product/insert_in.do")
public String insert_in(ProductVo vo) {
try {
product_service.insert_in(vo);
} catch (Exception e) {
// e.printStackTrace();
}
return "redirect:list.do";
}
// 출고처리
// /product/insert_out.do?name=TV&cnt=100
@RequestMapping("/product/insert_out.do")
public String insert_out(ProductVo vo,RedirectAttributes ra) {
try {
product_service.insert_out(vo);
} catch (Exception e) {
// e.printStackTrace();
String message = e.getMessage();
ra.addAttribute("error",message);
}
return "redirect:list.do";
}
// 입고취소
// /product/delete_in.do?idx=5
@RequestMapping("/product/delete_in.do")
public String delete_in(int idx,RedirectAttributes ra) {
try {
product_service.delete_in(idx);
} catch (Exception e) {
// e.printStackTrace();
String message = e.getMessage();
ra.addAttribute("error",message);
}
return "redirect:list.do";
}
// 출고취소
// /product/delete_out.do?idx=5
@RequestMapping("/product/delete_out.do")
public String delete_out(int idx,RedirectAttributes ra) {
try {
product_service.delete_out(idx);
} catch (Exception e) {
// e.printStackTrace();
String message = e.getMessage();
ra.addAttribute("error",message);
}
return "redirect:list.do";
}
}
반응형
'SpringBoot↗' 카테고리의 다른 글
Spring Boot) AOP (0) | 2024.08.13 |
---|---|
AWS 서버(프로젝트 업로드) (0) | 2024.08.13 |
Spring Boot)JPA로 Sawon 데이터 활용하기 (0) | 2024.08.12 |
Spring Boot) JPA (0) | 2024.08.08 |
SpringBoot 환경설정 및 초기 가이드 (0) | 2024.08.07 |