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. postman之设置关联

    接口关联(上一个接口的返回参数作为下一个接口的入参使用): 一:在第一个接口的test点击Response body:JSON value check和set an environment varia ...

  2. 《Redis5.x入门教程》正式推出

    关注公众号CoderBuff回复"redis"可抢先获取<Redis5.x入门教程>PDF完整版 在<ElasticSearch6.x实战教程>之后,又斗胆 ...

  3. Python3(十二) Pythonic与Python杂记

    一.用字典映射代替switch case语句 if/else可以代替switch但是非常不合适. 用字典代替switch: day = 5 switcher = { 0:'Sunday', 1:'Mo ...

  4. PHP 安装扩展步骤

    一般来说php安装扩展需要几下几个步骤   1.下载扩展包    比如  pdo_mysql.tar.gz  (如果不想下载,可以到php安装目录,(类似php-5.3.3/ext/)的ext文件中找 ...

  5. matplotlib如何画子图

    目录 前言 常用的两种方式 方式一:通过plt的subplot 方式二:通过figure的add_subplot 方式三:通过plt的subplots 如何不规则划分 前言 Matplotlib的可以 ...

  6. 2020-2-27今日总结——滚动监听&导航

    利用Bootstrap 开发工具实现滚动监听 (此文只做学习路上的归纳分享总结用,如有侵权,请联系我删除) 使用滚动监听,比较特殊,要在body中设置scroll,以及触点. 很好理解,因为滚动是多对 ...

  7. Linux学习小记(1)---nm*ip

    注意在CentOS7中ifconfig等命令已经被ip取代,ip的功能很强大,而NetworkManager系列命令(nmcli nmtui等)可以用于配置网络连接

  8. Linux下使用Nginx

    模拟tomcat集群 1.下载tomcat7,/usr/local下新建目录tomcat,将tomcat7剪切到/usr/local/tomcat wget http://mirror.bit.edu ...

  9. jq--实现自定义下拉框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. C#连接数据库的方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...