|3| Mapper

ํ˜ธ๋–กยท2022๋…„ 10์›” 19์ผ
0

๐Ÿ“Œ ์‚ฌ์šฉํ•  SQL๋ฌธ ์ •์˜


<?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.ssafy.board.model.dao.BoardDao">

	<resultMap type="Board" id="boardMap">
		<result column="id" property="id"/>
		<result column="writer" property="writer"/>
		<result column="title" property="title"/>
		<result column="content" property="content"/>
		<result column="reg_date" property="regDate"/>
		<result column="view_cnt" property="viewCnt"/>
	</resultMap>

	<!-- ์ „์ฒด ๊ธ€ ์กฐํšŒ -->
	<select id="selectAll" resultType="Board">
		SELECT id, writer, content, title, view_cnt as viewCnt, date_format(reg_date, '%Y-%M-%d') as regDate 
		FROM board
	</select>
	
	<!-- ํ•˜๋‚˜์˜ ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ -->
	<select id="selectOne" parameterType="int" resultMap="boardMap">
		SELECT id, writer, content, title, view_cnt, date_format(reg_date, '%Y-%M-%d %H:%i:%s') as reg_date
		FROM board
		WHERE id = #{id}
	</select>
	
	<!-- ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก -->
	<insert id="insertBoard" parameterType="Board">
		INSERT INTO board (title, writer, content)
		VALUES (#{title}, #{writer}, #{content})
	</insert>
	
	<!-- ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ -->
	<delete id="deleteBoard" parameterType="int">
		DELETE FROM board
		WHERE id = #{id}
	</delete>
	
	<!-- ๊ฒŒ์‹œ๊ธ€ ์—…๋ฐ์ดํŠธ ( ๋“ฑ๋ก๋‚ ์งœ๋ฅผ ๊ทธ๋ƒฅ ์ˆ˜์ •๋‚ ์งœ์ฒ˜๋Ÿผ ํ™œ์šฉ์„ ํ•ด๋ณธ๋‹ค ) --> 
	<update id="updateBoard" parameterType="Board">
		UPDATE board
		SET title = #{title}, content = #{content}, reg_date = now()
		WHERE id = #{id} 
	</update>	
	
	<!-- ์กฐํšŒ์ˆ˜ ์ฆ๊ฐ€ -->
	<update id="updateViewCnt" parameterType="int">
		UPDATE board
		SET view_cnt = view_cnt + 1
		WHERE id = #{id}
	</update>
	
	
	
	
	
	
	
</mapper>
  • namespace: xml Mapper์˜ namespace๋ฅผ Mapper Interface๋กœ ์„ค์ •ํ•œ๋‹ค. ์ฆ‰, ์—ฐ๊ฒฐ์‹œํ‚ฌ ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž… Dao์˜ ํ’€ํŒจํ‚ค์ง€๋ช…์„ ๊ธฐ์žฌํ•œ๋‹ค.
  • xml Mapper์˜ id์™€ Mapper Interface์˜ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ผ์น˜์‹œํ‚จ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€