ÃֽŠ°Ô½Ã±Û(JAVA)
2017.04.27 / 19:28

JSP + MyBatis3 ½±°Ô Àû¿ëÇϱâ #1

Scoda
Ãßõ ¼ö 143

°³ÀÎÀûÀ¸·Î myBatis´Â °ø½Ä »çÀÌÆ®¸¦ Âü°íÇÏ¿© °øºÎÇÏ´Â°É ÃßõÇÑ´Ù.

Çѱ۷ΠµÇ¾î ÀÖÀ¸´Ï º¸±âµµ ÆíÇÏ°Ô µÇ¾î ÀÖ´Ù. [MyBatis °ø½Ä ȨÆäÀÌÁö ¹Ù·Î°¡±â]





±×·¡µµ "³­ Áö±Ý´çÀå mybatis¸¦ »ç¿ëÇØ¾ß ÇØ. ±×°Íµµ jsp¿¡ !!" ÇÏ´Â ºÐµéÀ» À§ÇØ  jsp¿¡ mybatis¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇÏ°Ú´Ù.



¿À´ÃÀº µ¥ÀÌÅ͸¦ °¡Á®¿À´Â(SELECT) ºÎºÐ¸¸ ¼³¸íÀ» ÁøÇàÇÏ°Ú´Ù. À̰͸¸ ÀÌÇØÇصµ myBatisÀÇ ³ª¸ÓÁö ±â´ÉÀ» »ç¿ëÇϴµ¥ ½±´Ù.



Å×½ºÆ® ȯ°æ : tomcat7 + jsp + mybatis3.1 + mysql5.1






¿À´Ã ¼³¸íÇÏ´Â »ùÇÃÀÇ Àüü ±¸Á¶´Â ´ÙÀ½°ú °°´Ù.





1. mybatis, mysql jdbc µå¶óÀ̹ö ´Ù¿î


 mybatis-3.1.1.jar


 mysql-connector-java-5.1.6.jar




2. ´Ù¿î¹ÞÀº ¶óÀ̺귯¸®´Â WEB-INF/lib ¾È¿¡ Ãß°¡ÇÑ´Ù.



3. com.test.sqlMap ÆÐÅ°Áö¸¦ ¸¸µé°í ±× ¾Æ·¡ SqlSessionManager.java¸¦ »ý¼ºÇÑ´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.test.sqlMap;
 
import java.io.IOException;
import java.io.Reader;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
public class SqlSessionManager {
 
     
    public static SqlSessionFactory sqlSession;
 
 
    static {
        String resource = "com/test/sqlMap/Configuration.xml";
         
        Reader reader;
         
 
        try {
            reader = Resources.getResourceAsReader( resource );
            sqlSession = new SqlSessionFactoryBuilder().build( reader );
             
        } catch (IOException e) {
            e.printStackTrace();
        }
         
         
    }
     
     
    public static SqlSessionFactory getSqlSession() {
        return sqlSession;
    }
}


* ÇØ´ç Ŭ·¡½º´Â myBatis ¼³Á¤ ÆÄÀÏÀ» Àоî DB Ä¿³Ø¼ÇÀ» ¸ÎÀºµÚ Ä¿³Ø¼Ç ¸ÎÀº sessionÀ» ¹ÝȯÇØ ÁØ´Ù



4. com.test.sqlMap ÆÐÅ°Áö ¾È¿¡ Configuration.xml ÆÄÀÏÀ» »ý¼ºÇÑ´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8" ?>
 
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
<configuration>
     
    <settings>
        <setting name="cacheEnabled" value="false" />
        <setting name="useGeneratedKeys" value="true" />
        <setting name="defaultExecutorType" value="REUSE" />
    </settings>
 
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/testDB" />
                <property name="username" value="test" />
                <property name="password" value="test12" />
            </dataSource>
        </environment>
    </environments>
 
    <mappers>
 
        <mapper resource="com/test/sqlMap/mapper/test.xml" />
         
    </mappers>
</configuration>

* DB Á¢¼ÓÁ¤º¸ ¹× ±âŸ ¼³Á¤µîÀ» ÇÒ ¼ö ÀÖ´Â ¼³Á¤ÆÄÀÏÀÌ´Ù. Property Á¤º¸¿¡´Â º»ÀÎÀÇ DB Á¤º¸¸¦ ÀÔ·ÂÇØ ÁØ´Ù.



