MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[Spring] Persistence Layer ๊ตฌํ˜„ (Data Access)

HYEJU01 2024. 8. 24. 19:05

์˜์† ๊ณ„์ธต(Persistence Layer)

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•„ํ‚คํ…์ฒ˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ณ„์ธต์„ ์˜๋ฏธ

 

 

โ—ˆ  vo ํด๋ž˜์Šค ์ž‘์„ฑ

ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ vo ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

 

 

โ—ˆ  mapper ์ž‘์—… (interface , xml or @ ์ž‘์„ฑ)

1) root-context.xml ์—์„œ ํŒจํ‚ค์ง€ ์Šค์บ”์„ ์„ค์ •ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

2) xml ๋กœ ๊ตฌํ˜„ํ• ๊ฑด์ง€ @ ๋กœ ๊ตฌํ˜„ํ• ๊ฑด์ง€ ์„ ํƒํ•œ๋‹ค

๊ฐ„๋‹จํ•œ sql ์€ @ ์ถ”์ฒœ 

 

 

โ—ˆ  test ํด๋ž˜์Šค๋กœ DB ์ž‘๋™ ํ…Œ์ŠคํŠธ

1) @Autowired ๋ฅผ ํ†ตํ•ด์„œ Mapper ์ธํ„ฐํŽ˜์ด์…˜์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ฃผ์ž…์‹œํ‚จ๋‹ค.

2) SQL develop ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€ or ๊ฒฐ๊ณผ ๊ฐ’ ์ฒดํ‚น 

 

โ—ˆ xml or @ ์–ด๋…ธํ…Œ์ด์…˜ ์ž‘์„ฑ

1) xml

<mapper namespace="com.example.UserMapper">

    <!-- SQL ์ฟผ๋ฆฌ ์ •์˜ -->
    <select id="findUserById" parameterType="int" resultType="com.example.User">
        SELECT * FROM users WHERE id = #{id}
    </select>

    <insert id="insertUser" parameterType="com.example.User">
        INSERT INTO users (name, email) VALUES (#{name}, #{email})
    </insert>

    <update id="updateUser" parameterType="com.example.User">
        UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        DELETE FROM users WHERE id = #{id}
    </delete>

</mapper>

 

 

๐Ÿ˜Š CDATA ์„น์…˜ :: ํŠน์ˆ˜ ๋ฌธ์ž์™€ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ๋ฌธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ๋งค์šฐ ์œ ์šฉ

CDATA ์„น์…˜์€ XML์˜ ์š”์†Œ๋‚˜ ์†์„ฑ ๊ฐ’ ์•ˆ์—์„œ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. (xml ์— ๋ถ€๋“ฑํ˜ธ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ)

CDATA ์„น์…˜ ๋‚ด๋ถ€์˜ ๋‚ด์šฉ์€ XML ํŒŒ์„œ์— ์˜ํ•ด ํŒŒ์‹ฑ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์žˆ๋Š” ๊ทธ๋Œ€๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

<![CDATA[ CDATA ์„น์…˜์˜ ๋‚ด์šฉ ]]>


<mapper namespace="com.example.UserMapper">

    <!-- ๋ณต์žกํ•œ ์ฟผ๋ฆฌ -->
    <select id="findUsers" parameterType="map" resultType="com.example.User">
        <![CDATA[
            SELECT * FROM users
            WHERE name = #{name}
              AND age >= #{minAge}
              AND age <= #{maxAge}
        ]]>
    </select>

</mapper>
 

 

๐Ÿ˜Š <selectKey> ?? 

<selectKey> ์—˜๋ฆฌ๋จผํŠธ๋Š” INSERT ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฃผ๋กœ ๊ธฐ๋ณธ ํ‚ค ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ์ฟผ๋ฆฌ๋Š” INSERT ๋ฌธ ์‹คํ–‰ ์ „์— ๋˜๋Š” ํ›„์— ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค !!

์‚ฝ์ž… ์ „์— ๊ธฐ๋ณธ ํ‚ค(PK) ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ (BEFORE) !!c

 

  • keyProperty: ๊ธฐ๋ณธ ํ‚ค ๊ฐ’์„ ์ €์žฅํ•  ๋„๋ฉ”์ธ ๊ฐ์ฒด์˜ ์†์„ฑ์ž…๋‹ˆ๋‹ค.
  • resultType: ๊ธฐ๋ณธ ํ‚ค์˜ ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
  • before: true์ด๋ฉด INSERT ๋ฌธ ์ „์— selectKey ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜๊ณ , false์ด๋ฉด INSERT ๋ฌธ ํ›„์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.example.UserMapper">

    <!-- ๊ธฐ๋ณธ ํ‚ค๋ฅผ ์ž๋™ ์ƒ์„ฑํ•œ ํ›„ ์กฐํšŒ -->
    <insert id="insertUser" parameterType="com.example.User" useGeneratedKeys="true" keyProperty="id">
        <!-- ๊ธฐ๋ณธ ํ‚ค ์กฐํšŒ ์ฟผ๋ฆฌ -->
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>

        <!-- INSERT ๋ฌธ -->
        INSERT INTO users (name, email)
        VALUES (#{name}, #{email})
    </insert>

</mapper>

 

 

 

 

2) @ ์–ด๋…ธํ…Œ์ด์…˜

; ์„ธ๋ฏธ์ฝœ๋ก ์€ ์“ฐ์ง€ ์•Š๋Š”๋‹ค !

 XML ํŒŒ์ผ ์—†์ด ์ž๋ฐ” ์ฝ”๋“œ๋งŒ์œผ๋กœ ๋งคํผ๋ฅผ ์ •์˜ ๊ฐ€๋Šฅ

@Select("SELECT * FROM users WHERE id = #{id}") 
User findUserById(int id); 
@Insert("INSERT INTO users (name, email) VALUES (#{name}, #{email})")  void insertUser(User user);
@Update("UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}")  void updateUser(User user); 
 @Delete("DELETE FROM users WHERE id = #{id}")  void deleteUser(int id);