๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [MyBatis] ๋งคํ(Mapping)/mapper ์ค์ ๋ณธ๋ฌธ
JSP ํ๋ก๊ทธ๋๋ฐ - [MyBatis] ๋งคํ(Mapping)/mapper ์ค์
๋ํจ๋ 2020. 7. 13. 16:47MyBatis ๊ฐ์ฒด์ ์ค์ฝํ์ ์๋ช ์ฃผ๊ธฐ
SqlSessionFactoryBuilder -> SqlSessionFactory -> SqlSession -> Mapper
SqlSessionFactoryBuilder
์ด ํด๋์ค๋ ์ธ์คํด์คํ ๋์ด ์ฌ์ฉ๋๊ณ ๋์ ธ์ง ์ ์๋ค. SqlSessionFactory ๋ฅผ ์์ฑํ ํ ์ ์งํ ํ์๋ ์๋ค. ๊ทธ๋ฌ๋ฏ๋ก SqlSessionFactoryBuilder ์ธ์คํด์ค์ ๊ฐ์ฅ ์ข์ ์ค์ฝํ๋ ๋ฉ์๋ ์ค์ฝํ(์๋ฅผ๋ค๋ฉด ๋ฉ์๋ ์ง์ญ๋ณ์)์ด๋ค. ์ฌ๋ฌ ๊ฐ์ SqlSessionFactory ์ธ์คํด์ค๋ฅผ ๋น๋ํ๊ธฐ ์ํด SqlSessionFactoryBuilder๋ฅผ ์ฌ์ฌ์ฉํ ์๋ ์์ง๋ง ์ ์งํ์ง ์๋ ๊ฒ์ด ๊ฐ์ฅ ์ข๋ค.
SqlSessionFactory
ํ๋ฒ ๋ง๋ ๋ค SqlSessionFactory๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ ๋์ ์กด์ฌํด์ผ๋ง ํ๋ค. ๊ทธ๋์ ์ญ์ ํ๊ฑฐ๋ ์ฌ์์ฑํ ํ์๊ฐ ์๋ค. ์ ํ๋ฆฌ์ผ์ด์ ์ด ์คํ๋๋ ๋์ ์ฌ๋ฌ ์ฐจ๋ก SqlSessionFactory ๋ฅผ ๋ค์ ๋น๋ํ์ง ์๋ ๊ฒ์ด ๊ฐ์ฅ ์ข์ ํํ์ด๋ค. ์ฌ๋น๋ํ๋ ํํ๋ ์ข์ง ์๋ค. ๊ทธ๋ฌ๋ฏ๋ก SqlSessionFactory ์ ๊ฐ์ฅ ์ข์ ์ค์ฝํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ค์ฝํ์ด๋ค. ์ ํ๋ฆฌ์ผ์ด์ ์ค์ฝํ๋ก ์ ์งํ๊ธฐ ์ํ ๋ค์ํ ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค. ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ์ฑ๊ธํด ํจํด์ด๋ static ์ฑ๊ธํด ํจํด์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ๋๋ ๊ตฌ๊ธ ์ฅฌ์ค๋ ์คํ๋ง๊ณผ ๊ฐ์ ์์กด์ฑ ์ฝ์ ์ปจํ ์ด๋๋ฅผ ์ ํธํ ์๋ ์๋ค. ์ด๋ฌํ ํ๋ ์์ํฌ๋ SqlSessionFactory์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ฑ๊ธํด์ผ๋ก ๊ด๋ฆฌํ ๊ฒ์ด๋ค.
SqlSession
๊ฐ๊ฐ์ ์ฐ๋ ๋๋ ์์ฒด์ ์ผ๋ก SqlSession์ธ์คํด์ค๋ฅผ ๊ฐ์ ธ์ผ ํ๋ค. SqlSession์ธ์คํด์ค๋ ๊ณต์ ๋์ง ์๊ณ ์ฐ๋ ๋์ ์์ ํ์ง๋ ์๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๊ฐ์ฅ ์ข์ ์ค์ฝํ๋ ์์ฒญ ๋๋ ๋ฉ์๋ ์ค์ฝํ์ด๋ค. SqlSession ์ static ํ๋๋ ํด๋์ค์ ์ธ์คํด์ค ํ๋๋ก ์ง์ ํด์๋ ์๋๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ธ๋ฆฟ ํ๋ ์์ํฌ์ HttpSession ๊ณผ ๊ฐ์ ๊ด๋ฆฌ ์ค์ฝํ์ ๋ฌ์๋ ์๋๋ค. ์ด๋ ํ ์ข ๋ฅ์ ์น ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด HTTP ์์ฒญ๊ณผ ์ ์ฌํ ์ค์ฝํ์ ๋๋ ๊ฒ์ผ๋ก ๊ณ ๋ คํด์ผ ํ๋ค. ๋ฌ๋ฆฌ ๋งํด์ HTTP ์์ฒญ์ ๋ฐ์๋๋ง๋ค ๋ง๋ค๊ณ ์๋ต์ ๋ฆฌํดํ ๋๋ง๋ค SqlSession ์ ๋ซ์ ์ ์๋ค. SqlSession ์ ๋ซ๋ ๊ฒ์ ์ค์ํ๋ค. ์ธ์ ๋ finally ๋ธ๋ก์์ ๋ซ์์ผ๋ง ํ๋ค. ๋ค์์ SqlSession์ ๋ซ๋ ๊ฒ์ ํ์ธํ๋ ํ์ค์ ์ธ ํํ๋ค.
try (SqlSession session = sqlSessionFactory.openSession()) {
// do work
}
์ฝ๋์ ๋ฐ์ ์ด๋ฐ ํํ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์์ ์ ๋ซ๋ ๊ฒ์ผ๋ก ๋ณด์ฅํ๊ฒ ํ ๊ฒ์ด๋ค.
Mapper ์ธ์คํด์ค
Mapper๋ ๋งคํ๋ ๊ตฌ๋ฌธ์ ๋ฐ์ธ๋ฉ ํ๊ธฐ ์ํด ๋ง๋ค์ด์ผ ํ ์ธํฐํ์ด์ค์ด๋ค. mapper ์ธํฐํ์ด์ค์ ์ธ์คํด์ค๋ SqlSession ์์ ์์ฑํ๋ค. ๊ทธ๋์ mapper ์ธ์คํด์ค์ ๊ฐ์ฅ ์ข์ ์ค์ฝํ๋ SqlSession ๊ณผ ๋์ผํ๋ค. ์ด์จ๋ mapper ์ธ์คํด์ค์ ๊ฐ์ฅ ์ข์ ์ค์ฝํ๋ ๋ฉ์๋ ์ค์ฝํ์ด๋ค. ์ฌ์ฉํ ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด ์์ฑ๋๊ณ ๋๋๋ค. ๋ช ์์ ์ผ๋ก ๋ซ์ ํ์๋ ์๋ค.
try (SqlSession session = sqlSessionFactory.openSession()) {
BlogMapper mapper = session.getMapper(BlogMapper.class);
// do work
}
์ด์ ์ jsp๋ก ์์ฑํ ํ์๊ฐ์ /๋ก๊ทธ์ธ, ๊ฒ์ํ, ํ์ผ๊ฒ์ํ (member, board, file
com.java.myBatis ํจํค์ง๋ฅผ ์ถ๊ฐํด์ SqlCofig.xml, SqlManager.java ํ์ผ์ ์์ฑํ๋ค.
SqlConfig.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="mvc"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="/com/java/member/model/mapper/MemberMapper.xml"/>
<mapper resource="/com/java/board/model/mapper/BoardMapper.xml"/>
</mappers>
</configuration>
SqlManager.java
package com.java.myBatis;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlManager {
private static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory getInstance() {
try {
String resource = "com/java/myBatis/SqlConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
}
com.java.member.model.mapping ํจํค์ง๋ฅผ ์ถ๊ฐํด์ MemberMapping.xml ํ์ผ์ ์์ฑํ๋ค.
MemberMapping.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.java.member.model.mapper.MemberMapper">
<resultMap type="com.java.member.model.MemberDto" id="member_dto">
<result column="num" property="num"/>
<result column="id" property="id"/>
<result column="password" property="password"/>
<result column="name" property="name"/>
<result column="jumin1" property="jumin1"/>
<result column="jumin2" property="jumin2"/>
<result column="email" property="email"/>
<result column="zipcode" property="zipcode"/>
<result column="address" property="address"/>
<result column="job" property="job"/>
<result column="mailing" property="mailing"/>
<result column="interest" property="interest"/>
<result column="member_level" property="memberLevel"/>
<result column="register_date" property="registerDate"/>
</resultMap>
<!-- ๋ค์ด์ค๋ ๊ฒ์ parameterType ๋๊ฐ๋ ๊ฒ์ resultType id๋ ์๋ณ์, insert๋ resultType ์๋ต -->
<insert id="member_insert" parameterType="com.java.member.model.MemberDto">
insert into member values(
member_num_seq.nextval,
#{id},
#{password},
#{name},
#{jumin1},
#{jumin2},
#{email},
#{zipcode},
#{address},
#{job},
#{mailing},
#{interest},
#{memberLevel},
sysdate
)
</insert>
<select id="id_check" parameterType="java.lang.String" resultType="java.lang.String">
select id from member
where id = #{id}
</select>
<select id="member_zipcode" parameterType="String" resultType="com.java.member.model.MemberDto">
select * from zipcode
where dong = ${checkDong}
</select>
<select id="member_login" parameterType="java.util.Map" resultType="String">
select member_level from member
where id = #{id}
and password = #{password}
</select>
<select id="member_select" parameterType="String" resultMap="member_dto">
select * from member
where id = #{id}
</select>
<update id="member_update" parameterType="com.java.member.model.MemberDto">
update member set password = #{password},
email = #{email},
zipcode = #{zipcode},
address = #{address},
job = #{job},
mailing = #{mailing},
interest = #{interest}
where num = #{num}
</update>
<delete id="member_delete" parameterType="java.util.Map">
delete from member where id = #{id} and password = #{password}
</delete>
</mapper>
BoardDao.java
package com.java.board.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.java.database.ConnectionProvider;
import com.java.database.jdbcUtil;
import com.java.myBatis.SqlManager;
public class BoardDao {
private static SqlSessionFactory sqlSessionFactory = SqlManager.getInstance();
private SqlSession session;
private static BoardDao instance = new BoardDao();
public static BoardDao getInstance() {
return instance;
}
public int insert(BoardDto boardDto) {
Connection conn = null;
PreparedStatement pstmt = null;
int value = 0;
writeNumber(boardDto);
try {
session = sqlSessionFactory.openSession();
value = session.insert("board_insert", boardDto);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return value;
}
public void writeNumber(BoardDto boardDto) {
// ๊ทธ๋ฃน๋ฒํธ(ROOT), ๊ธ์์(์์), ๊ธ๋ ๋ฒจ(์์)
int boardNumber = boardDto.getBoardNumber(); // 0
int groupNumber = boardDto.getGroupNumber(); // 1
int sequenceNumber = boardDto.getSequenceNumber(); // 0
int sequenceLevel = boardDto.getSequenceLevel(); // 0
try {
if (boardNumber == 0) { // ROOT : ๊ทธ๋ฃน๋ฒํธ
session = sqlSessionFactory.openSession();
int max = session.selectOne("board_group_number_max");
if(max!=0) boardDto.setGroupNumber(max+1);
} else { // ๋ต๊ธ : ๊ธ ์์, ๊ธ ๋ ๋ฒจ
HashMap<String, Integer> hMap = new HashMap<String, Integer>();
session = sqlSessionFactory.openSession();
hMap.put("sequenceNumber", sequenceNumber);
hMap.put("sequenceLevel", sequenceLevel);
session.update("board_sequence_number", hMap);
sequenceNumber = sequenceNumber + 1;
sequenceLevel = sequenceLevel + 1;
boardDto.setSequenceNumber(sequenceNumber);
boardDto.setSequenceLevel(sequenceLevel);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
public int getCount() {
int value = 0;
try {
session = sqlSessionFactory.openSession();
value = session.selectOne("board_count");
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return value;
}
public List<BoardDto> getBoardList(int startRow, int endRow) {
HashMap<String, Integer> hMap = new HashMap<String, Integer>();
hMap.put("startRow", startRow);
hMap.put("endRow", endRow);
List<BoardDto> boardList = null;
try {
session = sqlSessionFactory.openSession();
boardList = session.selectList("board_list", hMap);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return boardList;
}
public BoardDto read(int boardNumber) {
BoardDto boardDto = null;
try {
session = sqlSessionFactory.openSession();
session.update("board_update_count", boardNumber);
boardDto = session.selectOne("board_read", boardNumber);
session.commit();
}catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally {
session.close();
}
return boardDto;
}
public int delete(int boardNumber, String password) {
HashMap<String, Object> hMap = new HashMap<String, Object>();
int value = 0;
try {
session = sqlSessionFactory.openSession();
hMap.put("boardNumber", boardNumber);
hMap.put("password", password);
value = session.delete("board_delete", hMap);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
} finally {
session.close();
}
return value;
}
public BoardDto getContentList(int boardNumber) {
BoardDto boardDto = null;
try {
session = sqlSessionFactory.openSession();
boardDto = session.selectOne("board_select", boardNumber);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return boardDto;
}
public int update(int boardNumber, BoardDto boardDto) {
HashMap<String, Object> hMap = new HashMap<String, Object>();
int value = 0;
try {
session = sqlSessionFactory.openSession();
hMap.put("boardNumber", boardNumber);
hMap.put("boardDto", boardDto);
value = session.update("board_update", hMap);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
} finally {
session.close();
}
return value;
}
}
ArrayList๋ก ์์ฑํ๋ ๊ฒ์ List๋ก ์ ์บ์คํ ํด์ฃผ๊ณ ListCommand.java ํ์ผ์์๋ List๋ก ๋ฐ๊ฟ์ค๋ค.
BoardMapping.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.java.board.model.mapper.BoardMapper">
<resultMap type="com.java.board.model.BoardDto" id="board_dto">
<result column="board_number" property="boardNumber"/>
<result column="num" property="num"/>
<result column="id" property="id"/>
<result column="password" property="password"/>
<result column="name" property="name"/>
<result column="jumin1" property="jumin1"/>
<result column="jumin2" property="jumin2"/>
<result column="email" property="email"/>
<result column="zipcode" property="zipcode"/>
<result column="address" property="address"/>
<result column="job" property="job"/>
<result column="mailing" property="mailing"/>
<result column="interest" property="interest"/>
<result column="member_level" property="memberLevel"/>
<result column="register_date" property="registerDate"/>
</resultMap>
<select id="board_group_number_max" resultType="int">
select nvl(max(group_number), 0) from board
</select>
<update id="board_sequence_number" parameterType="java.util.HashMap">
update board set sequence_number = sequence_number+1
where group_number = #{sequenceLevel}
and sequence_number > #{sequenceNumber}
</update>
<select id="board_insert" parameterType="com.java.board.model.BoardDto">
insert into board(
board_number,
writer,
subject,
email,
content,
password,
write_date,
read_count,
group_number,
sequence_number,
sequence_level)
values (board_number_seq.nextval,
#{writer},
#{subject},
#{email},
#{content},
#{password},
#{writeDate},
#{readCount},
#{groupNumber},
#{sequenceNumber},
#{sequenceLevel})
</select>
<select id="board_count" resultType="int">
select count(*) from board
</select>
<select id="board_list" parameterType="java.util.HashMap" resultMap="board_dto">
<!-- ์ฟผ๋ฆฌ๋ฌธ ์์ ํน์๋ฌธ์๊ฐ ๋ง์ ๊ฒฝ์ฐ์ ๋ช
๋ น์ด๋ก ์ธ์ํด์ ์๋ฌ๊ฐ ๋๊ธฐ๋๋ฌธ์ ํ์ -->
<![CDATA[
select * from
(select rownum as rnum, a.* from
(select * FROM board
order by group_number desc,
sequence_number asc) a) b
where b.rnum >= #{startRow} and b.rnum <= #{endRow}
]]>
</select>
<update id="board_update_count" parameterType="int">
<![CDATA[
update board set read_count = read_count+1
where board_number = #{boardNumber}
]]>
</update>
<select id="board_read" parameterType="int" resultMap="board_dto">
select * from board
where board_number = #{boardNumber}
</select>
<delete id="board_delete" parameterType="java.util.HashMap">
delete from board
where board_number = #{boardNumber}
and password = #{password}
</delete>
<select id="board_select" parameterType="int" resultMap="board_dto">
select * from board
where board_number = #{boardNumber}
</select>
<update id="board_update" parameterType="java.util.HashMap">
update board set subject = #{boardDto.subject},
email = #{boardDto.email},
content = #{boardDto.content},
password = #{boardDto.password},
write_date = #{boardDto.writeDate}
where board_number = #{boardDto.boardNumber}
</update>
</mapper>
int,String,float์ java.lang ํจํค์ง๋ช ์์จ๋ ๋จ.