IT_Note



myutil.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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.sp.common;
 
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.springframework.stereotype.Service;
 
@Service("myUtil")
public class MyUtil {
    //********************************************
    // 총페이지 수 구하기
    public int pageCount(int numPerPage, int dataCount) {
        int pageCount=0;
        
        if(dataCount > 0) {
            if(dataCount % numPerPage == 0)
                pageCount=dataCount/numPerPage;
            else
                pageCount=dataCount/numPerPage+1;
        }
        
        return pageCount;
    }
    
    //********************************************
    // 페이징(paging) 처리(GET 방식)
    public String paging(int current_page, int total_page, String list_url) {
        StringBuffer sb=new StringBuffer();
        
        int numPerBlock=10;
        int currentPageSetup;
        int n, page;
        
        if(current_page<1 || total_page < 1)
            return "";
        
        if(list_url.indexOf("?")!=-1)
            list_url+="&";
        else
            list_url+="?";
        
        // currentPageSetup : 표시할첫페이지-1
        currentPageSetup=(current_page/numPerBlock)*numPerBlock;
        if(current_page%numPerBlock==0)
            currentPageSetup=currentPageSetup-numPerBlock;
 
        sb.append("<style type='text/css'>");
        sb.append("#paginate {clear:both;font:12px 돋움,Dotum,굴림,Gulim,AppleGothic;padding:15px 0px 0px 0px;text-align:center;height:28px;white-space:nowrap;}");
        sb.append("#paginate a {border:1px solid #ccc;height:28px;color:#000000;text-decoration:none;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;outline:none; select-dummy: expression(this.hideFocus=true);}");
        sb.append("#paginate a:hover, a:active {border:1px solid #ccc;color:#6771ff;vertical-align:middle; line-height:normal;}");
        sb.append("#paginate .curBox {border:1px solid #e28d8d; background: #fff; color:#cb3536; font-weight:bold;height:28px;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;}");
        sb.append("#paginate .numBox {border:1px solid #ccc;height:28px;font-weight:bold;text-decoration:none;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;}");
        sb.append("</style>");
        
        sb.append("<div id='paginate'>");
        // 처음페이지, 이전(10페이지 전)
        n=current_page-numPerBlock;
        if(total_page > numPerBlock && currentPageSetup > 0) {
            sb.append("<a href='"+list_url+"page=1'>처음</a>");
            sb.append("<a href='"+list_url+"page="+n+"'>이전</a>");
        }
        
        // 바로가기
        page=currentPageSetup+1;
        while(page<=total_page && page <=(currentPageSetup+numPerBlock)) {
            if(page==current_page) {
                sb.append("<span class='curBox'>"+page+"</span>");
            } else {
                sb.append("<a href='"+list_url+"page="+page+"' class='numBox'>"+page+"</a>");
            }
            page++;
        }
        
        // 다음(10페이지 후), 마지막페이지
        n=current_page+numPerBlock;
        if(n>total_page) n=total_page;
        if(total_page-currentPageSetup>numPerBlock) {
            sb.append("<a href='"+list_url+"page="+n+"'>다음</a>");
            sb.append("<a href='"+list_url+"page="+total_page+"'>끝</a>");
        }
        sb.append("</div>");
    
        return sb.toString();
    }
 
