commons-dbutils实现增删改查
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实现增删改查的更多相关文章
- 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类
在上一篇文章中<使用DbUtils实现增删改查>,发现运行runner.query()这行代码时.须要自己去处理查询到的结果集,比較麻烦.这行代码的原型是: public Object q ...
- DBUtils的增删改查
数据准备: CREATE DATABASE mybase; USE mybase; CREATE TABLE users( uid INT PRIMARY KEY AUTO_INCREMENT, us ...
- 增删改查——Statement接口
1.增加数据表中的元组 package pers.datebase.zsgc; import java.sql.Connection; import java.sql.DriverManager; i ...
- dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)
jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...
- MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 开源工具DbUtils的使用(数据库的增删改查)
开源工具DbUtils的使用(数据库的增删改查) 一.DbUtils简介: DBUtils是apache下的一个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果 ...
- 增删改查——DBUtils
利用QueryRunner类实现对数据库的增删改查操作,需要先导入jar包:commons-dbutils-1.6.利用QueryRunner类可以实现对数据步骤的简化. 1.添加 运用JDBC工具类 ...
- mvc模式jsp+servel+dbutils oracle基本增删改查demo
mvc模式jsp+servel+dbutils oracle基本增删改查demo 下载地址
- Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用
前面做了一个非常垃圾的小demo,真的无法直面它,菜的抠脚啊,真的菜,好好努力把.菜鸡. --WH 一.JDBC是什么? Java Data Base Connectivity,java数据库连接,在 ...
- python操作mysql数据库增删改查的dbutils实例
python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...
随机推荐
- C/C++中的排序和查找
以下内容来自<C/C++程序设计实用案例教程> 1.排序 1.1使用qsort函数 C/C++库函数提供了快速排序函数qsort(q时quick的简写),需要引入头文件<stdlib ...
- VFP 的 SPT 起跳 -- 陈纯(BOE数据网络工作室)
细节描述 Visual FoxPro 的 SPT 技术快速入门 说在前面熟悉 Fox 的朋友都知道,在 VFP 里我们可以使用远程视图 (Remote View) 和 SPT(SQL Pass Thr ...
- k8s 开船记-全站登船:Powered by .NET Core on Kubernetes
今天 18:30 左右,我们迈出了 kubernetes 航行的关键一步——全站登船,完成了全站应用从 docker swarm 集群向 k8s 集群的切换,以前所未有的决心与信心重新开起这艘巨轮,而 ...
- 杭电-------2048不容易系列之(4)考新郎(C语言)
/* 思路:有n位新郎,但是又m位新郎会找错,那么有n-m位新郎会找对,而找对的n-m位新郎的找发就是在 n位新郎中随机找n-m位有多少种排列组合公式有n!/(m!*(n-m!)),而另外找错的新郎则 ...
- springmvc maven idea 多模块开发(四):建立Web子模块
先建立web的父模块,其他子web模块建立在该父模块之下,该模块打包方式选择为pom 建立实际web模块,右键点击web-parent,建立方法同上,打包方式为war 建立好后的目录结构如下: 手工建 ...
- APP图标在线生成
在线生成安卓APP图标生成 图标在线 在线图标 安卓图标 生成图标 https://icon.wuruihong.com/ 在线png图片压缩 png压缩 https://compresspng.c ...
- redis教程-基础数据结构
需要整套redis缓存高可用集群教学视频的加qq:1324981084,本套视频从安装到集群的搭建和源码的解析,从零基础讲解. 一.Redis 有 5 种基础数据结构,分别为:string (字符串) ...
- 普通版js运动框架
//获取样式 function getStyle(obj){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return ge ...
- JMeter接口测试-循环读取库的用户信息
前言 如何实现循环读取数据库的用户信息,并传递到下一个登录请求呢,下面我们一起来学习吧!在之前我们已经学会了利用JMeter连接数据库了,具体操作可以看我之前的随笔JMeter接口测试-JDBC测试 ...
- 为什么我不建议在C#中用下划线_开头来表示私有字段
我在C#官方文档的使用属性里看到这种代码: public class Date { private int _month = 7; // Backing store public int Month ...