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. UML回顾暨课程总结

    本文作为OO的最后一次博客作业,主要回顾了第四单元的架构设计和本学期的心路历程. 本单元架构设计 UML1 ​ 第一次作业的主要内容是解析mdj格式输入,记录特定数据并支持针对类.属性和方法等的查询功 ...

  2. 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)

    超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...

  3. spark 机器学习 随机森林 实现(二)

    通过天气,温度,风速3个特征,建立随机森林,判断特征的优先级结果 天气 温度 风速结果(0否,1是)天气(0晴天,1阴天,2下雨)温度(0热,1舒适,2冷)风速(0没风,1微风,2大风)1 1:0 2 ...

  4. tomcat7.x配置APR高并发模式

    Tomcat支持BIO/NIO/APR三种运行模式 ,性能各色春秋! Apr插件提高Tomcat性能 Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术. APR(Apac ...

  5. springboot使用过滤器Filter

    先创建过滤器配置类: ## 引入的包部分省略... @Configuration public class FilterConfig { @SuppressWarnings({"rawtyp ...

  6. web-mini框架的基本实现(一)

    本篇主要介绍WSGI-web-mini框架的基本实现,以及什么是WSGI协议.对于浏览器的资源请求服务端是如何处理的等相关知识: 一.浏览器资源请求 我们知道浏览器和web服务器之间是基于HTTP协议 ...

  7. [openssl][nginx] 使用openssl模拟ssl/tls客户端测试nginx stream

    一 server的配置 nginx # cat conf/nginx.conf daemon off; events { debug_connection ; } stream { upstream ...

  8. 【HCIA Gauss】学习汇总-数据库基础介绍-1

    存放在数据库中数据的特点 :永久存储 又组织 可共享 数据库系统是由 数据库 数据库管理系统 应用程序 管理员成员 组成的存储 管理 处理和维护数据的系统 三个阶段:人工阶段 ,文件系统阶段,数据库系 ...

  9. Python_模块的定义与使用

    1.模块的定义: 1.1 标准格式: import 模块名 模块名.函数名(实参列表) 1.2 特殊格式: from 模块名 import 函数名1,函数名2... 函数名(实参列表) 2.模块的使用 ...

  10. 191012 python3关于空格打印、赋值、+=符号的小坑

    1. python3中,直接打印空格不显示,不论是pycharm,cmd命令窗口,还是linux中,都有尝试,但是空格' '不能显示出来: # 打印菱形,只能用center方法for i in ran ...