    //********************************************
    // javascript 페이지 처리(javascript listPage() 함수 호출)
    public String paging(int current_page, int total_page) {
        if(current_page < 1 || total_page < 1)
            return "";
 
        int numPerBlock = 10;   // 리스트에 나타낼 페이지 수
        int currentPageSetUp;
        int n;
        int page;
        StringBuffer sb=new StringBuffer();
        
        // 표시할 첫 페이지
        currentPageSetUp = (current_page / numPerBlock) * numPerBlock;
        if (current_page % numPerBlock == 0)
            currentPageSetUp = currentPageSetUp - numPerBlock;
 
        sb.append("<style type='text/css'>");
        sb.append("#paginate {clear:both;font:12px 돋움,Dotum,굴림,Gulim,AppleGothic;padding:15px 0px 0px 0px;text-align:center;height:28px;white-space:nowrap;}");
        sb.append("#paginate a {border:1px solid #ccc;height:28px;color:#000000;text-decoration:none;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;outline:none; select-dummy: expression(this.hideFocus=true);}");
        sb.append("#paginate a:hover, a:active {border:1px solid #ccc;color:#6771ff;vertical-align:middle; line-height:normal;}");
        sb.append("#paginate .curBox {border:1px solid #e28d8d; background: #fff; color:#cb3536; font-weight:bold;height:28px;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;}");
        sb.append("#paginate .numBox {border:1px solid #ccc;height:28px;font-weight:bold;text-decoration:none;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;}");
        sb.append("</style>");
        
        sb.append("<div id='paginate'>");
        
        // 처음페이지, 이전(10페이지 전)
        n = current_page - numPerBlock;
        if ((total_page > numPerBlock) && (currentPageSetUp > 0)) {
            sb.append("<a onclick='listPage(1);'>처음</a>");
            sb.append("<a onclick='listPage("+n+");'>이전</a>");
        }
 
        // 바로가기 페이지 구현
        page = currentPageSetUp + 1;
        while((page <= total_page) && (page <= currentPageSetUp + numPerBlock)) {
           if(page == current_page) {
               sb.append("<span class='curBox'>"+page+"</span>");
           } else {
               sb.append("<a onclick='listPage("+page+");' class='numBox'>"+page+"</a>");
           }
           page++;
        }
 
        // 다음(10페이지 후), 마지막 페이지
        n = current_page + numPerBlock;
        if(n>total_page) n=total_page;
        if (total_page - currentPageSetUp > numPerBlock) {
            sb.append("<a onclick='listPage("+n+");'>다음</a>");
            sb.append("<a onclick='listPage("+total_page+");'>끝</a>");
        }
        sb.append("</div>");
 
        return sb.toString();
    }
 
    //********************************************
    // javascript 페이지 처리(javascript 지정 함수 호출, methodName:호출할 스크립트 함수명)
    public String pagingMethod(int current_page, int total_page, String methodName) {
        if(current_page < 1 || total_page < 1)
            return "";
 
        int numPerBlock = 10;   // 리스트에 나타낼 페이지 수
        int currentPageSetUp;
        int n;
        int page;
        StringBuffer sb=new StringBuffer();
        
        // 표시할 첫 페이지
        currentPageSetUp = (current_page / numPerBlock) * numPerBlock;
        if (current_page % numPerBlock == 0)
            currentPageSetUp = currentPageSetUp - numPerBlock;
 
        sb.append("<style type='text/css'>");
        sb.append("#paginate {clear:both;font:12px 돋움,Dotum,굴림,Gulim,AppleGothic;padding:15px 0px 0px 0px;text-align:center;height:28px;white-space:nowrap;}");
        sb.append("#paginate a {border:1px solid #ccc;height:28px;color:#000000;text-decoration:none;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;outline:none; select-dummy: expression(this.hideFocus=true);}");
        sb.append("#paginate a:hover, a:active {border:1px solid #ccc;color:#6771ff;vertical-align:middle; line-height:normal;}");
        sb.append("#paginate .curBox {border:1px solid #e28d8d; background: #fff; color:#cb3536; font-weight:bold;height:28px;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;}");
        sb.append("#paginate .numBox {border:1px solid #ccc;height:28px;font-weight:bold;text-decoration:none;padding:4px 7px 4px 7px;margin-left:3px;line-height:normal;vertical-align:middle;}");
        sb.append("</style>");
        
        sb.append("<div id='paginate'>");
        
        // 처음페이지, 이전(10페이지 전)
        n = current_page - numPerBlock;
        if ((total_page > numPerBlock) && (currentPageSetUp > 0)) {
            sb.append("<a onclick='"+methodName+"(1);'>처음</a>");
            sb.append("<a onclick='"+methodName+"("+n+");'>이전</a>");
        }
 
        // 바로가기 페이지 구현
        page = currentPageSetUp + 1;
        while((page <= total_page) && (page <= currentPageSetUp + numPerBlock)) {
           if(page == current_page) {
               sb.append("<span class='curBox'>"+page+"</span>");
           } else {
               sb.append("<a onclick='"+methodName+"("+page+");' class='numBox'>"+page+"</a>");
           }
           page++;
        }
 
        // 다음(10페이지 후), 마지막 페이지
        n = current_page + numPerBlock;
        if(n>total_page) n=total_page;
        if (total_page - currentPageSetUp > numPerBlock) {
            sb.append("<a onclick='"+methodName+"("+n+");'>다음</a>");
            sb.append("<a onclick='"+methodName+"("+total_page+");'>끝</a>");
        }
        sb.append("</div>");
 
        return sb.toString();
    }
 
