7. 공용 클래스 작성 - commonDAo
WEB/게시판구현_springMvc2+mybatis2017. 4. 12. 03:19
공용으로 쓰일 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
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 > 게시판구현_springMvc2+mybatis' 카테고리의 다른 글
9. 사용자 생성 및 테이블 작성 (0) | 2017.04.12 |
---|---|
8. 공용 클래스 작성 Myutil.java (0) | 2017.04.12 |
6. 마이바티스 설정 mybatis-config, mybatis-context, mapper (0) | 2017.04.12 |
5. mvc-config.xml ,application-config 설정 (0) | 2017.04.12 |
4. web.xml 설정 (0) | 2017.04.12 |