https://java119.tistory.com/45
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
์จ์. ์ด ๋?
์ ์์จ์.- ๋์
?
์๋ฆฌ์ ์์๋๋ก#{๋ฉค๋ฒํ๋๋ช }
์ ์ ์ด์ฃผ๋ฉด ๋ผ์.${}
์ ๋ฌธ์์ด์ด๋ ์ซ์๋''
๋ฅผ ์๋ถ์ฌ์. ๐''
์์ด ๋ฌธ์์ด์ ์ฐ๊ณ ์ถ์ ๋ ์จ์.#{}
์ ๋ฌธ์์ด์ด๋ฉด''
๋ถ์ด๊ณ ์ซ์๋ฉด ์๋ถ์ฌ์.
์ ๋๋ฌด ์ข์ํ์๋ ๊ฑฐ ์๋์์๐ฅฐ