1. article-mapper.xml

  • ArticleDAO.java 작성
package kr.co.company.hello.dao;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import kr.co.company.hello.vo.Article;

@Repository
public class ArticleDAO {
	
	@Autowired
	SqlSession sqlSession; //SqlSession 빈 DI
	
	public void insertArticle(Article article) {
		sqlSession.insert("mappers.article-mapper.insertArticle", article);
	}
	
	public Article selectArticleById(String articleId) {
		Article article = sqlSession.selectOne("mappers.article-mapper.selectArticleById", articleId);
		return article;
	}
}

  • article-mapper.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="mappers.article-mapper">
	<insert id="insertArticle" parameterType="kr.co.company.hello.vo.Article">
		insert into article(article_id
						  , author
						  , title
						  , content)
					values (#{articleId}
						  , #{author}
						  , #{title}
						  , #{content})
	</insert>
	
	<select id="selectArticleById" resultType="kr.co.company.hello.vo.Article" parameterType="string">
		select article_id as articleId
		     , author
		     , title
		     , content
		  from article
		 where article_id = #{articleId}
	</select>
</mapper>