Spring
Aop - Transaction/트랜잭션 예제(입고, 출고, 재고)
우가본
2024. 8. 6. 16:28
-> 성공하면 모두 성공, 실패하면 모두 실패
-> 트랜잭션은 커넥션(DBCP/1-0)에서 걸린다(1-1 -> 1-0으로 보내줌)
-> 4번에서 생긴 예외를 1-1에서 처리해준다(작업이 용이함)
spring_transaction.zip
0.00MB
spring_transaction_ui.zip
0.01MB
-> expression 수정 // 모두 연결되어있음(롤백시, 전부 취소가 되게끔)
-> 모니터링 용도, 있으나 없으나 차이는 없음.
)예제
)sql
--입고
create sequence seq_product_in_idx
create table product_in
(
idx int, --일련번호
name varchar2(255), --상품명
cnt int, --입고수량
regdate date --입고일자
)
alter table product_in
add constraint pk_product_in_idx primary key(idx);
--출고
create sequence seq_product_out_idx
create table product_out
(
idx int, --일련번호
name varchar2(255), --상품명
cnt int, --출고수량
regdate date --출고일자
)
alter table product_out
add constraint pk_product_out_idx primary key(idx);
--재고
create sequence seq_product_remain_idx
create table product_remain
(
idx int, --일련번호
name varchar2(255), --상품명
cnt int, --재고수량
regdate date --재고일자
)
alter table product_remain
add constraint pk_product_remain_idx primary key(idx);
-- 재고 상품명은 유일해야 함.
alter table product_remain
add constraint unique_product_remain_name unique(name);
)Vo(생략)
)Dao
)DaoImpl
package dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import vo.ProductVo;
public class Product_Remain_DaoImpl implements ProductDao {
// 수동 생성이어서 Autowired 사용 안했음
SqlSession sqlSession;
// Setter injection
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
// 재고 CRUD
@Override
public List<ProductVo> selectList() {
// TODO Auto-generated method stub
return sqlSession.selectList("product_remain.product_remain_list");
}
@Override
public int insert(ProductVo vo) {
// TODO Auto-generated method stub
return sqlSession.insert("product_remain.product_remain_insert", vo);
}
@Override
public int update(ProductVo vo) {
// TODO Auto-generated method stub
return sqlSession.update("product_remain.product_remain_update", vo);
}
@Override
public int delete(int idx) {
// TODO Auto-generated method stub
return sqlSession.delete("product_remain.product_remain_delete", idx);
}
// 똑같은 상품명이 있는지 검사(재고에서)
@Override
public ProductVo selectOne(String name) {
// TODO Auto-generated method stub
return sqlSession.selectOne("product_remain.product_remain_one", name);
}
@Override
public ProductVo selectOne(int idx) {
// TODO Auto-generated method stub
return sqlSession.selectOne("product_remain.product_remain_one_idx", idx);
}
}
-> String이 인자인 selectOne은 입고, 출고에서는 필요 없음.
)mapper
<?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="product_remain">
<!-- 전체조회 -->
<select id="product_remain_list" resultType="vo.ProductVo">
select * from product_remain order by idx
</select>
<select id="product_remain_one_idx" parameterType="int" resultType="product">
select * from product_remain where idx = #{idx}
</select>
<select id="product_remain_one" parameterType="String" resultType="product">
select * from product_remain where name = #{name}
</select>
<!-- 추가 -->
<insert id="product_remain_insert" parameterType="product">
insert into product_remain values(
seq_product_remain_idx.nextVal,
#{name},
#{cnt},
sysdate
)
</insert>
<!-- 수량 수정 -->
<update id="product_remain_update" parameterType="product">
update product_remain set cnt=#{cnt},
regdate=sysdate
where idx = #{idx}
</update>
<!-- 삭제 -->
<delete id="product_remain_delete" parameterType="int">
delete from product_remain where idx = #{idx}
</delete>
</mapper>
-> 입고, 출고도 똑같은 코드(String selectOne 하나만 빼주기)
)dao 주입
)service 주입
-> 연결 완료(dao - service)
)Controller와 연결(조회)
-> Controller는 자동생성됨(Servlet-Context를 통해)
*) map에 담은 list의 이름은 UI와 통일시켜줘야 한다.
)입고 처리
-> 목록 조회, 입고 처리 완료
)출고 처리