1、maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ly.spring</groupId>
<artifactId>spring03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2、实体类

package com.ly.spring.domain;

import java.io.Serializable;

public class Account implements Serializable {
private Integer id;
private Integer uid;
private double money; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getUid() {
return uid;
} public void setUid(Integer uid) {
this.uid = uid;
} public double getMoney() {
return money;
} public void setMoney(double money) {
this.money = money;
} @Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}';
}
}

3、Service接口

package com.ly.spring.service;

import com.ly.spring.domain.Account;

import java.sql.SQLException;
import java.util.List; public interface IAccountService {
public List<Account> findAll() throws SQLException;
public Account findOne(Integer id) throws SQLException;
public void save(Account account) throws SQLException;
public void update(Account account) throws SQLException;
public void delete(Integer id) throws SQLException;
}

4、Service实现类

package com.ly.spring.service.impl;

import com.ly.spring.dao.IAccountDao;
import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.sql.SQLException;
import java.util.List; @Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public List<Account> findAll() throws SQLException {
return accountDao.findAll();
} @Override
public Account findOne(Integer id) throws SQLException {
return accountDao.findOne(id);
} @Override
public void save(Account account) throws SQLException {
accountDao.save(account);
} @Override
public void update(Account account) throws SQLException {
accountDao.update(account);
} @Override
public void delete(Integer id) throws SQLException {
accountDao.delete(id);
}
}

5、Dao接口

package com.ly.spring.dao;

import com.ly.spring.domain.Account;

import java.sql.SQLException;
import java.util.List; public interface IAccountDao {
public List<Account> findAll() throws SQLException;
public Account findOne(Integer id) throws SQLException;
public void save(Account account) throws SQLException;
public void update(Account account) throws SQLException;
public void delete(Integer id) throws SQLException;
}

6、Dao实现类

package com.ly.spring.dao.impl;

import com.ly.spring.dao.IAccountDao;
import com.ly.spring.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import java.sql.SQLException;
import java.util.List; @Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner queryRunner;
public List<Account> findAll() throws SQLException {
return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class));
} @Override
public Account findOne(Integer id) throws SQLException {
return queryRunner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),id);
} @Override
public void save(Account account) throws SQLException {
queryRunner.update("insert into account(uid,money) values(?,?)",account.getUid(),account.getMoney());
} @Override
public void update(Account account) throws SQLException {
queryRunner.update("update account set money = ? where id = ?",account.getMoney(),account.getId());
} @Override
public void delete(Integer id) throws SQLException {
queryRunner.update("delete from account where id = ?",id);
}
}

7、测试类

package com.ly.spring.test;
import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException;
import java.util.List; public class MainTest {
private ApplicationContext context;
private IAccountService accountService;
@Before
public void init() {
context = new ClassPathXmlApplicationContext("bean.xml");
accountService = context.getBean("accountService", IAccountService.class);
}
@Test
public void findAll() throws SQLException {
List<Account> accounts = accountService.findAll();
System.out.println(accounts);
} @Test
public void findOne() throws SQLException {
Account account = accountService.findOne(3);
System.out.println(account);
} @Test
public void save() throws SQLException {
Account account = new Account();
account.setUid(51);
account.setMoney(8000);
accountService.save(account);
} @Test
public void update() throws SQLException {
Account account = new Account();
account.setId(4);
account.setMoney(100000);
accountService.update(account);
}
@Test
public void delete() throws SQLException {
accountService.delete(4);
}
}

8、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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解扫描包-->
<context:component-scan base-package="com.ly.spring"></context:component-scan>
<!--引入外部properties文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置QueryRunner并注入数据源-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!--配置c3p0数据源并注入相关属性-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>

9、数据库配置文件

jdbc.url = jdbc:mysql://localhost:3306/db01
jdbc.driver = com.mysql.jdbc.Driver
jdbc.user = root
jdbc.password = root

