代码结构:

bean.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置Service--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><!--注入dao--><property name="accountDao" ref="accountDao"></property></bean><!--配置dao对象--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!--注入QueryRunner--><property name="runner" ref="runner"></property></bean><!--配置QueryBean--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner"><!--配置数据源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!--配置数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--连接数据库的四大必备信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="root"></property></bean>
</beans>

业务接口层

/*** 账户的业务层接口*/
public interface IAccountService {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param accountId*/void deleteAccount(Integer accountId);
}

业务接口层实现

/*** 账户的业务成是先烈*/
public class AccountServiceImpl implements IAccountService {public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}private IAccountDao accountDao;@Overridepublic List<Account> findAllAccount() {return accountDao.findAllAccount();}@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}@Overridepublic void saveAccount(Account account) {accountDao.saveAccount(account);}@Overridepublic void updateAccount(Account account) {accountDao.updateAccount(account);}@Overridepublic void deleteAccount(Integer accountId) {accountDao.deleteAccount(accountId);}
}

持久层接口

/*** 账户的持久层接口*/
public interface IAccountDao {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param accountId*/void deleteAccount(Integer accountId);
}

持久层接口实现

/*** 账户的持久层实现类*/
public class AccountDaoImpl implements IAccountDao {public void setRunner(QueryRunner runner) {this.runner = runner;}private QueryRunner runner;@Overridepublic List<Account> findAllAccount() {try {return runner.query("select * from account",new BeanListHandler<Account>(Account.class));} catch (Exception e) {throw new RuntimeException();}}@Overridepublic Account findAccountById(Integer accountId) {try {return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);} catch (Exception e) {throw new RuntimeException();}}@Overridepublic void saveAccount(Account account) {try {runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());} catch (Exception e) {throw new RuntimeException();}}@Overridepublic void updateAccount(Account account) {try {runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());} catch (Exception e) {throw new RuntimeException();}}@Overridepublic void deleteAccount(Integer accountId) {try {runner.update("delete from account where id=?",accountId);} catch (Exception e) {throw new RuntimeException();}}
}

实体层

/*** 账户的实体类*/
public class Account implements Serializable {private Integer id;private String name;private Float 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 Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}

测试类

/*** 使用Junited单元测试,测试我们的配置*/
public class AccountServiceTest {@Testpublic void testFindAll() {//1. 获取容器ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//2. 得到业务层对象IAccountService as=ac.getBean("accountService",IAccountService.class);//3. 执行方法List<Account> accounts=as.findAllAccount();for(Account account:accounts){System.out.println(account);}}@Testpublic void testFindOne() {//1. 获取容器ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//2. 得到业务层对象IAccountService as=ac.getBean("accountService",IAccountService.class);//3. 执行方法Account account=as.findAccountById(1);}@Testpublic void testSave() {Account account=new Account();account.setName("zhaofen");account.setMoney(200f);//1. 获取容器ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//2. 得到业务层对象IAccountService as=ac.getBean("accountService",IAccountService.class);//3. 执行方法as.saveAccount(account);}@Testpublic void testUpdate() {//1. 获取容器ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//2. 得到业务层对象IAccountService as=ac.getBean("accountService",IAccountService.class);//3. 执行方法Account account=as.findAccountById(4);account.setName("test");as.updateAccount(account);}@Testpublic void testDelete() {//1. 获取容器ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//2. 得到业务层对象IAccountService as=ac.getBean("accountService",IAccountService.class);//3. 执行方法as.deleteAccount(4);}
}

【Spring】基于XML的IOC案例相关推荐

  1. 基于XML的IOC案例

    创建一张数据表account create table account(id int primary key auto_increment,name varchar(40),money float ) ...

  2. spring基于注解的IOC以及IoC的案例——概念

    1.spring中ioc的常用注解 2.案例使用xml方式和注解方式实现单表的CRUD操作     持久层技术选择:dbutils 3.改造基于注解的ioc案例,使用纯注解的方式实现     spri ...

  3. spring框架的概述以及spring中基于XML的IOC配置——概念

    1.spring的概述     spring是什么     spring的两大核心     spring的发展历程和优势     spring体系结构 2.程序的耦合及解耦     曾经案例中问题   ...

  4. 一步一步手绘Spring IOC运行时序图二(基于XML的IOC容器初始化)

    相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...

  5. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

  6. Spring基于XML装配Bean

    Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...

  7. spring基于XML的AOP-编写必要的代码

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  8. 【spring】spring基于xml的声明式事务控制

    结构 domain package com.itheima.domain;import java.io.Serializable;public class Account implements Ser ...

  9. spring基于XML的声明式事务控制-配置步骤

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

最新文章

  1. 命令别名的设置alias,unalias
  2. scrum敏捷开发工具leangoo如何添加成员
  3. 中央暗示:07年别急买房
  4. c语言程序设计电子图书 汉诺塔,用C写的汉诺塔(hanoi)程序
  5. 小米12 Ultra延期发布:或与小米MIX Fold 2折叠屏旗舰同台亮相
  6. 新拟物素材|UI设计领域必掌握的要领!
  7. 曾经的8848,远离真实的代价
  8. 单片机零基础入门(8-5)模块化编程
  9. upf模板,来自synopsys rm golden.upf
  10. 2合1笔记本 android,华为二合一笔记本支持Android可能是鸡肋
  11. JetChat-简仿微信聊天应用
  12. 光盘镜像和系统启动盘制作
  13. html内嵌式选择器,CSS样式 CSS选择器(Cascading Style Sheet)
  14. ip后面带端口号如何做域名解析
  15. SVACH264AVS标准的去块滤波比较
  16. python 多态app_**python多态
  17. 计算机硬件的五大单元以及CPU的种类
  18. SQL难学吗,有什么好的学习建议?
  19. vue是怎么实现数据响应式的?
  20. 数字集成电路设计系列学习总结

热门文章

  1. javabean实体类与实体类之间的快速转换
  2. Go 知识点(18)— 条件编译(编译标签、文件后缀)
  3. 利用牛顿法求平方根-Go语言实现
  4. 【牛腩新闻发布系统】开始前端03
  5. 深度学习的分布式训练--数据并行和模型并行
  6. 5 分钟入门 Google 最强NLP模型:BERT
  7. 旷视MegEngine基本概念
  8. 空间点像素索引(一)
  9. 2021年大数据ELK(四):Lucene的美文搜索案例
  10. C++ 重载运算符 operator