利用spring进行基于xml的解耦

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/beans
        http://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>
    <!--&lt;!&ndash;配置使用 QueryRunner
      QueryRunner 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  属性方法  给本类添加queryRunner
    public void setQueryRunner(QueryRunner queryRunner){
        this.queryRunner = queryRunner;
    }

    public AccountDaoImpl() {
    }

    @Override
    public void addAccount(Accounts accounts) {
        try {  //添加数据 没有返会值
            queryRunner.update("INSERT INTO accounts VALUES (NULL,?,?)",accounts.getAccountName(),accounts.getBalance());
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new RuntimeException("添加数据失败");
        }
    }
    @Override
    public void delAccount(Integer aid) {
        try {
            queryRunner.update("DELETE  FROM accounts WHERE aid = ? ",aid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("删除操作失败");
        }
    }

    @Override
    public 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("修改操作失败");
        }
    }

    @Override
    public 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;
        }
    }

    @Override
    public 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;
    }

    @Override
    public void addAccount(Accounts accounts) {
        accountDao.addAccount(accounts);
    }

    @Override
    public void delAccount(Integer aid) {
        accountDao.delAccount(aid);
    }

    @Override
    public void updataAccount(Accounts accounts) {
        accountDao.updataAccount(accounts);
    }

    @Override
    public Accounts findAccountByAid(Integer aid) {
        return accountDao.findAccountByAid(aid);
    }

    @Override
    public 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 {
    @Test
    public 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);
        }
    }
    @Test
    public 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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扶摇的星河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值