阅读更多
使用commons-dbutils-1.3.jar,请自行下载.
关于Connection对象和事务在业务层作了处理.
主要代码如下:
package keel.util;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.log4j.Logger;
/**
* 操作数据库方法
* @author 蔡治平
*
*/
public class SqlUtil {
private static Logger logger = Logger.getLogger(SqlUtil.class);
private static QueryRunner qr = new QueryRunner();
/**
* 查询
* @param conn
* @param sql
* @param params
* @return List
测试代码片段:
List> list = SqlUtil.search(conn, sql, new Object[]{"1001","admin"});
for(int i=0;i userList = SqlUtil.search(conn, User.class, sql, new Object[]{"1001","admin"});
for(int i=0;i map = SqlUtil.getById(conn, sql2, null);
System.out.println(map.get("id"));