博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spirng注解和dbutils集成
阅读量:4112 次
发布时间:2019-05-25

本文共 8732 字,大约阅读时间需要 29 分钟。

背景:

操作关系型数据库的框架很多,其实最好的是jdbc不过开发量稍高那么有没有其他的相对合适的框架呢?此处选择了dbutils。那么就需要spring和dbutils集成,对事务要求不高,基于spring注解集成

环境:

org.springframework
spring-core
3.2.0.RELEASE
org.springframework
spring-context
3.2.0.RELEASE
org.springframework
spring-beans
3.2.0.RELEASE
commons-dbutils
commons-dbutils
1.5
commons-pool
commons-pool
1.6
commons-dbcp
commons-dbcp
20030825.184428
mysql
mysql-connector-java
5.1.28

实现:

1.spring-source.xml:

2.DBUtilsTemplate

@Service("dbUtilsTemplate")public class DBUtilsTemplate {	@Autowired	private DataSource dataSource;	private QueryRunner queryRunner;	private static final Log LOG = LogFactory.getLog(DBUtilsTemplate.class);	public void setDataSource(BasicDataSource dataSource) {		this.dataSource = dataSource;	}	/**	 * 执行sql语句	 * 	 * @param sql	 *            sql语句	 * @param params	 *            参数数组	 * @return 受影响的行数	 */	public int update(String sql, Object[] params) {		queryRunner = new QueryRunner(dataSource);		int affectedRows = 0;		try {			if (params == null) {				affectedRows = queryRunner.update(sql);			} else {				affectedRows = queryRunner.update(sql, params);			}		} catch (SQLException e) {			LOG.error("Error occured while attempting to update data", e);		}		return affectedRows;	}	/**	 * 执行批量sql语句	 * 	 * @param sql	 *            sql语句	 * @param params	 *            二维参数数组	 * @return 受影响的行数的数组	 */	public int[] batchUpdate(String sql, Object[][] params) {		queryRunner = new QueryRunner(dataSource);		int[] affectedRows = new int[0];		try {			affectedRows = queryRunner.batch(sql, params);		} catch (SQLException e) {			LOG.error("Error occured while attempting to batch update data", e);		}		return affectedRows;	}	/**	 * 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中	 * 	 * @param sql	 *            sql语句	 * @return 查询结果	 */	public List
> find(String sql) { return find(sql, null); } /** * 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中 * * @param sql * sql语句 * @param param * 参数 * @return 查询结果 */ public List
> find(String sql, Object param) { return find(sql, new Object[] { param }); } /** * 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中 * * @param sql * sql语句 * @param params * 参数数组 * @return 查询结果 */ public List
> find(String sql, Object[] params) { queryRunner = new QueryRunner(dataSource); List
> list = new ArrayList
>(); try { if (params == null) { list = (List
>) queryRunner.query(sql, new MapListHandler()); } else { list = (List
>) queryRunner.query(sql, new MapListHandler(), params); } } catch (SQLException e) { LOG.error("Error occured while attempting to query data", e); } return list; } /** * 执行查询,将每行的结果保存到Bean中,然后将所有Bean保存到List中 * * @param entityClass * 类名 * @param sql * sql语句 * @return 查询结果 */ public
List
find(Class
entityClass, String sql) { return find(entityClass, sql, null); } /** * 执行查询,将每行的结果保存到Bean中,然后将所有Bean保存到List中 * * @param entityClass * 类名 * @param sql * sql语句 * @param param * 参数 * @return 查询结果 */ public
List
find(Class
entityClass, String sql, Object param) { return find(entityClass, sql, new Object[] { param }); } /** * 执行查询,将每行的结果保存到Bean中,然后将所有Bean保存到List中 * * @param entityClass * 类名 * @param sql * sql语句 * @param params * 参数数组 * @return 查询结果 */ @SuppressWarnings("unchecked") public
List
find(Class
entityClass, String sql, Object[] params) { queryRunner = new QueryRunner(dataSource); List
list = new ArrayList
(); try { if (params == null) { list = (List
) queryRunner.query(sql, new BeanListHandler( entityClass)); } else { list = (List
) queryRunner.query(sql, new BeanListHandler( entityClass), params); } } catch (SQLException e) { LOG.error("Error occured while attempting to query data", e); } return list; } /** * 查询出结果集中的第一条记录,并封装成对象 * * @param entityClass * 类名 * @param sql * sql语句 * @return 对象 */ public
T findFirst(Class
entityClass, String sql) { return findFirst(entityClass, sql, null); } /** * 查询出结果集中的第一条记录,并封装成对象 * * @param entityClass * 类名 * @param sql * sql语句 * @param param * 参数 * @return 对象 */ public
T findFirst(Class
entityClass, String sql, Object param) { return findFirst(entityClass, sql, new Object[] { param }); } /** * 查询出结果集中的第一条记录,并封装成对象 * * @param entityClass * 类名 * @param sql * sql语句 * @param params * 参数数组 * @return 对象 */ @SuppressWarnings("unchecked") public
T findFirst(Class
entityClass, String sql, Object[] params) { queryRunner = new QueryRunner(dataSource); Object object = null; try { if (params == null) { object = queryRunner.query(sql, new BeanHandler(entityClass)); } else { object = queryRunner.query(sql, new BeanHandler(entityClass), params); } } catch (SQLException e) { LOG.error("Error occured while attempting to query data", e); } return (T) object; } /** * 查询出结果集中的第一条记录,并封装成Map对象 * * @param sql * sql语句 * @return 封装为Map的对象 */ public Map
findFirst(String sql) { return findFirst(sql, null); } /** * 查询出结果集中的第一条记录,并封装成Map对象 * * @param sql * sql语句 * @param param * 参数 * @return 封装为Map的对象 */ public Map
findFirst(String sql, Object param) { return findFirst(sql, new Object[] { param }); } /** * 查询出结果集中的第一条记录,并封装成Map对象 * * @param sql * sql语句 * @param params * 参数数组 * @return 封装为Map的对象 */ public Map
findFirst(String sql, Object[] params) { queryRunner = new QueryRunner(dataSource); Map
map = null; try { if (params == null) { map = (Map
) queryRunner.query(sql, new MapHandler()); } else { map = (Map
) queryRunner.query(sql, new MapHandler(), params); } } catch (SQLException e) { LOG.error("Error occured while attempting to query data", e); } return map; } /** * 查询某一条记录,并将指定列的数据转换为Object * * @param sql * sql语句 * @param columnName * 列名 * @return 结果对象 */ public Object findBy(String sql, String columnName) { return findBy(sql, columnName, null); } /** * 查询某一条记录,并将指定列的数据转换为Object * * @param sql * sql语句 * @param columnName * 列名 * @param param * 参数 * @return 结果对象 */ public Object findBy(String sql, String columnName, Object param) { return findBy(sql, columnName, new Object[] { param }); } /** * 查询某一条记录,并将指定列的数据转换为Object * * @param sql * sql语句 * @param columnName * 列名 * @param params * 参数数组 * @return 结果对象 */ public Object findBy(String sql, String columnName, Object[] params) { queryRunner = new QueryRunner(dataSource); Object object = null; try { if (params == null) { object = queryRunner.query(sql, new ScalarHandler(columnName)); } else { object = queryRunner.query(sql, new ScalarHandler(columnName), params); } } catch (SQLException e) { LOG.error("Error occured while attempting to query data", e); } return object; } /** * 查询某一条记录,并将指定列的数据转换为Object * * @param sql * sql语句 * @param columnIndex * 列索引 * @return 结果对象 */ public Object findBy(String sql, int columnIndex) { return findBy(sql, columnIndex, null); } /** * 查询某一条记录,并将指定列的数据转换为Object * * @param sql * sql语句 * @param columnIndex * 列索引 * @param param * 参数 * @return 结果对象 */ public Object findBy(String sql, int columnIndex, Object param) { return findBy(sql, columnIndex, new Object[] { param }); } /** * 查询某一条记录,并将指定列的数据转换为Object * * @param sql * sql语句 * @param columnIndex * 列索引 * @param params * 参数数组 * @return 结果对象 */ public Object findBy(String sql, int columnIndex, Object[] params) { queryRunner = new QueryRunner(dataSource); Object object = null; try { if (params == null) { object = queryRunner.query(sql, new ScalarHandler(columnIndex)); } else { object = queryRunner.query(sql, new ScalarHandler(columnIndex), params); } } catch (SQLException e) { LOG.error("Error occured while attempting to query data", e); } return object; }}

