
.png)
.png)
.png)
https://java119.tistory.com/45
interface์ ์๋ ์ถ์๋ฉ์๋๊ฐxml์๋ ์์ด์ผ ํด์
UserMapper.xml + UserMapper.java ๐ MYBATIS
UserMapper.interfaceUserMapper.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);
}
}
- ์ด๋ ๊ฒ
mapper2๊ฐ๋ก (์ธํฐํ์ด์ค์xml๊ฐ์ง๊ณ ) ๋น๋ฑ๋ก๋ํด์ฃผ๊ณ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ์ค๋ฒ๋ผ์ด๋ ํด์ ์ด๋ ๊ฒ ๋ง๋ค์ด์ค์.mapper.selBoardList()์ด๋ ๊ฒ ๊ฐ๋ค์ฐ๋ ๊ฒ์ด์์.bean๋ฑ๋ก๊น์ง ๋์ด ์์ผ๋๊น ๊ฐ์ฒด์์์. ๊ทธ๋์ ์ด๋ ๊ฒ ๋ง ๊ฐ๋ค ์ธ ์ ์๋ ๊ฑฐ์์.bean๋ฑ๋ก ์ํ๊ฒ ์ด์?DIํ ๋ ค๊ณ ์!
.png)
.png)
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>
.png)
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์จ์. ์ด ๋?์ ์์จ์.- ๋์
?์๋ฆฌ์ ์์๋๋ก#{๋ฉค๋ฒํ๋๋ช }์ ์ ์ด์ฃผ๋ฉด ๋ผ์.${}์ ๋ฌธ์์ด์ด๋ ์ซ์๋''๋ฅผ ์๋ถ์ฌ์. ๐''์์ด ๋ฌธ์์ด์ ์ฐ๊ณ ์ถ์ ๋ ์จ์.#{}์ ๋ฌธ์์ด์ด๋ฉด''๋ถ์ด๊ณ ์ซ์๋ฉด ์๋ถ์ฌ์.
์ ๋๋ฌด ์ข์ํ์๋ ๊ฑฐ ์๋์์๐ฅฐ