    //********************************************
    // HTML 태그 제거
    public String removeHtmlTag(String str) {
        if(str==null||str.length()==0)
            return "";
 
        String regex="<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>";
        String result=str.replaceAll(regex, "");
        return result;
    }
 
    //********************************************
    // HTML 문서의 img 태그 src 속성값 추출 
    public List<String> getImgSrc(String html) {
        List<String> result = new ArrayList<String>();
        
        if(html==null||html.length()==0)
            return result;
 
        String regex="<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>";
        Pattern nonValidPattern = Pattern.compile(regex);
 
        Matcher matcher = nonValidPattern.matcher(html);
        while (matcher.find()) {
            result.add(matcher.group(1));
        }
        return result;
    }
    
    //********************************************
    // 특수문자를 HTML 문자로 변경
    public String escape(String str) {
        if(str==null||str.length()==0)
            return "";
        
        StringBuilder builder = new StringBuilder((int)(str.length() * 1.2f));
 
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            switch (c) {
            case '<':
                builder.append("&lt;");
                break;
            case '>':
                builder.append("&gt;");
                break;
            case '&':
                builder.append("&amp;");
                break;
            case '\"':
                builder.append("&quot;");
                break;
            default:
                builder.append(c);
            }
        }
        return builder.toString();
    }
 
    //********************************************
    // 특수문자를 HTML 문자로 변경 및 엔터를 <br>로 변경 
     public String htmlSymbols(String str) {
        if(str==null||str.length()==0)
            return "";
 
         str=str.replaceAll("&""&amp;");
         str=str.replaceAll("\"""&quot;");
         str=str.replaceAll(">""&gt;");
         str=str.replaceAll("<""&lt;");
         
         str=str.replaceAll(" ""&nbsp;");
         str=str.replaceAll("\n""<br>");
         
         return str;
     }
 
    //********************************************
     // 문자열의 내용중 원하는 문자열을 다른 문자열로 변환
     // String str = replaceAll(str, "\n", "<br>"); // 엔터를 <br>로 변환
     public String replaceAll(String str, String oldStr, String newStr) throws Exception {
         if(str == null)
             return "";
 
         Pattern p = Pattern.compile(oldStr);
         
         // 입력 문자열과 함께 매쳐 클래스 생성
         Matcher m = p.matcher(str);
 
         StringBuffer sb = new StringBuffer();
         // 패턴과 일치하는 문자열을 newStr 로 교체해가며 새로운 문자열을 만든다.
         while(m.find()) {
             m.appendReplacement(sb, newStr);
         }
 
         // 나머지 부분을 새로운 문자열 끝에 덫 붙인다.
         m.appendTail(sb);
 
         return sb.toString();
     }
 
    //********************************************
     // E-Mail 검사
     public boolean isValidEmail(String email) {
         if (email==nullreturn false;
         boolean b = Pattern.matches(
             "[\\w\\~\\-\\.]+@[\\w\\~\\-]+(\\.[\\w\\~\\-]+)+"
             email.trim());
         return b;
     }
 
    //********************************************
     // NULL 인 경우 ""로 
     public String checkNull(String str) {
         String strTmp;
         if (str == null)
             strTmp = "";
         else
             strTmp = str;
         return strTmp;
     }
}
 




공용으로 쓰일 dao클래스를 작성한다


CommonDAO.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
package com.sp.common.dao;
 
import java.util.List;
import java.util.Map;
 
public interface CommonDAO {
    public int insertData(String id, Object value) throws Exception;
    
    public int updateData(String id, Object pData) throws Exception;
    public int updateData(String id, Map<String, Object> map) throws Exception;
    
    public int deleteData(String id, Map<String, Object> map) throws Exception;
    public int deleteData(String id, Object value) throws Exception;
    public int deleteAll(String id) throws Exception;
    
