📌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>com.springbook</groupId>
<artifactId>biz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MybatisProject</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<!-- Ibatis -->
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
📌프로젝트 선택 후 - Properties- Project Facets - Java - jdk 선택
📌src > main > webapp > lib 폴더 생성후 ojdbc.jar
를 넣어준다.
📌Maven Dependencies
에 mybatis
와 ibatis
가 들어간걸 확인!
📌Mybatis Mapper XML
선택
📌board-mapping.xml
생성
📌src/main/resourses에 mappings
라는 패키지 생성 후
미리 만든 board-mapping.xml
을 옮겨준다.
New-Others... > Mybatis Configuration XML
선택
📌src/main/resources
로 db.properties와 sql-map-config.xml을 이동
📌db.properties 수정
📌sql-map-config.xml 수정
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Properties 파일 설정 -->
<properties resource="db.properties" />
<!-- Alias 설정 -->
<typeAliases>
<typeAlias type="com.springbook.biz.board.BoardVO" alias="board"></typeAlias>
</typeAliases>
<!-- Datasource 설정 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<!-- Sql Mapper 설정 -->
<mappers>
<mapper resource="mappings/board-mapping.xml" />
</mappers>
</configuration>
📌SqlSession 객체 생성
SqlSessionFactoryBean
작성
package com.springbook.biz.util;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionFactoryBean {
private static SqlSessionFactory sessionFactory = null;
static {
try {
if(sessionFactory == null) {
Reader reader = Resources.getResourceAsReader("sql-map-config.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSessionInstance() {
return sessionFactory.openSession();
}
}
📌BoardDAO 작성
package com.springbook.biz.board.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.util.SqlSessionFactoryBean;
public class BoardDAO {
private SqlSession mybatis;
public BoardDAO() {
mybatis = SqlSessionFactoryBean.getSqlSessionInstance();
}
public void insertBoard(BoardVO vo) {
//mapper에 있는 name으로 호출
mybatis.insert("BoardDAO.insertBoard",vo);
mybatis.commit();
}
public void updateBoard(BoardVO vo) {
mybatis.update("BoardDAO.updateBoard",vo);
mybatis.commit();
}
public void deleteBoard(BoardVO vo) {
mybatis.delete("BoardDAO.deleteBoard",vo);
mybatis.commit();
}
public BoardVO getBoard(BoardVO vo) {
return (BoardVO) mybatis.selectOne("BoardDAO.getBoard",vo);
}
public List<BoardVO> getBoardList(BoardVO vo){
return mybatis.selectList("BoardDAO.getBoardList",vo);
}
}
📌mappings - board-mapping.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="BoardDAO">
<insert id="insertBoard">
<![CDATA[
INSERT INTO BOARD(SEQ,TITLE,WRITER,CONTENT)
VALUES((SELECT NVL(MAX(SEQ),0+1 FROM BOARD),#{title},#{writer},#{content}
]]>
</insert>
<update id="updateBoard">
<![CDATA[
UPDATE BOARD
SET TITLE=#{title},
CONTENT=#{content}
WHERE SEQ=#{seq}
]]>
</update>
<delete id="deleteBoard">
<![CDATA[
DELETE BOARD
WHERE SEQ=#{seq}
]]>
</delete>
<select id="getBoard" resultType="board">
<![CDATA[
SELECT *
FROM BOARD
WHERE SEQ=#{seq}
]]>
</select>
<select id="getBoardList" resultType="board">
<![CDATA[
SELECT *
FROM BOARD
WHERE TITLE LIKE '%'||#{searchKeyword}||'%'
ORDER BY SEQ DESC
]]>
</select>
</mapper>
id
는 유일해야하지만, 다른 mapper에서 id가 겹쳐도 namespace로 구분하기 때문에 이 경우에는 동일해도 다른 id로 처리된다.
Mapper파일에 등록한 SQL구문에서 '<' 나 '>'같은 기호를 '작다'라는 의미의 연산자가 아닌 또 다른 태그의 시작으로 처리하기 때문에 에러가 발생한다.
이때, CDATA Section
으로 SQL 구문을 감싸주어야한다.
SQL구문을 모두 대문자
로 표현하여 식별성을 높인다.
pom.xml 추가
applicationContext.xml 추가