企业官方网站地址/seo推广百度百科
DBUtils的使用
1 DBUtils是apache提供的一个主要针对JDBC访问的一个封装(公共组件)
2 如何使用(搭建环境)
<!--使用dbutils工具 这是一个公共组件 主要是对jdbc访问进行的封装 里面有QueryRunner类 用此类进行增删改查--><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.7</version></dependency>
3 基本语法
QueryRunner queryRunner = new QueryRunner(传入datasource);queryRunner.update(sql,args); //args 为参数queryRunner.query(sql,args);new BeanListHandler<>(类.calss) //返回集合 new BeanHandler<>(类.calss); //返回一条结果数据
利用spring进行基于xml的解耦
applicationContext.xml 简单的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--开始进行反射配置 --><!--配置德鲁伊 Druid 数据源 --><!-- 写死的配置 == DruidUtils--><bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" ><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql:///day05"/><property name="username" value="root"/><property name="password" value="root"/></bean><!--<!–配置使用 QueryRunnerQueryRunner queryRunner = new QueryRunner(dataSource)--><bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" ><!-- 使用 构造参数 传入 索引为0 位置的参数 ref 引用传入参数为 druidDataSource --><constructor-arg index="0" ref="druidDataSource" /></bean><!--实例化 AccountDaoImpl--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!--使用set 属性方法 set 和 get 方法必须是公共方法 public 修饰--><property name="queryRunner" ref="queryRunner" /></bean><bean id="accountService" class="com.itheima.services.impl.AccountServicesImpl" ><property name="accountDao" ref="accountDao" /></bean>
</beans>
AccountDaoImpl 的简单模拟 (继承了接口)
package com.itheima.dao.impl;import com.itheima.dao.AccountDao;
import com.itheima.domain.Accounts;
import com.itheima.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;public class AccountDaoImpl implements AccountDao {private QueryRunner queryRunner ; //设置私有的queryRunner
// static {
// queryRunner = new QueryRunner(DruidUtils.getDataSource());
//
// }//使用set 属性方法 给本类添加queryRunnerpublic void setQueryRunner(QueryRunner queryRunner){this.queryRunner = queryRunner;}public AccountDaoImpl() {}@Overridepublic void addAccount(Accounts accounts) {try { //添加数据 没有返会值queryRunner.update("INSERT INTO accounts VALUES (NULL,?,?)",accounts.getAccountName(),accounts.getBalance());} catch (SQLException e) {e.printStackTrace();throw new RuntimeException("添加数据失败");}}@Overridepublic void delAccount(Integer aid) {try {queryRunner.update("DELETE FROM accounts WHERE aid = ? ",aid);} catch (SQLException e) {e.printStackTrace();throw new RuntimeException("删除操作失败");}}@Overridepublic void updataAccount(Accounts accounts) {try {queryRunner.update("UPDATE accounts SET accountName = ? , balance = ? WHERE aid = ? ;",accounts.getAccountName(),accounts.getBalance(),accounts.getAid());} catch (SQLException e) {e.printStackTrace();throw new RuntimeException("修改操作失败");}}@Overridepublic Accounts findAccountByAid(Integer aid) {try { //获取一条 使用query 进行查询操作 使用 new BeanHanler<>(返回数据类字节码文件) 指定返回一条指定数据return queryRunner.query("SELECT * FROM accounts WHERE aid = ? ",new BeanHandler<>(Accounts.class),aid);} catch (Exception e) {e.printStackTrace();return null;}}@Overridepublic List<Accounts> findAll() {try { //查询操作 返回 指定数据集合 使用query查询 指定数据类型 使用 new BeanListHandler<>(数据类型字节码文件)return queryRunner.query("SELECT * FROM accounts",new BeanListHandler<>(Accounts.class));} catch (SQLException e) {e.printStackTrace();}return null;}
}
AccountServicesImpl 的简单模拟 (继承了接口)
package com.itheima.services.impl;import com.itheima.dao.AccountDao;
import com.itheima.domain.Accounts;
import com.itheima.services.AccountServices;import java.util.List;public class AccountServicesImpl implements AccountServices {private AccountDao accountDao;public AccountDao getAccountDao() {return accountDao;}public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void addAccount(Accounts accounts) {accountDao.addAccount(accounts);}@Overridepublic void delAccount(Integer aid) {accountDao.delAccount(aid);}@Overridepublic void updataAccount(Accounts accounts) {accountDao.updataAccount(accounts);}@Overridepublic Accounts findAccountByAid(Integer aid) {return accountDao.findAccountByAid(aid);}@Overridepublic List<Accounts> findAll() {return accountDao.findAll();}
}
test 测试
package com.itheima.test;import com.itheima.dao.AccountDao;
import com.itheima.dao.impl.AccountDaoImpl;
import com.itheima.domain.Accounts;
import com.itheima.services.AccountServices;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class AccountDaoTest {@Testpublic void test01(){ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");AccountDao accountDao = classPathXmlApplicationContext.getBean("accountDao", AccountDao.class);List<Accounts> all = accountDao.findAll();for (Accounts accounts : all) {System.out.println(accounts);}}@Testpublic void test02(){ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");AccountServices accountService = classPathXmlApplicationContext.getBean("accountService", AccountServices.class);List<Accounts> all = accountService.findAll();for (Accounts accounts : all) {System.out.println(accounts);}}
}