5. com.test.sqlMap.mapper ÆÐÅ°Áö¸¦ ¸¸µé°í ±× ¾È¿¡ test.xml À» »ý¼ºÇÑ´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
<mapper namespace="Test">
     
    <select id="getCount" resultType="int">
     
        SELECT
            count(*)
        FROM
            TB_test a
     
    </select>
     
     
    <select id="getContents" resultType="hashmap">
     
        SELECT
            id, name
        FROM
            TB_test a
        WHERE
            id = 'admin'
     
    </select>
     
     
    <select id="getList" resultType="hashmap">
     
        SELECT
            id, name
        FROM
            TB_test a
     
    </select>
     
     
    <select id="getCountP" parameterType="hashmap" resultType="int">
     
        SELECT
            count(*)
        FROM
            TB_test a
        WHERE
            name = #{name}
     
    </select>
     
     
    <select id="getContentsP" parameterType="hashmap" resultType="hashmap">
     
        SELECT
            id, name
        FROM
            TB_test a
        WHERE
            id = #{name} and age = #{age}
     
    </select>
 
 
</mapper>


* myBatis¸¦ ÅëÇØ DB¸¦ ´Ù·ê¶§ »ç¿ëÇÒ ÆÄÀÏÀÌ´Ù. ½ÇÇàÇÏ°íÀÚ ÇÒ Äõ¸® ¸ðÀ½À̶ó°í »ý°¢Çϸé ÆíÇÏ´Ù.



6. ÃÖ»óÀ§¿¡ index.jsp ÆÄÀÏÀ» ¸¸µç´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<p><%@ 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{
         
        // ´ÜÀÏ °ª °¡Á®¿À±â
        int cnt = sqlSession.selectOne("Test.getCount") ;
         
         
         
        // ´ÜÀÏÇà °¡Á®¿À±â
        HashMap hm = sqlSession.selectOne("Test.getConents") ;
         
         
         
        // ¸®½ºÆ® °¡Á®¿À±â
        List list = sqlSession.selectList("Test.getList") ;
         
         
        // °¡Á®¿Â ¸®½ºÆ® »ç¿ëÇϱâ
        if(list.size() > 0){
            for(int i=0; i < list.size(); i++){
                HashMap rs = (HashMap)list.get(i) ;
                 
            }
        }
         
         
         
        // String parameter ³Ñ°Ü¼­ ´ÜÀÏ °ª °¡Á®¿À±â
        String name = "È«±æµ¿" ;
        int pCnt = sqlSession.selectOne("Test.getCountP", name) ;
         
         
        // HashMap parameter ³Ñ°Ü¼­ ´ÜÀÏ Çà °¡Á®¿À±â
        HashMap pHm = new HashMap();
        pHm.put("name", "È«±æµ¿") ;
        pHm.put("age", "28") ;
         
        HashMap pRs = sqlSession.selectOne("Test.getConentsP", pHm) ;
         
         
         
         
    }catch(Exception e){
        e.printStackTrace() ;
    }finally{
        sqlSession.close() ;
    }
     
     
     
%>
</p>






[°£·«¼³¸í]


SELECT (index.jsp)

 selectOne 

 ´ÜÀÏ Çà ¶Ç´Â ´ÜÀÏ °ªÀ» °¡Á®¿Ã¶§ »ç¿ë

 selectList

 ¸®½ºÆ®·Î µÇ¾î ÀÖ´Â Ç׸ñÀ» °¡Á®¿Ã¶§ »ç¿ë



Mapper (test.xml)

  namespace

 ¸ÅÆÛ À̸§ - sqlSession.selectOne("Test.getConentsP", pHm) ;

 id

 È£ÃâÇÒ ID À̸§ - sqlSession.selectOne("Test.getConentsP", pHm) ;

 resultType

 µ¥ÀÌÅÍ ¹Ýȯ Çü½Ä - int, string, hashmap 

 parameterType

 »ç¿ëÇÒ ÀÎÀÚ°ª Çü½Ä - int, string, hashmap 




Ãâó: http://fruitdev.tistory.com/29 [°úÀÏ°¡°Ô °³¹ßÀÚ]