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容器获取对象就可以