출처: ibatis
config.ibatis
sql-map-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="./config/properties/database.properties" />
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false" />
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driverClassName}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
</dataSource>
</transactionManager>
<sqlMap resource="./config/sqlmap/Board.xml" />
</sqlMapConfig>
config.sqlmap
Board.xml 파일
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<!-- 결과 맵 -->
<resultMap id="selectBoardListResultMap" class="example.dao.Board">
<result column="BOARD_SEQ" property="boardSeq" />
<result column="BOARD_TYPE" property="boardType" />
<result column="TITLE" property="title" />
<result column="REG_DATE" property="regDate" />
</resultMap>
<resultMap id="selectBoardResultMap" class="example.dao.Board">
<result column="BOARD_SEQ" property="boardSeq" />
<result column="BOARD_TYPE" property="boardType" />
<result column="CONTENTS" property="contents" />
<result column="TITLE" property="title" />
<result column="REG_DATE" property="regDate" />
</resultMap>
<!-- 게시물 목록 쿼리 -->
<select id="selectBoardList" resultMap="selectBoardListResultMap">
SELECT BOARD_SEQ, BOARD_TYPE, TITLE, REG_DATE
FROM T_BOARD
ORDER BY REG_DATE DESC
</select>
<select id="selectBoard" resultMap="selectBoardResultMap">
SELECT BOARD_SEQ, BOARD_TYPE, TITLE, CONTENTS, REG_DATE
FROM T_BOARD
WHERE BOARD_SEQ = #boardSeq#
</select>
<insert id="insertBoard" parameterClass="example.dao.Board">
INSERT INTO T_BOARD
(
BOARD_TYPE,
TITLE,
CONTENTS,
REG_DATE
)
VALUES
(
#boardType#,
#title#,
#contents#,
NOW()
)
</insert>
</sqlMap>
package example.ibatis;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class BaseSqlMapConfig {
//sqlmapclient는 최초선언 이후 변경되면 안됨. static 메소드로 접근가능하게
private static final SqlMapClient sqlMapClient;
//여기 안에 있는 메소드 이용해서 xml안의 태그를 호출한다.
static {
try {
String resource = "./config/ibatis/sql-map-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
System.out.println("sqlMapClient : " + sqlMapClient);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("ibatis config error : " + e);
}
}
public static SqlMapClient getSqlMapInstance() {
return sqlMapClient;
}
}
메소드들은 다음과 같다
queryForList와 queryForObject의 차이점
queryForObject : 1개의 레코드만 가져와 객체에 저장함. 하나 이상 반환되는 경우 예외처리
queryForList : 1개 이상의 레코드를 가져와 자바객체의 List를 만드는데 사용됨.
example.dao
BoardDao
package example.dao;
import java.sql.SQLException;
import java.util.List;
import example.ibatis.BaseSqlMapConfig;
public class BoardDao {
@SuppressWarnings("unchecked")
public List<Board> selectBoardList() throws SQLException {
return BaseSqlMapConfig.getSqlMapInstance().queryForList("selectBoardList");
}
public Board selectBoard(int boardSeq) throws SQLException {
return (Board) BaseSqlMapConfig.getSqlMapInstance().queryForObject("selectBoard", boardSeq);
}
public void insertBoard(Board board) throws SQLException {
BaseSqlMapConfig.getSqlMapInstance()
.insert("insertBoard", board);
}
}
WEB-INF의 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>java-web-02-jsp</display-name>
<servlet>
<servlet-name>boardList</servlet-name>
<servlet-class>example.servlet.BoardListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>boardList</servlet-name>
<url-pattern>/board/list</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>boardForm</servlet-name>
<servlet-class>example.servlet.BoardFormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>boardForm</servlet-name>
<url-pattern>/board/form</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>boardDetail</servlet-name>
<servlet-class>example.servlet.BoardDetailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>boardDetail</servlet-name>
<url-pattern>/board/detail</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
<sql>
refid - <sql>로 생성된 쿼리문을 refid=id로 해서 <include>로 가져온다