commons-dbutils实现增删改查的更多相关文章

  1. 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类

    在上一篇文章中<使用DbUtils实现增删改查>,发现运行runner.query()这行代码时.须要自己去处理查询到的结果集,比較麻烦.这行代码的原型是: public Object q ...

  2. DBUtils的增删改查

    数据准备: CREATE DATABASE mybase; USE mybase; CREATE TABLE users( uid INT PRIMARY KEY AUTO_INCREMENT, us ...

  3. 增删改查——Statement接口

    1.增加数据表中的元组 package pers.datebase.zsgc; import java.sql.Connection; import java.sql.DriverManager; i ...

  4. dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)

    jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...

  5. MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  6. 开源工具DbUtils的使用(数据库的增删改查)

    开源工具DbUtils的使用(数据库的增删改查) 一.DbUtils简介: DBUtils是apache下的一个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果 ...

  7. 增删改查——DBUtils

    利用QueryRunner类实现对数据库的增删改查操作,需要先导入jar包:commons-dbutils-1.6.利用QueryRunner类可以实现对数据步骤的简化. 1.添加 运用JDBC工具类 ...

  8. mvc模式jsp+servel+dbutils oracle基本增删改查demo

    mvc模式jsp+servel+dbutils oracle基本增删改查demo 下载地址

  9. Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用

    前面做了一个非常垃圾的小demo,真的无法直面它,菜的抠脚啊,真的菜,好好努力把.菜鸡. --WH 一.JDBC是什么? Java Data Base Connectivity,java数据库连接,在 ...

  10. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

随机推荐

  1. Go语言实现:【剑指offer】左旋转字符串

    该题目来源于牛客网<剑指offer>专题. 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左 ...

  2. Codeforces Round #617 (Div. 3) D. Fight with Monsters

    D : Fight with Monsters 题目大意 : 有一组数,每个值对应着一个怪物的 hp 值,现在有两个人,一个自己一个对手,每个人有一个攻击值, 两个人轮流攻击怪物,如果是自己将怪物先打 ...

  3. 四步搞定Zabbix 日志文件监控

    Zabbix 日志文件监控 一.给运行Zabbix agent的用户授予要监控日志的读取权限. 1. 執行下面的命令,追加app的可讀權限: setfacl -m u:app:r-- /var/log ...

  4. 20191230--python学习第一天(补)

    1.py第一个脚本 打开电脑终端,功能键+R 输入命令:解释器路径+脚本路径(建议.py后缀) 2.编码 (1)初始编码 ascii,英文,8为表示一个东西,2**8  8位 = 1字节 unicod ...

  5. 忘记centos的root用户密码怎么办?

    1 重置centos7管理员密码 1.1 重置centos7管理员密码的几个步骤 1)重启服务器后,在grub菜单界面,根据界面显示的提示信息,按 e 进入编辑模式.注意:是否开启selinux,重置 ...

  6. 保留yum安装的软件包

    文件路径 /etc/yum.conf [root@opvnserver ~]# grep "keepcache" /etc/yum.conf keepcache=0 [root@o ...

  7. 对C语言整数类型的一点理解

    作者:autogeek 原文链接:http://www.cnblogs.com/autogeek/p/4321635.html 1.先从一个列子引出问题: //sample_1 unsigned ch ...

  8. mac item2的快捷键

    https://juejin.im/entry/58fac23fb123db4449071c99 听说这个工具可以解决,iterm2的整句翻译的问题.一致找不到免费的破解版本. Myna for Go ...

  9. Solr系列2-Solr服务安装

    1: Solr简介 1.1 简介: 1.2 下载: 2:Solr 安装 2.1 安装 2.2 目录结构 3 :启动Solr 3.1 启动 3.2使用Solr提供的测试数据 3.5 Solr配置文集 3 ...

  10. Arm开发板+Qt学习之路-析构函数和对话框一起时

    先记录一下代码 一:先将指针释放掉,在显示对话框 void MainWindow::canResponseError(SendCanMsgThread *sendCanMsgThread ){ std ...