DBUtils的简单实现方式

第一步、创建Java工程【此处使用的是maven工程】并导入jar包

<!--导入mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--导入dbutils包-->
<dependency>
<groupId>commons‐dbutils</groupId>
<artifactId>commons‐dbutils</artifactId>
<version>1.6</version>
</dependency>
<!--导入连接池包-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--导入spring包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--导入junit包用于测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

第二步、创建数据库

CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) DEFAULT NULL,
`money` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

第三步、创建JavaBean(又称Pojo对象)

public class Account {
private Integer id;
private String name;
private Double money; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Double getMoney() {
return money;
} public void setMoney(Double money) {
this.money = money;
} @Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}

第四步、创建Dao接口

import java.util.List;

public interface AccountDao {

    public Account findById(Integer id);

    public List<Account> findAll();

    public void save(Account account);

    public void update(Account account);

    public void deleteById(Integer id);

}

第五步、编写实现类

public class AccountDaoImp implements AccountDao{

    private QueryRunner queryRunner;

    //依赖注入
public void setQueryRunner(QueryRunner queryRunner){
this.queryRunner=queryRunner;
} @Override
public Account findById(Integer id) {
Account account=new Account();
//此处要求如下
//第二个 参数是顶级接口ResultSetHandler的实现类
// 如果返回一个对象用BeanResultSetHandler
//如果返回多个对象使用BeanListHandler
//要求数据库字段和实体类字段要要一致
//如果不一致需要实现ResultSetHandler接口
try{
account=queryRunner.query("select * from account where id= ?",new BeanHandler<Account>(Account.class),id);
} catch (Exception e){
e.printStackTrace();
}
return account;
} @Override
public List<Account> findAll() {
List<Account> list=new ArrayList<>();
try{
list=queryRunner.query("select * from account where id= ?",new BeanListHandler<Account>(Account.class));
} catch (Exception e){
e.printStackTrace();
}
return list;
} @Override
public void save(Account account) { } @Override
public void update(Account account) { } @Override
public void deleteById(Integer id) { }
}

第六步、测试

使用Junit调用实现类进行测试!

DBUtils的Spring下的使用

前四步相同,下面从第五步开始

第五步、编写Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 把dataSource交给IOC容器-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///mybatis"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 把queryRunner对象交给IOC容器-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"/>
</bean> <!-- 为accountDao注入依赖-->
<bean id="accountDao" class="AccountDaoImp">
<property name="queryRunner" ref="queryRunner"/>
</bean>
  <!--    把实现类对象交给IOC容器-->
<bean id="" class="">
<constructor-arg name="ds" ref="dataSource"/>
</bean>
</beans>

第六步、编写实现类

在实现类中通过从IOC容器获取对象就可以

第七步、测试

dbutils的环境搭建以及使用的更多相关文章

  1. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门

    2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...

  2. Azure Service Fabric 开发环境搭建

    微服务体系结构是一种将服务器应用程序构建为一组小型服务的方法,每个服务都按自己的进程运行,并通过 HTTP 和 WebSocket 等协议相互通信.每个微服务都在特定的界定上下文(每服务)中实现特定的 ...

  3. rnandroid环境搭建

    react-native 环境搭建具体步骤这个大家已经玩烂了,这个主要是记录下来自己做win7系统遇到的坑 1.com.android.ddmlib.installexception 遇到这个问题,在 ...

  4. python开发环境搭建

    虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...

  5. springMVC初探--环境搭建和第一个HelloWorld简单项目

    注:此篇为学习springMVC时,做的笔记整理. MVC框架要做哪些事情? a,将url映射到java类,或者java类的方法上 b,封装用户提交的数据 c,处理请求->调用相关的业务处理—& ...

  6. 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~

    一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...

  7. Nexus(一)环境搭建

    昨天,成功搭建了自己的 Maven 环境(详见:Maven(一)环境搭建),今天就来研究和探讨下 Nexus 的搭建! 使用背景: 安装环境:Windows 10 -64位 JDK版本:1.7 Mav ...

  8. 「译」JUnit 5 系列:环境搭建

    原文地址:http://blog.codefx.org/libraries/junit-5-setup/ 原文日期:15, Feb, 2016 译文首发:Linesh 的博客:环境搭建 我的 Gith ...

  9. appium+robotframework环境搭建

    appium+robotframework环境搭建步骤(Windows系统的appium自动化测试,只适用于测试安卓机:ios机需要在mac搭建appium环境后测试) 搭建步骤,共分为3部分: 一. ...

随机推荐

  1. 混编用到 C++中数组和vector 复习下大学课本

    本文基于邓俊辉编著<数据结构(C++语言版)(第3版)>.<C++ Primer(第5版)>以及网上的相关博文而写,博主水平有限,若有不妥处,欢迎指出. 一.数组 C++中数组 ...

  2. React Native 开发豆瓣评分(四)集中管理 fetch 数据请求

    豆瓣评分的API接口 接口是从网上查找的,看样子应该是微信小程序里面扣出来的(ua 里面有 wechatdevtools) 接口都需要设置apiKey(054022eaeae0b00e0fc068c0 ...

  3. Node学习之(第三章:art-template模板引擎的使用)

    前言 大家之前都有使用过浏览器中js模板引擎,其实在Node.js中也可以使用模板引擎,最早使用模板引擎的概念是在服务端新起的. art-template art-template是一款高性能的Jav ...

  4. 笔谈runloop(一)

    关于runloop熟悉而又陌生,熟悉的是在iOS开发的过程中线程的执行就跟runloop扯上关系了,陌生的是runloop到底是个什么东西.去公司面试的时候,很多公司的面试人员会问这个问题.runlo ...

  5. linq 书籍推荐 博客汇总 (经典)

    1.博客推荐 博客园linq专区 https://kb.cnblogs.com/zt/linq/ LINQ体验系列文章导航 https://www.cnblogs.com/lyj/archive/20 ...

  6. python字符串的常见方法

    1.join方法:拼接字符串------->str a = "你是风儿我是沙"b = "@".join(a)print(b)>>>你@是 ...

  7. ARM的Semihosting技术(转)

    Semihosting技术将应用程序中的IO请求通过一定的通道传送到主机(host),由主机上的资源响应应用程序的IO请求, 而不是像在主机上执行本地应用程序一样,由应用程序所在的计算机响应应用程序I ...

  8. Flask之WTfroms组件

    一.WTfroms简介 WTForms插件是类似于django的form组件的插件,可以帮我们写标签,校验数据等. 二.安装与使用 安装: pip install WTForms 使用: from w ...

  9. jQuery和bootstrap

    1. jQuery学习,搜索开发者网络: js学习: https://www.apeland.con/web/20/568 https://www.apeland.con/web/21 vue饿了么 ...

  10. Minio对象存储

    目录 Minio对象存储 1.概述 2.功能特性 3.2.多节点 3.3.分布式 4.分布式minio集群搭建 4.1.集群规划 4.3.编写集群启动脚本(所有节点) 4.4.编写服务脚本(所有节点) ...