by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

Transactional 어노테이션 실습




package com.oracleclub.study.mvc.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 
 * dept 테이블 모델객체
 * 
 * @author : oramaster
 * 
 */
public class Dept {
    private int deptno;
    private String dname;
    private String loc;

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="dept">

  	<typeAlias alias="dept" type="com.oracleclub.study.mvc.model.Dept"/>
 	
	<insert id="insertDept"  parameterClass="dept"  >
 	    INSERT INTO dept 
 	    	   (deptno, dname, loc)
        VALUES (#deptno#, #dname#, #loc#)
    </insert>   	
</sqlMap>

package com.oracleclub.study.mvc.dao;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
import com.oracleclub.study.mvc.model.Dept;

/**
 * 부서정보를 관리하는 DAO
 * 
 * @author : oramaster
 * 
 */
@Repository
public class DeptDAO extends SqlMapClientDaoSupport {

    private static final String NAMESPACE = "dept.";

    /**
     * 부사정보 등록
     * 
     * @param dept
     */
    public void insertDept(Dept dept) {
        getSqlMapClientTemplate().insert(NAMESPACE + "insertDept", dept);
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
 

<sqlMapConfig>
	<settings
		cacheModelsEnabled="true"
		enhancementEnabled="true"
		lazyLoadingEnabled="true"
		useStatementNamespaces="true"
	/>
	
	<sqlMap resource="sqlmap/CacheModel.xml" /> 
	<sqlMap resource="sqlmap/Dept.xml" />
	<sqlMap resource="sqlmap/Emp.xml" />  
	
	
</sqlMapConfig>
/*
 * EmpBO.java 2012. 11. 16.
 *
 * Copyright www.gurubee.net All rights Reserved.
 */
package com.oracleclub.study.mvc.bo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oracleclub.study.mvc.dao.DeptDAO;
import com.oracleclub.study.mvc.dao.EmpDAO;
import com.oracleclub.study.mvc.model.Dept;
import com.oracleclub.study.mvc.model.Emp;
import com.oracleclub.study.mvc.model.EmpSearch;

/**
 * 사원관리 비즈니스로직 처리
 * 
 * @author : oramaster
 * 
 */
@Service
public class EmpBO {

    @Autowired
    private EmpDAO empDAO;

    @Autowired
    private DeptDAO deptDAO;

    /**
     * 사원목록 조회
     * 
     * @return
     */
    public List<Emp> getEmpList(EmpSearch search) {
        return empDAO.selectEmpList(search);
    }

    /**
     * 사원정보 조회
     * 
     * @param empno
     * @return
     */
    public Emp getEmp(int empno) {
        return empDAO.selectEmp(empno);
    }

    /**
     * 사원정보 등록
     * 
     * @param emp
     */
    public void registerEmp(Emp emp) {
        empDAO.insertEmp(emp);
    }

    @Transactional(timeout = 5, rollbackFor = Exception.class)
    public void registerTransaction(Emp emp) {
        Dept dept = new Dept();
        dept.setDeptno(10);

        empDAO.insertEmp(emp);
        deptDAO.insertDept(dept);
    }
}

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.