    public int getIntValue(String id, Map<String, Object> map) throws Exception;
    public int getIntValue(String id, Object value) throws Exception;
    public int getIntValue(String id) throws Exception;
    
    public <T> List<T> getListData(String id, Map<String, Object> map) throws Exception;
    public <T> List<T> getListData(String id, Object value) throws Exception;
    public <T> List<T> getListData(String id) throws Exception;
    
    public <T> T getReadData(String id) throws Exception;
    public <T> T getReadData(String id, Object value) throws Exception;
    public <T> T getReadData(String id, Map<String, Object> map) throws Exception;
    
    // INSERT, UPDATE, DELETE 프로시져(IN)
    public void callUpdateProcedure(String id, Object value) throws Exception;
    
    // SELECT(OUT)
    public <T> Map<String, T> callSelectOneProcedureMap(String id, Map<String, T> map) throws Exception;
    public <T> Map<String, T> callSelectListProcedureMap(String id, Map<String, T> map) throws Exception;
}
 

update delete 등 공용으로 자주 쓰이게 될 클래될 클래스


MybatisDaoImpl.java 


Colored By Color Scripter

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.sp.common.dao;
 
import java.util.List;
import java.util.Map;
 
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
@Repository("dao")
public class MyBatisDaoImpl implements CommonDAO {
    @Autowired
    private SqlSession sqlSession;
    
/*    
    public void setSqlSessionTemplate(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
*/
    
    // ******************************************************************
    // 데이터 추가
    public int insertData(String id, Object value) throws Exception {
        int result = 0;
 
        try {
            result = sqlSession.insert(id, value);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        } finally {
        }
        return result;
    }
    
    // ******************************************************************
    // 데이터 수정
    public int updateData(String id, Object value) throws Exception {
        int result = 0;
        
        try {
               result = sqlSession.update(id, value);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }
        
        return result;
    }
    public int updateData(String id, Map<String, Object> map) throws Exception {
        int result = 0;
        
        try {
               result = sqlSession.update(id, map);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }
        
        return result;
    }
    
    // ******************************************************************
    // 데이터 삭제
    public int deleteData(String id, Map<String, Object> map) throws Exception {
        int result = 0;
 
        try {
            result = sqlSession.delete(id, map);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }
 
        return result;
    }
    public int deleteData(String id, Object value) throws Exception {
        int result = 0;
        
        try {
            result = sqlSession.delete(id, value);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }
 
        return result;
    }
    public int deleteAll(String id) throws Exception {
        int result = 0;
        
        try {
            result = sqlSession.delete(id);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }
 
        return result;
    }
 
