现在的位置: 首页 > 综合 > 正文

spring中使用DBUtils

2018年05月07日 ⁄ 综合 ⁄ 共 8695字 ⁄ 字号 评论关闭
 <!-- 定义数据源Bean--> 
 2 <bean id="dataSource" 
 3     class="org.apache.commons.dbcp.BasicDataSource" 
 4     destroy-method="close"> 
 5     <property name="driverClassName" 
 6         value="${jdbc.driverClassName}" /> 
 7     <property name="url" value="${jdbc.url}" /> 
 8     <property name="username" value="${jdbc.username}" /> 
 9     <property name="password" value="${jdbc.password}" /> 
10 </bean> 
11 <!-- 下面定义了DbUtils的一个模板操作Bean --> 
12 <bean id="dbUtilsTemplate" class="com.company.project.dao.template.DbUtilsTemplate"> 
13         <property name="dataSource" ref="dataSource" /> 
14 </bean> 
复制代码
复制代码
  1 package com.company.project.dao.template; 
  2 
  3 import java.sql.SQLException; 
  4 import java.util.ArrayList; 
  5 import java.util.List; 
  6 import java.util.Map; 
  7 
  8 import javax.sql.DataSource; 
  9 
 10 import org.apache.commons.dbcp.BasicDataSource; 
 11 import org.apache.commons.dbutils.QueryRunner; 
 12 import org.apache.commons.dbutils.handlers.BeanHandler; 
 13 import org.apache.commons.dbutils.handlers.BeanListHandler; 
 14 import org.apache.commons.dbutils.handlers.MapHandler; 
 15 import org.apache.commons.dbutils.handlers.MapListHandler; 
 16 import org.apache.commons.dbutils.handlers.ScalarHandler; 
 17 import org.apache.commons.logging.Log; 
 18 import org.apache.commons.logging.LogFactory; 
 19 
 20 /** 
 21 * 调用Apache Commons DBUtil组件的数据库操作类 
 22 * 采用DBCP作为数据源,数据源在Spring中已经配置好 
 23 * 本类已经在Spring中配置好,在需要的地方,set注入后即可调用 
 24 * <code> 
 25 * private DbUtilsTemplate dbUtilsTemplate; 
 26 * public void setDbUtilsTemplate(DbUtilsTemplate dbUtilsTemplate) { 
 27 *     this.dbUtilsTemplate = dbUtilsTemplate; 
 28 * } 
 29 * </code> 
 30 * @author Sunshine 
 31 * @version 1.0 2009-07-29 
 32 */ 
 33 public class DbUtilsTemplate { 
 34 
 35     private DataSource dataSource; 
 36     private QueryRunner queryRunner; 
 37     private static final Log LOG = LogFactory.getLog(DbUtilsTemplate.class); 
 38 
 39     public void setDataSource(BasicDataSource dataSource) { 
 40         this.dataSource = dataSource; 
 41     } 
 42 
 43     /** 
 44      * 执行sql语句 
 45      * @param sql sql语句 
 46      * @return 受影响的行数 
 47      */ 
 48     public int update(String sql) { 
 49         return update(sql, null); 
 50     } 
 51       
 52     /** 
 53      * 执行sql语句 
 54      * <code> 
 55      * executeUpdate("update user set username = 'kitty' where username = ?", "hello kitty"); 
 56      * </code> 
 57      * @param sql sql语句 
 58      * @param param 参数 
 59      * @return 受影响的行数 
 60      */ 
 61     public int update(String sql, Object param) { 
 62         return update(sql, new Object[] { param }); 
 63     } 
 64       
 65     /** 
 66      * 执行sql语句 
 67      * @param sql sql语句 
 68      * @param params 参数数组 
 69      * @return 受影响的行数 
 70      */ 
 71     public int update(String sql, Object[] params) { 
 72         queryRunner = new QueryRunner(dataSource); 
 73         int affectedRows = 0; 
 74         try { 
 75             if (params == null) { 
 76                 affectedRows = queryRunner.update(sql); 
 77             } else { 
 78                 affectedRows = queryRunner.update(sql, params); 
 79             } 
 80         } catch (SQLException e) { 
 81             LOG.error("Error occured while attempting to update data", e); 
 82         } 
 83         return affectedRows; 
 84     } 
 85       
 86     /** 
 87      * 执行批量sql语句 
 88      * @param sql sql语句 
 89      * @param params 二维参数数组 
 90      * @return 受影响的行数的数组 
 91      */ 
 92     public int[] batchUpdate(String sql, Object[][] params) { 
 93         queryRunner = new QueryRunner(dataSource); 
 94         int[] affectedRows = new int[0]; 
 95         try { 
 96             affectedRows = queryRunner.batch(sql, params); 
 97         } catch (SQLException e) { 
 98             LOG.error("Error occured while attempting to batch update data", e); 
 99         } 
100         return affectedRows; 
101     }     
102 
103     /** 
104      * 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中 
105      * @param sql sql语句 
106      * @return 查询结果 
107      */ 
108     public List<Map<String, Object>> find(String sql) { 
109         return find(sql, null); 
110     } 
111       
112     /** 
113      * 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中 
114      * @param sql sql语句 
115      * @param param 参数 
116      * @return 查询结果 
117      */ 
118     public List<Map<String, Object>> find(String sql, Object param) { 
119         return find(sql, new Object[] {param}); 
120     } 
121       
122     /** 
123      * 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中 
124      * @param sql sql语句 
125      * @param params 参数数组 
126      * @return 查询结果 
127      */ 
128     @SuppressWarnings("unchecked") 
129     public List<Map<String, Object>> find(String sql, Object[] params) { 
130         queryRunner = new QueryRunner(dataSource); 
131         List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); 
132         try { 
133             if (params == null) { 
134                 list = (List<Map<String, Object>>) queryRunner.query(sql, new MapListHandler()); 
135             } else { 
136                 list = (List<Map<String, Object>>) queryRunner.query(sql, new MapListHandler(), params); 
137             } 
138         } catch (SQLException e) { 
139             LOG.error("Error occured while attempting to query data", e); 
140         } 
141         return list; 
142     } 
143       
144     /** 
145      * 执行查询,将每行的结果保存到Bean中,然后将所有Bean保存到List中 
146      * @param entityClass 类名 
147      * @param sql sql语句 
148      * @return 查询结果 
149      */ 
150     public <T> List<T> find(Class<T> entityClass, String sql) { 
151         return find(entityClass, sql, null); 
152     } 
153       
154     /** 
155      * 执行查询,将每行的结果保存到Bean中,然后将所有Bean保存到List中 
156      * @param entityClass 类名 
157      * @param sql sql语句 
158      * @param param 参数 
159      * @return 查询结果 
160      */ 
161     public <T> List<T> find(Class<T> entityClass, String sql, Object param) { 
162         return find(entityClass, sql, new Object[] { param }); 
163     } 
164       
165     /** 
166      * 执行查询,将每行的结果保存到Bean中,然后将所有Bean保存到List中 
167      * @param entityClass 类名 
168      * @param sql sql语句 
169      * @param params 参数数组 
170      * @return 查询结果 
171      */ 
172     @SuppressWarnings("unchecked") 
173     public <T> List<T> find(Class<T> entityClass, String sql, Object[] params) { 
174         queryRunner = new QueryRunner(dataSource); 
175         List<T> list = new ArrayList<T>(); 
176         try { 
177             if (params == null) { 
178                 list = (List<T>) queryRunner.query(sql, new BeanListHandler(entityClass)); 
179             } else { 
180                 list = (List<T>) queryRunner.query(sql, new BeanListHandler(entityClass), params); 
181             } 
182         } catch (SQLException e) { 
183             LOG.error("Error occured while attempting to query data", e); 
184         } 
185         return list; 
186     } 
187       
188     /** 
189      * 查询出结果集中的第一条记录,并封装成对象 
190      * @param entityClass 类名 
191      * @param sql sql语句 
192      * @return 对象 
193      */ 
194     public <T> T findFirst(Class<T> entityClass, String sql) { 
195         return findFirst(entityClass, sql, null); 
196     } 
197       
198     /** 
199      * 查询出结果集中的第一条记录,并封装成对象 
200      * @param entityClass 类名 
201      * @param sql sql语句 
202      * @param param 参数 
203      * @return 对象 
204      */ 
205     public <T> T findFirst(Class<T> entityClass, String sql, Object param) { 
206         return findFirst(entityClass, sql, new Object[] { param }); 
207     } 
208       
209     /** 
210      * 查询出结果集中的第一条记录,并封装成对象 
211      * @param entityClass 类名 
212      * @param sql sql语句 
213      * @param params 参数数组 
214      * @return 对象 
215      */ 
216     @SuppressWarnings("unchecked") 
217     public <T> T findFirst(Class<T> entityClass, String sql, Object[] params) { 
218         queryRunner = new QueryRunner(dataSource); 
219         Object object = null; 
220         try { 
221             if (params == null) { 
222                 object = queryRunner.query(sql, new BeanHandler(entityClass)); 
223             } else { 
224                 object = queryRunner.query(sql, new BeanHandler(entityClass), params); 
225             } 
226         } catch (SQLException e) { 
227             LOG.error("Error occured while attempting to query data", e); 
228         } 
229         return (T) object; 
230     } 
231       
232     /** 
233      * 查询出结果集中的第一条记录,并封装成Map对象 
234      * @param sql sql语句 
235      * @return 封装为Map的对象 
236      */ 
237     public Map<String, Object> findFirst(String sql) { 
238         return findFirst(sql, null); 
239     } 
240       
241     /** 
242      * 查询出结果集中的第一条记录,并封装成Map对象 
243      * @param sql sql语句 
244      * @param param 参数 
245      * @return 封装为Map的对象 
246      */ 
247     public Map<String, Object> findFirst(String sql, Object param) { 
248         return findFirst(sql, new Object[] { param }); 
249     } 
250       
251     /** 
252      * 查询出结果集中的第一条记录,并封装成Map对象 
253      * @param sql sql语句 
254      * @param params 参数数组 
255      * @return 封装为Map的对象 
256      */ 
257     @SuppressWarnings("unchecked") 
258     public Map<String, Object> findFirst(String sql, Object[] params) { 
259         queryRunner = new QueryRunner(dataSource); 
260         Map<String, Object> map = null; 
261         try { 
262             if (params == null) { 
263                 map = (Map<String, Object>) queryRunner.query(sql, new MapHandler()); 
264             } else { 
265                 map = (Map<String, Object>) queryRunner.query(sql, new MapHandler(), params); 
266             } 
267         } catch (SQLException e) { 
268             LOG.error("Error occured while attempting to query data", e); 
269         } 
270         return map; 
271     } 
272       
273     /** 
274      * 查询某一条记录,并将指定列的数据转换为Object 
275      * @param sql sql语句 
276      * @param columnName 列名 
277      * @return 结果对象 
278      */ 
279     public Object findBy(String sql, String columnName) { 
280         return findBy(sql, columnName, null); 
281     } 
282       
283     /** 
284      * 查询某一条记录,并将指定列的数据转换为Object 
285      * @param sql sql语句 
286      * @param columnName 列名 
287      * @param param 参数 
288      * @return 结果对象 
289      */ 
290     public Object findBy(String sql, String columnName, Object param) { 
291         return findBy(sql, columnName, new Object[] { param }); 
292     } 
293       
294     /** 
295      * 查询某一条记录,并将指定列的数据转换为Object 
296      * @param sql sql语句 
297      * @param columnName 列名 
298      * @param params 参数数组 
299      * @return 结果对象 
300      */ 
301     public Object findBy(String sql, String columnName, Object[] params) { 
302         queryRunner = new QueryRunner(dataSource); 
303         Object object = null; 
304         try { 
305             if (params == null) { 
306                 object = queryRunner.query(sql, new ScalarHandler(columnName)); 
307             } else { 
308                 object = queryRunner.query(sql, new ScalarHandler(columnName), params); 
309             } 
310         } catch (SQLException e) { 
311             LOG.error("Error occured while attempting to query data", e); 
312         } 
313         return object; 
314     } 
315       
316     /** 
317      * 查询某一条记录,并将指定列的数据转换为Object 
318      * @param sql sql语句 
319      * @param columnIndex 列索引 
320      * @return 结果对象 
321      */ 
322     public Object findBy(String sql, int columnIndex) { 
323         return findBy(sql, columnIndex, null); 
324     } 
325       
326     /** 
327      * 查询某一条记录,并将指定列的数据转换为Object 
328      * @param sql sql语句 
329      * @param columnIndex 列索引 
330      * @param param 参数 
331      * @return 结果对象 
332      */ 
333     public Object findBy(String sql, int columnIndex, Object param) { 
334         return findBy(sql, columnIndex, new Object[] { param }); 
335     } 
336       
337     /** 
338      * 查询某一条记录,并将指定列的数据转换为Object 
339      * @param sql sql语句 
340      * @param columnIndex 列索引 
341      * @param params 参数数组 
342      * @return 结果对象 
343      */ 
344     public Object findBy(String sql, int columnIndex, Object[] params) { 
345         queryRunner = new QueryRunner(dataSource); 
346         Object object = null; 
347         try { 
348             if (params == null) { 
349                 object = queryRunner.query(sql, new ScalarHandler(columnIndex)); 
350             } else { 
351                 object = queryRunner.query(sql, new ScalarHandler(columnIndex), params); 
352             } 
353         } catch (SQLException e) { 
354             LOG.error("Error occured while attempting to query data", e); 
355         } 
356         return object; 
357     } 
358 }

抱歉!评论已关闭.