3.测试:

public static ClassPathXmlApplicationContext context = null;public static ClassPathXmlApplicationContext getContextInstance() {	if (context == null) {		start();	}	if (!context.isRunning()) {		CopyOfApp.context.refresh();		context.registerShutdownHook();	}	return context;}private static void start() {	context = new ClassPathXmlApplicationContext(			"classpath:spring-source.xml");	context.registerShutdownHook();}public static void main(String[] args) throws Exception {	App.start();	App.context.refresh();	DBUtilsTemplate dbUtilsTemplate = CopyOfApp.context.getBean(			"dbUtilsTemplate", DBUtilsTemplate.class);	String sql = "select count(1) from admin";	List
> list = dbUtilsTemplate.find(sql); System.out.println(list + "--"); }

 

 

转载地址:http://fvqsi.baihongyu.com/

你可能感兴趣的文章
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第四章 - 程序计数器
查看>>
第七章 - 本地方法栈
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
JDBC核心技术 - 下篇
查看>>
一篇搞懂Java反射机制
查看>>
一篇彻底搞懂Java注解与枚举类
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】树
查看>>
MySQL主从复制不一致的原因以及解决方法
查看>>
RedisTemplate的key默认序列化器问题
查看>>
序列化与自定义序列化
查看>>
ThreadLocal
查看>>
从Executor接口设计看设计模式之最少知识法则
查看>>
OKhttp之Call接口
查看>>
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
关于Content-Length
查看>>