    // ******************************************************************
    // 레 코드 수 / 최대 값 구하기
    public int getIntValue(String id, Map<String, Object> map) throws Exception {
        int num = 0;
 
        try {
            num = ((Integer)sqlSession.selectOne(id, map)).intValue();
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
 
        return num;
    }
    public int getIntValue(String id, Object value) throws Exception {
        int num = 0;
        
        try {
            num = ((Integer)sqlSession.selectOne(id, value)).intValue();
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
        
        return num;
    }
    public int getIntValue(String id) throws Exception {
        int num = 0;
        try {
            num = ((Integer)sqlSession.selectOne(id)).intValue();
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
        return num;
    }
    
    // ******************************************************************
    // 테이블의 하나 이상의 레코드를 SELECT
    public <T> List<T> getListData(String id, Map<String, Object> map) throws Exception {
        List<T> list = null;
        try {
            list = sqlSession.selectList(id, map);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
        return list;
    }
    public <T> List<T> getListData(String id, Object value) throws Exception {
        List<T> list = null;
        try {
            list = sqlSession.selectList(id, value);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
        return list;
    
    }
    public <T> List<T> getListData(String id) throws Exception {
        List<T> list = null;
        try {
            list = sqlSession.selectList(id);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
        return list;
    }
    
    // ******************************************************************
    // 테이블의 하나의 레코드를 SELECT
    public <T> T getReadData(String id) throws Exception {
        try {
            return  sqlSession.selectOne(id);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
    }
    public <T> T getReadData(String id, Object value) throws Exception {
        try {
            return  sqlSession.selectOne(id, value);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
    }
    public <T> T getReadData(String id, Map<String, Object> map) throws Exception {
        try {
            return  sqlSession.selectOne(id, map);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }        
    }
    
    // ******************************************************************
    // 프로시져
    // INSERT, UPDATE, DELETE
    @Override
    public void callUpdateProcedure(String id, Object value) throws Exception{
        try {
            sqlSession.update(id, value);
        } catch (Exception e) {
            System.out.println(e.toString());
            
            throw e;
        }
    }
 
    // SELECT : OUT 파라미터가 SYS_REFCURSOR 이외의 하나의 레코드(INTEGER 등)
    @Override
    public <T> Map<String, T> callSelectOneProcedureMap(String id, Map<String, T> map) throws Exception{
        try {
            // select procedure 결과는 map로 리턴한다.
            sqlSession.selectOne(id, map);
            return map;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        
        return map;
    }
    
    // SELECT : OUT 파라미터가 SYS_REFCURSOR(하나 또는 하나 이상의 레코드)
    public <T> Map<String, T> callSelectListProcedureMap(String id, Map<String, T> map) throws Exception{
        try {
            // select procedure 결과는 map로 리턴한다.
            sqlSession.selectList(id, map);
            return map;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        
        return map;
    }
}
 



WEB-INF/mybatis/jdbc.properties   

jdbc오라클 환경 설정 파일. txt파일.

##jdbc.driverClass=oracle.jdbc.driver.OracleDriver
##jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.driverClass=net.sf.log4jdbc.DriverSpy
jdbc.url=jdbc:log4jdbc:oracle:thin:@127.0.0.1:1521:xe
jdbc.username=board1
jdbc.password=java$!


mybatis-config.xml    - mybatis환경설정 xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "HTTP://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>

<settings>

<setting name="cacheEnabled" value="false" />

<setting name="useGeneratedKeys" value="true" />

<setting name="defaultExecutorType" value="REUSE" />

</settings>

<!-- 별명 -->

    <typeAliases>

         <typeAlias alias="hashMap" type="java.util.HashMap" />

         <typeAlias alias="map" type="java.util.Map" />

    </typeAliases>

 

<!--

<mappers>

<mapper resource="mybatis/mapper/tempMapper.xml" />

</mappers>

-->


<mappers>

<mapper resource="mybatis/mapper/bbsMapper.xml" />

</mappers>

</configuration>




mybatis-context.xml  -  mybatis 객체 생성을 위한 xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>/WEB-INF/mybatis/jdbc.properties</value>

</property>

</bean>


    <!-- DBCP를 위한 DataSource 객체 -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${jdbc.driverClass}"/>

<property name="url" value="${jdbc.url}"/>

<property name="username" value="${jdbc.username}"/>

<property name="password" value="${jdbc.password}"/>

  <property name="maxActive" value="10" />

  <property name="maxIdle" value="3" />

  <property name="maxWait" value="10000" />

  <property name="defaultAutoCommit" value="true" />   <!--  기본:true  -->

</bean>

<!--  SqlSessionFactoryBean : SqlSessionTemplate 객체를 얻는다. -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" lazy-init="true">

<property name="dataSource" ref="dataSource" />

<property name="configLocation" value="/WEB-INF/mybatis/mybatis-config.xml"/>

         <property name="mapperLocations" value="/WEB-INF/mybatis/mapper/*.xml" />

<!-- 

<property name="mapperLocations" value="classpath:com/think/mapper/**/*.xml" />

-->

         <!-- 

         <property name="transactionFactory">

                <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />

          </property>

           -->

</bean>


<!-- SqlSessionTemplate : MyBatis SQL method 호출 및 관리, 예외처리 담당 -->

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

           <constructor-arg index="0" ref="sqlSessionFactory" />

    </bean>


    <!-- 트랜잭션관리자. -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource" />

    </bean>


<!-- 트랜잭션과 관련된 작업(트랜잭션시작, 커및, 롤백등) -->

    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">

         <property name="transactionManager" ref="transactionManager" />

    </bean>

     

</beans>



bbsMapper.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="bbs">


</mapper>