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容器獲取對象就可以