自制DbHelper实现自动化数据库交互

41 篇文章 1 订阅
6 篇文章 0 订阅
博主仿造apache DbUtils创建了一个小型框架,通过配置数据库连接信息,利用反射和泛型实现bean对象与数据库字段的自动映射。文章介绍了设计思路、依赖、技术细节,并给出了测试代码的提示。源码和jar文件已上传至GitHub,邀请读者一同研究和贡献。
摘要由CSDN通过智能技术生成

之前一直对apache的DbUtils很好奇,也很佩服其中的设计上的智慧。于是就自己模拟实现了一个更加简便的小框架。我们只需要在配置文件中写上数据库层面的连接信息,就可以随心所欲的实现自己的需求了。

设计思路

框架设计初稿

依赖

  • apache-commons-logging.jar
  • apache-commons-beanutils.jar
  • mysql-connector.jar

技术细节

其中最主要的是是使用了反射技术和泛型操作,实现了对bean对象的属性的解析以及实例化相关的bean对象。

public class Converter<T> {

    public static <T> T convert2Bean
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace MyOfficeDAL { public static class DBHelper { private static SqlConnection connection; public static SqlConnection Connection { get { string connectionString = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString; connection = new SqlConnection(connectionString); if (connection == null) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } } public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } public static string ReturnStringScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); try { string result = cmd.ExecuteScalar().ToString(); return result; } catch (Exception ex) { return "0"; } connection.Close(); } public static int GetScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); try { int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } catch (Exception e) { return 0; } connection.Close(); } public static int GetScalar(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; connection.Close(); } public static SqlDataReader GetReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.Close(); reader.Dispose(); } public static SqlDataReader GetReader(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.Close(); reader.Dispose(); } public static DataTable GetDataSet(string safeSql) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); connection.Close(); connection.Dispose(); return ds.Tables[0]; } public static DataTable GetDataSet(string sql, params SqlParameter[] values) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); connection.Close(); connection.Dispose(); return ds.Tables[0]; } } }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰 戈 尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值