JSP + MyBatis3 ½±°Ô Àû¿ëÇϱâ #2
Áö³¹ø¿¡ ½±°í ºü¸£°Ô JSP¿Í MyBatis¸¦ ÀÌ¿ëÇÏ¿© µ¥ÀÌÅ͸¦ SELECT ÇÏ´Â ºÎºÐÀ» Æ÷½ºÆÃÇß´Ù.
¿À´ÃÀº MyBatis¸¦ ÀÌ¿ëÇÏ¿© INSERT, UPDATE, DELETE ÇÏ´Â ¹æ¹ýÀ» ¼³¸íÀ» ÇÏ°Ú´Ù.
Áö³¹ø¿¡ XML Mapper ÆÄÀÏ¿¡ SELECT Äõ¸®¸¦ ÀÛ¼ºÇÒ¶§ ¾Æ·¡¿Í °°Àº ű׸¦ ÀÌ¿ëÇÏ¿´´Ù.
INSERT, UPDATE, DELETE¸¦ ÇÒ¶§ »ç¿ëµÇ´Â ű׵µ Å©°Ô ´Ù¸£Áö ¾Ê´Ù.
1. mapper ¿¡ ±¸¹® Ãß°¡
<
insert
id
=
"insContents"
parameterType
=
"hashmap"
>
INSERT INTO
TB_test(id, name)
VALUES
(#{id}, #{name})
</
insert
>
<
update
id
=
"updateContents"
parameterType
=
"hashmap"
>
UPDATE
TB_test
SET
name = #{name}
WHERE
id = #{id}
</
update
>
<
delete
id
=
"deleteContents"
parameterType
=
"hashmap"
>
DELETE FROM TB_test WHERE id = #{id}
</
delete
>
Ãâó: http://fruitdev.tistory.com/30 [°úÀÏ°¡°Ô °³¹ßÀÚ]
* ÅÂ±× ¸í¿¡ ÁÖÀÇÇÏ¸é¼ ³Ö´Â´Ù. °¡·É
2. INSERT, UPDATE, DELETE
<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <% @page import = "com.test.sqlMap.SqlSessionManager" %> <% @page import = "org.apache.ibatis.session.SqlSessionFactory" %> <% @page import = "org.apache.ibatis.session.SqlSession" %> <% @page import = "java.util.*" %> <% SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSession(); SqlSession sqlSession = sqlSessionFactory.openSession(); try { HashMap pHm = new HashMap(); pHm.put( "name" , "È«±æµ¿" ) ; pHm.put( "age" , "28" ) ; // Insert sqlSession.insert( "Test.insContents" , pHm) ; // Update sqlSession.update( "Test.updateContents" , pHm) ; // ¿µÇâ¹ÞÀº ·¹ÄÚµå °³¼ö¸¦ ¸®ÅÏ int upCnt = sqlSession.update( "Test.updateContents" , pHm) ; // DELETE sqlSession.delete( "Test.deleteContents" , pHm) ; // ¿µÇâ¹ÞÀº ·¹ÄÚµå °³¼ö¸¦ ¸®ÅÏ int delCnt = sqlSession.delete( "Test.deleteContents" , pHm) ; // Ä¿¹Ô sqlSession.commit() ; } catch (Exception e){ e.printStackTrace() ; } finally { sqlSession.close() ; } %> |
Ãâó: http://fruitdev.tistory.com/30 [°úÀÏ°¡°Ô °³¹ßÀÚ]
[°£·«Á¤¸®]
±¸ºÐ | mapper | java/jsp |
INSERT | sqlSession.insert | |
UPDATE | sqlSession.update | |
DELETE | sqlSession.delete | |
COMMIT | ¡¡ | sqlSession.commit |
Ãâó: http://fruitdev.tistory.com/30 [°úÀÏ°¡°Ô °³¹ßÀÚ]