** Oracle MyBatis 연동
1. 데이터베이스 준비
GOODS 테이블 만들어둠
=> code : number
=> name : varchar2
=> regdata : date
2. Simple Spring Maven 프로젝트 생성
=> 안만들어지면 Java Project를 생성해서 Maven Project로 변환해서 사용
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringMyBatis</groupId>
<artifactId>SpringMyBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- 이 코드를 pom.xml 파일에 붙여 넣습니다. -->
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.0.7.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. Oracle을 MyBatis를 이용해서 사용할 때 필요한 의존성을 설정 - pom.xml
=> Oracle, SpringJDBC, MyBatis, MyBatis-Spring
1) 오라클 사용을 위해서 repository 설정
<!-- 중앙 저장소가 아닌 곳에서 라이브러리를 다운로드 받고자 하는 경우 설정 : Oracle 때문에 설정 -->
<repositories>
<repository>
<id>oracle</id>
<name>ORACLE JDBC Repository</name>
<url>http://maven.jahia.org/maven2</url>
</repository>
</repositories>
2) depedencies 설정
<!-- 오라클 의존성 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<!-- 스프링에서 데이터베이스를 사용할 때 필요한 의존성 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- MyBatis 의존성 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- Spring에서 MyBatis를 사용하기 위한 의존성 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringMyBatis</groupId>
<artifactId>SpringMyBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- 이 코드를 pom.xml 파일에 붙여 넣습니다. -->
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.0.7.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.12</junit.version>
</properties>
<!-- 중앙 저장소가 아닌 곳에서 라이브러리를 다운로드 받고자 하는 경우 설정 : Oracle 때문에 설정 -->
<repositories>
<repository>
<id>oracle</id>
<name>ORACLE JDBC Repository</name>
<url>http://maven.jahia.org/maven2</url>
</repository>
</repositories>
<dependencies>
<!-- 오라클 의존성 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<!-- 스프링에서 데이터베이스를 사용할 때 필요한 의존성 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- MyBatis 의존성 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- Spring에서 MyBatis를 사용하기 위한 의존성 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4. 데이터베이스 접속 테스트
=> spring에서는 데이터베이스를 사용할 때 설정 파일에 DataSource를 만들어서 사용하는 것을 강제한다.
src 또는 src/main/resources 디렉토리에 spring bean configuration파일을 추가하고 작성 - applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DataSource 빈 생성 코드 -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<!-- 데이터베이스 종류 : Oracle -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<!-- 연결할 데이터베이스 URL -->
<property name="url" value="jdbc:oracle:thin:@192.168.0.200:1521:xe"/>
<!-- 사용할 데이터베이스 계정 -->
<property name="username" value="user05"/>
<!-- 사용할 계정의 비밀번호 -->
<property name="password" value="user05" />
</bean>
</beans>
5. 위의 설정이 제대로 되었는지 테스트 클래스를 만들어서 테스트
=>src/test/java 또는 src 디렉토리에 테스트용 클래스를 생성 - MyBatisTest - 포트폴리오에 넣기
Main
import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
try {
GenericXmlApplicationContext context =
new GenericXmlApplicationContext(
"applicationContext.xml");
DataSource dataSource =
context.getBean(DataSource.class);
//내용이 정상적으로 출력되면 데이터베이스 접속 성공
Connection con = dataSource.getConnection();
System.out.println(con);
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
6. 테이블과 매핑이 될 DTO 클래스를 생성
=> 자료형과 프로퍼티 이름을 정확하게 매핑
package mybatis.domain;
import java.util.Date;
public class Good {
private int code;
private String name;
private Date regdate;
public Good() {
super();
// TODO Auto-generated constructor stub
}
public Good(int code, String name, Date regdate) {
super();
this.code = code;
this.name = name;
this.regdate = regdate;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
@Override
public String toString() {
return "Good [code=" + code + ", name=" + name + ", regdate=" + regdate + "]";
}
}
7. MyBatis나 Hibernate 등의 데이터베이스 연동 프레임워크를 사용하면 여기까지는 공통의 작업이다.
**MyBatis를 XML을 이용해서 사용
=> 환경 설정 파일을 1개 생성 - 생략가능
패키지이름이 길 때 패키지 이름을 생략하고 DTO를 사용하기 위해서 생성
resultMap이라고 해서 DTO에 만든 프로퍼티 이름과 테이블의 컬럼이름이 다를 때 매핑시켜주는 설정을 작성
=> SQL을 작성할 매퍼 파일을 n개 생성 - 필수
=> spring 설정 파일에 위의 정보를 이용해서 SqlSessionFactoryBean과 SqlSessionTemplate 클래스의 bean을 생성하는 코드를 작성
=> DAO 클래스에서 SqlSession을 주입받아서 사용
1. 매퍼 파일 작성 요령
=> namespace를 생성 - 이 파일의 이름을 정하는거다.
1) select
<select id="구별할이름"
parameterType="파라미터 전체를 하나로 묶어서 전달할 자료형"
resultType="selece 절의 컬럼들을 하나로 묶어서 전달할 자료형">
select 구문
</select>
=> 외부로부터 대입받는 데이터는 ? 대신에 #{이름} 의 형태로 작성한다.
code, name, regdate 로 구성된 Goods 테이블의 모든 데이터를 가져오는 SQL
<select id="allgood" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
</select>
code를 입력받아서 code에 해당하는 데이터를 가져오는 SQL
<select id ="getgood" parameterType="java.lang.Integer"
resultType"mybatis.domain.Good">
select code, name, regdate
from goods
where code = #{code}
</select>
user 테이블에서 email에 해당하는 데이터를 찾아오는 SQL
User 클래스가 없는 경우는 Map을 사용
<select id="getuser" parameterType"java.lang.String"
resultType="java.util.Map">
select email, password, nickname, image
from user
where email = #{email}
</select>
user 테이블에서 email과 password에 해당하는 데이터를 찾아오는 SQL
User 클래스가 없는 경우는 Map을 사용
<select id="getuser" parameterType"java.util.Map"
resultType="java.util.Map">
select email, password, nickname, image
from user
where email = #{email} and password =#{password}
</select>
=> 부등호를 직접 사용할 수 없으니 %lt; (<), %gt; (>)
=> like
Oracle : '% 나 _' || #{keyword} || '% 나 _'
MySQL : concat) '% 나 _', #{keyword}, '% 나 _')
2) select 이외 구문
insert, update, delete 태그로 만드는데 사실 아무 태그나 써도 결과는 같다.
<insert id="insertgood" parameterType="mybatis.domain.Good">
insert into goods)code, name, regdate)
values(#{code}, #{name}, #{regdate})
</insert>
2. src에 mappers 디렉토리를 생성하고 good.xml 파일을 만들어서 sql을 작성
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD : XML로 작성한 내용을 해석하는 위치를 설정
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="good">
<!--파라미터 없이 전체 데이터를 읽어오는 SQL -->
<select id="allgood" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
</select>
<!--code를 가지고 하나의 데이터를 읽어오는 SQL : 상세보기 -->
<select id="getgood" parameterType="java.lang.Integer" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
where code = #{code}
</select>
<!--데이터를 삽입하는 SQL -->
<select id="insertgood" parameterType="mybatis.domain.Good" >
insert into goods(code, name, regdate)
values(#{code}, #{name}, #{regdate})
</select>
<!--데이터를 수정하는 SQL 기본키(code)를 가지고 데이터를 자겨와서 나머지 컬럼의 값을 수정-->
<update id="updategood" parameterType="mybatis.domain.Good" >
update goods
set name=#{name}, regdate=#{regdate}
where code= #{code}
</update>
<!--데이터를 삭제하는 SQL 기본키를 가지고 데이터를 삭제하는 것이 일반적-->
<delete id="deletegood" parameterType="java.lang.Integer" >
delete from goods
where code = #{code}
</delete>
</mapper>
=> 없는 자료형을 사용하면 애플리케이션이 시작될 때 에러가 발생
=> id 틀리게 호출하거나 paremeter가 잘못되면 메소드를 호출할 때 에러가 발생
3. Spring Bean Configuration 파일에 xml을 이용하는 MyBatisBean을 생성하는 코드를 작성
=> SqlSessionFactoryBean, SqlSessionTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DataSource 빈 생성 코드 -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<!-- 데이터베이스 종류 : Oracle -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<!-- 연결할 데이터베이스 URL -->
<property name="url" value="jdbc:oracle:thin:@192.168.0.200:1521:xe"/>
<!-- 사용할 데이터베이스 계정 -->
<property name="username" value="user05"/>
<!-- 사용할 계정의 비밀번호 -->
<property name="password" value="user05" />
</bean>
<bean id="sqlSesseionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 연결할 데이터베이스 설정 -->
<property name="dataSource" ref="dataSource"/>
<!-- 매패 파일의 위치 설정 -->
<property name="mapperLocations" value="mappers/good.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref = "sqlSessionFactory"/>
</bean>
</beans>
4. 설정확인
=> SqlSessionTemplate을 주입받아 보면 된다.
=> 이부분에서 에러가 발생하는 것은 대부분 매퍼 파일의 경로를 틀려서이다.
5. XML을 이용한 MyBatis 연동 DAO 작성 방법
=> SqlSession을 주입받아서 사용
=> SQL 호출
=> 파라미터가 없는 경우는 파라미터를 생략 가능
1) selectList("아이디", 파라미터) : resultType의 List를 리턴
2) selectOne("아이디", 파라미터) : resultType으로 리턴
=> select의 결과가 0개이면 null이 리턴되고 2개 이상이면 예외가 발생
=> primary key나 unique 제약조건이 설정된 컬럼을 가지고 조회를 하거나 count처럼 집계함수를 이용해서 데이터를 1개만 조회하는 경우에 사용해야한다.
3) insert("아이디", 파라미터)
4) update("아이디", 파라미터)
5) delete("아이디", 파라미터)
=> 3개의 메소드는 정수를 리턴한다.
정수는 영향받은 행의 개수이다.
insert의 경우는 0이 리턴되면 삽입 실패이지만 update와 delete는 0이 리턴되면 조건에 맞는 데이터가 없어서 작업을 수행하지 않는 것이다.
6) 많이 발생시키는 예외
=> 아이디를 잘못 입력하는 경우
=> 파라미터를 대입해야하는데 파라미터를 생략하는 경우
=> sql을 잘못 입력해서 SqlException이 발생하기도 한다.
6. 앞에서 작성한 xml을 호출할 DAO를 생성
=> 여기까지는 프로젝트 종류가 달라지더라도 코드수정을 할 필요가 없다.
=> Service나 Controller는 URL통신이나 Socket 통신이냐에 따라서 달라질 수 있다.
package mybatis.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import mybatis.domain.Good;
//Controller, Service, Repository, Component
//bean을 자동 생성해주는 어노테이션 - IoC(제어의 역전)
@Repository
public class GoodDAO {
//데이터베이스 연동 프레임워크의 변수
@Autowired
private SqlSession sqlSession;
//전체 데이터를 가져오는 메소드
//파라미터 없이 전체 데이터를 가져오는 SQL을 호출
public List<Good> allGood(){
return sqlSession.selectList("good.allgood");
}
//code를 입력받아서 데이터1개를 찾아오는 메소드
public Good getGood(int code) {
return sqlSession.selectOne("good.getgood", code);
}
//데이터를 삽입하는 메소드
public int insertGood(Good good) {
return sqlSession.insert("good.insertgood", good);
}
//데이터를 수정하는 메소드
public int deleteGood(Good good) {
return sqlSession.update("good.updategood", good);
}
//데이터를 삭제하는 메소드
public int deleteGood(int code) {
return sqlSession.delete("good.deletegood", code);
}
}
7. GoodDAO 클래스의 bean을 자동으로 생성해주는 설정을 SpringBeanConfiguration 파일에 추가
=> Spring MVC Project면 할 필요가 없는 작업
1) applicationContext.xml 파일에 context 네임 스페이스를 추가
![]() |
2) applicationContext.xml 파일에 어노테이션을 사용할 수 있도록 하고 baen을 자동 생성할 패키지를 등록
<?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"
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">
<!-- DataSource 빈 생성 코드 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<!-- 데이터베이스 종류 : Oracle -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<!-- 연결할 데이터베이스 URL -->
<property name="url" value="jdbc:oracle:thin:@192.168.0.200:1521:xe"/>
<!-- 사용할 데이터베이스 계정 -->
<property name="username" value="user05"/>
<!-- 사용할 계정의 비밀번호 -->
<property name="password" value="user05" />
</bean>
<!-- MyBatis 사용을 위한 bean 설정 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 연결할 데이터베이스 설정 -->
<property name="dataSource" ref="dataSource"/>
<!-- 매퍼 파일의 위치 설정 -->
<property name="mapperLocations" value="mappers/good.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>
<!-- 어노테이션을 사용할 수 있도록 해주는 설정 -->
<context:annotation-config />
<!-- bean을 자동 생성할 패키지 등록 -->
<context:component-scan base-package="mybatis" />
</beans>
8. main 메소드에서 DAO를 가져와서 사용
import org.springframework.context.support.GenericXmlApplicationContext;
import mybatis.dao.GoodDAO;
public class Main {
public static void main(String[] args) {
try {
GenericXmlApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
// DataSource dataSource = context.getBean(DataSource.class);
// //내용이 정상적으로 출력되면 데이터베이스 접속 성공
// Connection con = dataSource.getConnection();
// System.out.println(con);
// SQLSession 주입받아보기
// 출력이 되면 MyBatis 설정에는 문제가 없음
// SqlSession sqlSession = context.getBean(SqlSession.class);
// System.out.println(sqlSession);
GoodDAO goodDAO = context.getBean(GoodDAO.class);
//전체 데이터 가져오기
System.out.println(goodDAO.allGood());
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
=> 데이터 삽입 작업을 만들 때는 primary key나 unique 제약조건이 설정된 컬럼이 있다면 이 컬럼의 값을 어떤 식으로 만들것인지 생각해봐야 한다.
1. Sequence 나 Auto_Increment를 이용해서 자동으로 값을 설정
=> Sequence 나 Auro_Increment는 증가만 하고 감소나 삭제는 안되고 초기화는 가능
=> 입력하고 바로 삭제하는 경우 번호를 재사용할 수 없다.
=> 사용하는 관계형 데이터베이스마다 생성하는 방법이 다르다.
2. Primary key나 unique한 데이터가 정수라면 가장 큰 정수 값을 찾아서 +1을 해주는 방법
=> 별도의 SQL을 한 번 수행해야 한다.
3. 직접 입력받는 방법
=> 중복 검사를 수행해야 한다.
4. 1번이나 2번으로 생성한 데이터는 순번 이외의 아무런 기능도 없다.
=> 데이터 수정은 데이터 삽입과 모양이 같다.
기본키를 가지고 데이터를 찾아와서 데이터를 화면에 출력한 후 수정할 수 있도록 해주고 수정 작업을 처리한다.
데이터 수정도 삽입처럼 정수가 리턴되는데 0이 아닌 양수면 조건에 맞는 데이터를 수정한 것이고 0이면 조건에 맞는 데이터가 없어서 수정하지 않았다는 의미
=> 데이터 삭제
삭제는 구현을 할 때 비밀번호를 입력받거나 정말로 삭제할 것인지 되묻는 작업을 수행하고 삭제를 한다.
실업무에서는 삭제보다는 대부분 특정 컬럼의 값을 변경해서 삭제된 것처럼 하는 경우가 많다.
메소드를 구현할 때는 기본키를 매개변수로 받아서 삭제
import java.util.Date;
import org.springframework.context.support.GenericXmlApplicationContext;
import mybatis.dao.GoodDAO;
import mybatis.domain.Good;
public class Main {
public static void main(String[] args) {
try {
GenericXmlApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
// DataSource dataSource = context.getBean(DataSource.class);
// //내용이 정상적으로 출력되면 데이터베이스 접속 성공
// Connection con = dataSource.getConnection();
// System.out.println(con);
// SQLSession 주입받아보기
// 출력이 되면 MyBatis 설정에는 문제가 없음
// SqlSession sqlSession = context.getBean(SqlSession.class);
// System.out.println(sqlSession);
GoodDAO goodDAO = context.getBean(GoodDAO.class);
//전체 데이터 가져오기
//System.out.println(goodDAO.getGood(2));
//삽입할 데이터를 생성
/*
Good good = new Good();
good.setCode(8);
good.setName("수박");
good.setRegdate(new Date());
System.out.println(goodDAO.insertGood(good));
*/
/*데이터 수정 확인
Good good = new Good();
good.setCode(20);
good.setName("무화과");
good.setRegdate(new Date());
System.out.println(goodDAO.updateGood(good));
*/
System.out.println(goodDAO.deleteGood(8));
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
** 새로운 데이터베이스 작업이 필요한 경우
1. mapper 파일에 필요한 SQL을 작성 (good.xml)
<!-- name에 포함된 데이터를 검색하는 SQL -->
<select id="searchname" parameterType="java.lang.String" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
where name like '%'|| #{name} || '%'
</select>
MySQL에서의 where 절
where name like concat('%', #{name}, '%')
2. DAO에 적절한 메소드를 만들어서 호출 (GoodDAO.java)
//name의 일부분을 가지고 name이 포함된 데이터를 찾아오는 메소드
public List<Good> searchName(String name){
return sqlSession.selectList("good.searchname", name);
}
package mybatis.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import mybatis.domain.Good;
//Controller, Service, Repository, Component
//bean을 자동 생성해주는 어노테이션 - IoC(제어의 역전)
@Repository
public class GoodDAO {
//데이터베이스 연동 프레임워크의 변수
@Autowired
private SqlSession sqlSession;
//전체 데이터를 가져오는 메소드
//파라미터 없이 전체 데이터를 가져오는 SQL을 호출
public List<Good> allGood(){
return sqlSession.selectList("good.allgood");
}
//code를 입력받아서 데이터1개를 찾아오는 메소드
public Good getGood(int code) {
return sqlSession.selectOne("good.getgood", code);
}
//데이터를 삽입하는 메소드
public int insertGood(Good good) {
return sqlSession.insert("good.insertgood", good);
}
//데이터를 수정하는 메소드
public int updateGood(Good good) {
return sqlSession.update("good.updategood", good);
}
//데이터를 삭제하는 메소드
public int deleteGood(int code) {
return sqlSession.delete("good.deletegood", code);
}
//name의 일부분을 가지고 name이 포함된 데이터를 찾아오는 메소드
public List<Good> searchName(String name){
return sqlSession.selectList("good.searchname", name);
}
}
** Procedure 실행
1. procedure
=> 자주 사용하는 SQL 문을 하나의 이름으로 만들어 두고 이름을 호출하는 형태로 실행
=> 프로그래밍 언어의 메소드처럼 사용
=> 프로시저의 장점은 실행속도가 빠름 (한번 호출되면 메모리에 적재된 상테에서 다음 호출에 이용되기 때문)
보안 향상 (SQL을 보여주지 않기 때문에 테이블이 어떻게 구성되었는지 알 수 없음)
=> 프로시져는 데이터베이스 종류마다 다른 문법으로 작성
=> Oracle에서 프로시저를 만드는 문법을 PL/SQL 이라고 하고 MS-SQL Server에서 만드는 문법은 T-SQL이라고 한다.
2. MyBatis에서 프로시저 호출
{
call procedure 이름(파라미터)
}
=> 태그에는 statementType 속성에 CALLABLE을 설정해서 CallableStatement가 호출하도록 해주어야 한다.
=> SqlSession이 호출할 때는 이전처럼 selectList, selectIne, insert, delete, update 메소드로 호출
1) 프로시저 생성
Oracle에서
--goods 테이블에 데이터를 삽입하는 프로시저
CREATE OR REPLACE PROCEDURE insertgood(
vcode goods.code%TYPE,
vname goods.name%TYPE,
vregdate goods.regdate%TYPE
)
IS
BEGIN
INSERT INTO GOODS (code, name, REGDATE)
values(vcode, vname, vregdate);
END;
MySQL에서
--MySQL에서의 프로시저
CREATE PROCEDURE insertgood(
IN vcode int.
IN vname varchar(20),
IN vregdate date
)
BEGIN
INSERT INTO goods(code, name, regdate)
values(vcode, vname, vregdate);
end
2) 매퍼파일에서 데이터 삽입하는 SQL을 수정
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD : XML로 작성한 내용을 해석하는 위치를 설정 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="good">
<!--파라미터 없이 전체 데이터를 읽어오는 SQL -->
<select id="allgood" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
</select>
<!--code를 가지고 하나의 데이터를 읽어오는 SQL : 상세보기 -->
<select id="getgood" parameterType="java.lang.Integer" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
where code = #{code}
</select>
<!--
<!--데이터를 삽입하는 SQL -->
<insert id="insertgood" parameterType="mybatis.domain.Good" >
insert into goods(code, name, regdate)
values(#{code}, #{name}, #{regdate})
</insert>
-->
<!--프로시저를 이용한 insert SQL 작업 -->
<insert id = "insertgood" parameterType="mybatis.domain.Good" statementType="CALLABLE">
{call insertgood(#{code}, #{name}, #{regdate})}
</insert>
<!--데이터를 수정하는 SQL 기본키(code)를 가지고 데이터를 자겨와서 나머지 컬럼의 값을 수정-->
<update id="updategood" parameterType="mybatis.domain.Good" >
update goods
set name=#{name}, regdate=#{regdate}
where code= #{code}
</update>
<!--데이터를 삭제하는 SQL 기본키를 가지고 데이터를 삭제하는 것이 일반적-->
<delete id="deletegood" parameterType="java.lang.Integer" >
delete from goods
where code = #{code}
</delete>
<!-- name에 포함된 데이터를 검색하는 SQL -->
<select id="searchname" parameterType="java.lang.String" resultType="mybatis.domain.Good">
select code, name, regdate
from goods
where name like '%'|| #{name} || '%'
</select>
</mapper>
3) main 메소드에서 테스트
import java.util.Date;
import org.springframework.context.support.GenericXmlApplicationContext;
import mybatis.dao.GoodDAO;
import mybatis.domain.Good;
public class Main {
public static void main(String[] args) {
try {
GenericXmlApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
// DataSource dataSource = context.getBean(DataSource.class);
// //내용이 정상적으로 출력되면 데이터베이스 접속 성공
// Connection con = dataSource.getConnection();
// System.out.println(con);
// SQLSession 주입받아보기
// 출력이 되면 MyBatis 설정에는 문제가 없음
// SqlSession sqlSession = context.getBean(SqlSession.class);
// System.out.println(sqlSession);
GoodDAO goodDAO = context.getBean(GoodDAO.class);
//전체 데이터 가져오기
//System.out.println(goodDAO.getGood(2));
//삽입할 데이터를 생성
/*
Good good = new Good();
good.setCode(8);
good.setName("수박");
good.setRegdate(new Date());
System.out.println(goodDAO.insertGood(good));
*/
/*데이터 수정 확인
Good good = new Good();
good.setCode(20);
good.setName("무화과");
good.setRegdate(new Date());
System.out.println(goodDAO.updateGood(good));
*/
//System.out.println(goodDAO.deleteGood(8));
//System.out.println(goodDAO.searchName("감"));
Good good = new Good();
good.setCode(8);
good.setName("수박");
good.setRegdate(new Date());
System.out.println(
goodDAO.insertGood(good));
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
** 인터페이스 이용
=> xml 파일 대신에 인터페이스를 이용해서 MyBatis 연동 가능
=> 이 방식을 이용하면 별도의 DAO 클래스가 필요 없다.
1. 인터페이스에서 SQL 생성
@Select("sql 구문")
메소드 선언
=> 삽입하는 SQL이면 Select 대신에 Insert, 갱신은 Update, 삭제는 Delete를 사용
2. MyBatis 사용을 위한 Bean 생성코드
=> SqlSessionFactoryBean에는 dateSource만 설정
<bean id="아이디" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="매퍼인터페이스경로"/>
<property name="sqlSessionFactory" ref="앞에서 만든 bean의 id"/>
</bean>
3. 인터페이스 주입 받아서 메소드 호출
** XML로 작업했던 내용을 인터페이스로 수정
1. pom.xml 파일에 데이터베이스, mybatis 관련 의존성을 설정
=> Oracle, Spring-JDBC, MyBatis, MyBatis-Spring 의존성을 설정
2. Spring Bean Configuration 파일에 DataSource 빈을 생성하는 코드를 작성하고 데이터베이스 연결이 되는지 확인
1) applicationContext.xml 파일에 데이터베이스 접속 정보 작성
<!-- DataSource 빈 생성 코드 -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<!-- 데이터베이스 종류 : Oracle -->
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver"/>
<!-- 연결할 데이터베이스 URL -->
<property name="url" value="jdbc:oracle:thin:@192.168.0.200:1521:xe"/>
<!-- 사용할 데이터베이스 계정 -->
<property name="username" value="system"/>
<!-- 사용할 계정의 비밀번호 -->
<property name="password" value="********" />
</bean>
2) 테스트 할 메소드에서 위의 bean을 가져와서 테스트하는 코드를 작성하고 실행
import java.sql.Connection;
import java.util.Date;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import mybatis.dao.GoodDAO;
import mybatis.domain.Good;
public class Main {
public static void main(String[] args) {
// try {
// GenericXmlApplicationContext context =
// new GenericXmlApplicationContext(
// "applicationContext.xml");
//// DataSource dataSource =
//// context.getBean(DataSource.class);
//// //내용이 정상적으로 출력되면 데이터베이스 접속 성공
//// Connection con = dataSource.getConnection();
//// System.out.println(con);
//
// //SQLSession 주입받아보기
// //출력이 되면 MyBatis 설정에는 문제가 없음
//// SqlSession sqlSession =
//// context.getBean(SqlSession.class);
//// System.out.println(sqlSession);
//
// GoodDAO goodDAO =
// context.getBean(GoodDAO.class);
// //전체 데이터 가져오기
// //System.out.println(goodDAO.allGood());
//
// //코드를 이용해서 데이터 1개 찾아오기
// //System.out.println(goodDAO.getGood(1));
//
// //삽입할 데이터를 생성
// /*
// Good good = new Good();
// good.setCode(8);
// good.setName("수박");
// good.setRegdate(new Date());
//
// System.out.println(
// goodDAO.insertGood(good));
// */
//
// //데이터 수정 확인
// /*
// Good good = new Good();
// good.setCode(1);
// good.setName("무화과");
// good.setRegdate(new Date());
//
// System.out.println(
// goodDAO.updateGood(good));
// */
//
// /*
// System.out.println(
// goodDAO.deleteGood(8));
// */
//
// /*
// System.out.println(
// goodDAO.searchName("감"));
// */
//
// Good good = new Good();
// good.setCode(8);
// good.setName("수박");
// good.setRegdate(new Date());
//
// System.out.println(
// goodDAO.insertGood(good));
//
// }catch(Exception e) {
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
try {
//스프링 설정 파일의 경로를 설정
GenericXmlApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
//데이터베이스 접속 정보 가져와서 접속이 되는지 테스트
DataSource ds = context.getBean(DataSource.class);
Connection con = ds.getConnection();
System.out.println(con);
context.close();
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
3) 예외가 발생하지 않아야 하고 콘솔에 Connection 관련 텍스트가 출력되어야 한다.
3. DTO 클래스 생성
![]() |
4. SQL을 저장하고 실행할 인터페이스를 생성하고 필요한 SQL을 작성
package mybatis.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import mybatis.domain.Good;
//MyBatis Mapper로 사용할 인터페이스
public interface GoodMapper {
//전체 데이터를 가져오는 메소드
@Select("select code, name, regdate from goods")
public List<Good> allGood();
//code를 가지고 하나의 데이터를 찾아오는 메소드
@Select("select code, name, regdate from goods where code=#{code}")
public Good getGood(int code);
//데이터를 삽입하는 메소드
@Insert("insert into goods(code, name, regdate) values(#{code}, #{name}, #{regdate})")
public int insertGood(Good good);
//데이터를 수정하는 메소드
@Update("update goods set name=#{name}, regdate=#{regdate} where code = #{code}")
public int updateGood(Good good);
//데이터를 삭제하는 메소드
@Delete("delete from goods where code = #{code}")
public int deleteGood(int code);
}
5. 스프링 설정 파일에 매퍼 인터페이스를 사용하는 MyBatis Bean을 생성
<?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"
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">
<!-- DataSource 빈 생성 코드 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<!-- 데이터베이스 종류 : Oracle -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<!-- 연결할 데이터베이스 URL -->
<property name="url" value="jdbc:oracle:thin:@192.168.0.200:1521:xe"/>
<!-- 사용할 데이터베이스 계정 -->
<property name="username" value="user05"/>
<!-- 사용할 계정의 비밀번호 -->
<property name="password" value="user05" />
</bean>
<!-- XML을 이용하는 MyBatis 사용을 위한 bean 설정
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="mappers/good.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>
-->
<!-- Mapper 엔터페이스를 사용하는 MyBatis bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="goodMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mybatis.dao.GoodMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 어노테이션을 사용할 수 있도록 해주는 설정 -->
<context:annotation-config />
<!-- bean을 자동 생성할 패키지 등록 -->
<context:component-scan base-package="mybatis" />
</beans>
6. GoodMapper를 주입받아서 사용하면 된다.
import java.sql.Connection;
import java.util.Date;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import mybatis.dao.GoodDAO;
import mybatis.dao.GoodMapper;
import mybatis.domain.Good;
public class Main {
public static void main(String[] args) {
// try {
// GenericXmlApplicationContext context =
// new GenericXmlApplicationContext(
// "applicationContext.xml");
//// DataSource dataSource =
//// context.getBean(DataSource.class);
//// //내용이 정상적으로 출력되면 데이터베이스 접속 성공
//// Connection con = dataSource.getConnection();
//// System.out.println(con);
//
// //SQLSession 주입받아보기
// //출력이 되면 MyBatis 설정에는 문제가 없음
//// SqlSession sqlSession =
//// context.getBean(SqlSession.class);
//// System.out.println(sqlSession);
//
// GoodDAO goodDAO =
// context.getBean(GoodDAO.class);
// //전체 데이터 가져오기
// //System.out.println(goodDAO.allGood());
//
// //코드를 이용해서 데이터 1개 찾아오기
// //System.out.println(goodDAO.getGood(1));
//
// //삽입할 데이터를 생성
// /*
// Good good = new Good();
// good.setCode(8);
// good.setName("수박");
// good.setRegdate(new Date());
//
// System.out.println(
// goodDAO.insertGood(good));
// */
//
// //데이터 수정 확인
// /*
// Good good = new Good();
// good.setCode(1);
// good.setName("무화과");
// good.setRegdate(new Date());
//
// System.out.println(
// goodDAO.updateGood(good));
// */
//
// /*
// System.out.println(
// goodDAO.deleteGood(8));
// */
//
// /*
// System.out.println(
// goodDAO.searchName("감"));
// */
//
// Good good = new Good();
// good.setCode(8);
// good.setName("수박");
// good.setRegdate(new Date());
//
// System.out.println(
// goodDAO.insertGood(good));
//
// }catch(Exception e) {
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
try {
//스프링 설정 파일의 경로를 설정
GenericXmlApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
//데이터베이스 접속 정보 가져와서 접속이 되는지 테스트
// DataSource ds = context.getBean(DataSource.class);
// Connection con = ds.getConnection();
// System.out.println(con);
GoodMapper goodMapper = context.getBean(GoodMapper.class);
System.out.println(goodMapper.allGood());
context.close();
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
=> XML을 이용하는 방식은 XML 파일에 SQL을 작성하고 DAO 클래스를 만들어서 SQL을 호출하는 형태
=> Mapper 인터페이스를 이용하면 하나의 인터페이스에 SQL과 메소드를 같이 선언해서 사용하는 방식인데 복잡한 동적인 SQL을 작성할 때는 불편하다.
**MyBatis 로그 확인
=> MyBatis를 사용할 때 로그를 조금 더 자세하게 출력하고자 하면 log4jdbc-log4j2-jdbc4 라이브러리를 사용할 수 있다.
1. pom.xml에 의존성을 설정
<!-- MyBatis Log 기록 라이브러리 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
2. 루트 디렉토리(src, src/main/java, src/main/resources 등)에 프로퍼티 설정 파일을 추가 : log4jdbc.log4j2.properties
내용 : log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
3. 데이터베이스 접속 부분에 log4jdbc:을 추가
applicationContext.xml 파일 url 부분에
<property name="url" value="jdbc:log4jdbc:oracle:thin:@192.168.0.200:1521:xe"/> 이렇게 수정
<?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"
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">
<!-- DataSource 빈 생성 코드 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<!-- 데이터베이스 종류 : Oracle -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<!-- 연결할 데이터베이스 URL -->
<property name="url" value="jdbc:log4jdbc:oracle:thin:@192.168.0.200:1521:xe"/>
<!-- 사용할 데이터베이스 계정 -->
<property name="username" value="user05"/>
<!-- 사용할 계정의 비밀번호 -->
<property name="password" value="user05" />
</bean>
<!-- XML을 이용하는 MyBatis 사용을 위한 bean 설정
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="mappers/good.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>
-->
<!-- Mapper 엔터페이스를 사용하는 MyBatis bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="goodMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mybatis.dao.GoodMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 어노테이션을 사용할 수 있도록 해주는 설정 -->
<context:annotation-config />
<!-- bean을 자동 생성할 패키지 등록 -->
<context:component-scan base-package="mybatis" />
</beans>
** Transaction
=> 논리적 작업단위
=> 한번에 이루어져야 하는 작업 단위
1. ACID 성질
Atomicity : 트랜잭션은 All or Nothing
Consistency : 일관성이 있어야한다.
Isolation : 다른 트랜잭션과 격리되어야 한다.
Durability : 한번 완료된 트랜잭션은 계속되어야 한다. (영속성)
2. 트랜잭션 작업
=> commit : 작업 완료되서 데이터베이스 원본에 반영
=> rollback : 작업을 취소해서 데이터베이스 원본에 반영하지 않는 것
=> savepoint : rollback 할 지점을 만드는 것
3. Auto Commit
=> SQL이 정상적으로 수행되면 자동으로 Commit되는 경우
=> DDL(Create, Alter, Drop, Truncate, Rename 등)을 성공적으로 수행한 경우
=> DCL(Grant, Revoke - 권한 부여 및 회수)을 성공적으로 수행한 경우
=> 데이터베이스 접속 프로그램을 정상적으로 종료하는 경우
=> 데이터베이스가 정상적으로 종료되는 경우
4. Auto Rollback
=> 접속 프로그램이나 데이터베이스가 비정상적으로 종료된 경우
5. select 는 트랜잭션과 아무런 상관이 없다.
6. MyBatis는 트랜잭션 사용이 옵션이지만 HIbernate는 트랜잭션 사용이 필수