실습 4일차_MyBatis_Transaction ★
Spring MyBatis_Transaction ★
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.DeptDTO;
import com.service.DeptService;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/dept.xml");
DeptService service = ctx.getBean("deptService", DeptService.class);
// 1. 저장 ==> Spring + Mybatis는 자동으로 commit된다.
// int num = service.insert(new DeptDTO(99, "IT", "부산"));
// int num2 = service.update(new DeptDTO(99, "AI", "서울"));
// int num3 = service.delete(99);
// transaction 처리
try {
service.tx();
} catch (Exception e) {
System.out.println(e.getMessage());
}
List<DeptDTO> list = service.selectAll();
System.out.println(list);
}
}
DeptDAO.java
package com.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dto.DeptDTO;
@Repository("deptDAO")
public class DeptDAO {
@Autowired
SqlSession session; // null
// SqlSessionTemplate session;
public List<DeptDTO> selectAll(){
return session.selectList("com.config.DeptMapper.selectAll");
}
public DeptDTO selectByDeptno(int deptno) {
return session.selectOne("com.config.DeptMapper.selectByDeptno", deptno);
}
public int insert(DeptDTO dto) {
return session.insert("com.config.DeptMapper.insert", dto);
}
public int update(DeptDTO dto) {
return session.update("com.config.DeptMapper.update", dto);
}
public int delete(int deptno) {
return session.delete("com.config.DeptMapper.delete", deptno);
}
}
DeptDTO.java
package com.dto;
import org.apache.ibatis.type.Alias;
@Alias("DeptDTO")
public class DeptDTO {
int deptno;
String dname;
String loc;
public DeptDTO() {
}
public DeptDTO(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = 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;
}
@Override
public String toString() {
return "DeptDTO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
DeptService.java
package com.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.dto.DeptDTO;
@Service("deptService")
public interface DeptService {
// 전체 레코드 조회
public List<DeptDTO> selectAll();
// 특정 레코드 조회
public DeptDTO selectByDeptno(int deptno);
// 저장
public int insert(DeptDTO dto);
// 수정
public int update(DeptDTO dto);
// 삭제
public int delete(int deptno);
// 트랜잭션 실습 메서드
public void tx() throws Exception;
}
DeptServiceImpl.java
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dao.DeptDAO;
import com.dto.DeptDTO;
@Service("deptService")
public class DeptServiceImpl implements DeptService {
@Autowired
DeptDAO dao; //null
@Override
public List<DeptDTO> selectAll() {
return dao.selectAll();
}
@Override
public DeptDTO selectByDeptno(int deptno) {
return dao.selectByDeptno(deptno);
}
@Override
public int insert(DeptDTO dto) {
return dao.insert(dto);
}
@Override
public int update(DeptDTO dto) {
return dao.update(dto);
}
@Override
public int delete(int deptno) {
return dao.delete(deptno);
}
// 트랜잭션 실습 메서드
@Transactional
@Override
public void tx() throws Exception {
int num = dao.insert(new DeptDTO(96, "IT", "서울"));
int num2 = dao.delete(1);
}
}
dept.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- mybatis 연동 -->
<!-- 1. jdbc.properties 등록 -->
<context:property-placeholder location="classpath:com/config/jdbc.properties"/>
<!-- 2. DBCP2 등록 (Connection Pool 기능을 가지고 있음) -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.oracle}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.userid}" />
<property name="password" value="${jdbc.passwd}" />
</bean>
<!-- 트랜잭션 처리 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 트랜잭션 처리 -->
<!-- 3. SqlSessionFactoryBean 등록 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath:com/config/DeptMapper.xml</value>
</list>
</property>
<property name="typeAliases">
<list>
<value>com.dto.DeptDTO</value>
</list>
</property>
</bean>
<!-- 4. SqlSessionTemplate 등록 -->
<bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sessionFactory" />
</bean>
<!-- mybatis 연동 -->
<context:component-scan base-package="com.*" />
</beans>
DeptMapper.xml
<?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.config.DeptMapper">
<select id="selectAll" resultType="DeptDTO">
<![CDATA[
select deptno,dname,loc
from dept
order by deptno desc
]]>
</select>
<select id="selectByDeptno" resultType="DeptDTO"
parameterType="int">
<![CDATA[
select deptno,dname,loc
from dept
where deptno = #{deptno}
]]>
</select>
<insert id="insert" parameterType="DeptDTO">
<![CDATA[
insert into dept ( deptno, dname, loc )
values ( #{deptno} , #{dname} , #{loc})
]]>
</insert>
<update id="update" parameterType="DeptDTO">
<![CDATA[
update dept
set dname = #{dname} , loc=#{loc}
where deptno = #{deptno}
]]>
</update>
<delete id="delete" parameterType="int">
<![CDATA[
delete from dept
where deptno = #{deptno}
]]>
</delete>
</mapper>
jdbc.properties
#주석입니다.
#문법은 key=value
jdbc.oracle=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.userid=????? #DB 계정
jdbc.passwd=????? #DB 계정 패스워드
* 출력 화면
출처
Mybatis :
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 5일차_SpEL_Collection (0) | 2022.03.19 |
---|---|
[Spring] 실습 5일차_SpEL (0) | 2022.03.19 |
[Spring] 실습 4일차_MyBatis ★ (0) | 2022.03.18 |
[Spring] 실습 4일차_Component_Scan (0) | 2022.03.18 |
[Spring] 실습 4일차_@Value_resourceBundle (0) | 2022.03.18 |