๐ŸŒธ [SPRING] | Mapper ( interface , xml )

0


๐Ÿ”น resultType


https://java119.tistory.com/45


๐Ÿ”น UserMapper.interface ย ย ย ย ย UserMapper.xml

  • interface์— ์žˆ๋Š” ์ถ”์ƒ๋ฉ”์†Œ๋“œ๊ฐ€ xml์—๋„ ์žˆ์–ด์•ผ ํ•ด์š”

๐Ÿ”น UserMapper.xml + UserMapper.java ๐Ÿ‘‰ MYBATIS

  • UserMapper.interface UserMapper.xml ๋‘ ๊ฐœ์˜ ๋‚ด์šฉ์œผ๋กœ MyBatis๊ฐ€ DAO๋ฅผ ๋งŒ๋“ค์–ด์ค˜์š”. ๋‘ ๊ฐœ๊ฐ€ ์„ธํŠธ์˜ˆ์š”. ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ฆฌ ๋ˆˆ์—๋Š” ์•ˆ๋ณด์—ฌ์š”.
  • mybatis๋Š” ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” xml์„ ๊ฐ€์ง€๊ณ  DAOํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์š”
  • ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๊ณ , xml์˜ sql์„ ๊ฐ€์ง€๊ณ  DAO๋กœ(์•„๋ž˜์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ) ๋งŒ๋“ค์–ด์š”.
public static int joinUser(UserVO param) {
		Connection con = null;
		PreparedStatement ps = null;
		
		String sql = "INSERT INTO t_user "
				+ "(uid, upw, unm, gender) "
				+ "VALUES "
				+ "(?, ?, ?, ?)";
		
		try {
			con = DBUtils.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, param.getUid());
			ps.setString(2, param.getUpw());
			ps.setString(3, param.getUnm());
			ps.setInt(4, param.getGender());			
			return ps.executeUpdate();
			
		} catch (Exception e) {			
			e.printStackTrace();
			return 0;
		} finally {
			DBUtils.close(con, ps);
		}
	}
  • ์ด๋ ‡๊ฒŒ mapper 2๊ฐœ๋กœ (์ธํ„ฐํŽ˜์ด์Šค์™€ xml๊ฐ€์ง€๊ณ  ) ๋นˆ๋“ฑ๋ก๋„ํ•ด์ฃผ๊ณ  ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์„œ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ค˜์š”.
  • mapper.selBoardList()์ด๋ ‡๊ฒŒ ๊ฐ–๋‹ค์“ฐ๋Š” ๊ฒƒ์ด์˜ˆ์š”.
  • bean๋“ฑ๋ก๊นŒ์ง€ ๋˜์–ด ์žˆ์œผ๋‹ˆ๊นŒ ๊ฐ์ฒด์ž–์•„์š”. ๊ทธ๋ž˜์„œ ์ด๋ ‡๊ฒŒ ๋ง‰ ๊ฐ–๋‹ค ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์˜ˆ์š”.
  • bean๋“ฑ๋ก ์™œํ•˜๊ฒ ์–ด์š”? DIํ• ๋ ค๊ณ ์š”!


๐Ÿ”น mapper.xml์–‘์‹ ํ…œํ”Œ๋ › ๋“ฑ๋ก

UserMapper.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.koreait.first.user.UserMapper">
    <insert id="insUser">
        INSERT INTO t_user
        (uid, upw, unm, gender)
        VALUES
        (#{uid}, #{upw}, #{unm}, #{gender})
    </insert>

</mapper>
  • ์ด mapper.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="#[[$END$]]#">

</mapper>

๐Ÿ”น mapper.xml์—์„œ SQL์“ฐ๋Š” ๋ฒ•

UserMapper.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.koreait.first.user.UserMapper">
    <insert id="insUser">
        INSERT INTO t_user
        (uid, upw, unm, gender)
        VALUES
        (#{uid}, #{upw}, #{unm}, #{gender})
    </insert>

</mapper>
  • <insert id="์“ฐ๊ณ  ์‹ถ์€ ๋ฉ”์†Œ๋“œ๋ช…"> ํƒœ๊ทธ ์‚ฌ์ด์— SQL์จ์š”. ์ด ๋•Œ ?์€ ์•ˆ์จ์š”.
  • ๋Œ€์‹  ?์ž๋ฆฌ์— ์ˆœ์„œ๋Œ€๋กœ #{๋ฉค๋ฒ„ํ•„๋“œ๋ช…}์„ ์ ์–ด์ฃผ๋ฉด ๋ผ์š”.
  • ${}์€ ๋ฌธ์ž์—ด์ด๋“  ์ˆซ์ž๋˜ ''๋ฅผ ์•ˆ๋ถ™์—ฌ์š”. ๐Ÿ‘‰ ''์—†์ด ๋ฌธ์ž์—ด์„ ์“ฐ๊ณ  ์‹ถ์„ ๋•Œ ์จ์š”.
  • #{}์€ ๋ฌธ์ž์—ด์ด๋ฉด ''๋ถ™์ด๊ณ  ์ˆซ์ž๋ฉด ์•ˆ๋ถ™์—ฌ์š”.
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

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

comment-user-thumbnail
2021๋…„ 6์›” 2์ผ

์ € ๋„ˆ๋ฌด ์ข‹์•„ํ•˜์‹œ๋Š” ๊ฑฐ ์•„๋‹ˆ์—์š”๐Ÿฅฐ

1๊ฐœ์˜ ๋‹ต๊ธ€