실습 4일차_MyBatis ★

 

 Spring MyBatis ★

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);
		
		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);
}

DeptServiceImpl.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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);
	}

}

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"
	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">

  <!-- 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>
  
  <!-- 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 : 

+ 강의 교재

 

+ Recent posts