【Spring】基于XML的IOC案例
代码结构:
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案例相关推荐
- 基于XML的IOC案例
创建一张数据表account create table account(id int primary key auto_increment,name varchar(40),money float ) ...
- spring基于注解的IOC以及IoC的案例——概念
1.spring中ioc的常用注解 2.案例使用xml方式和注解方式实现单表的CRUD操作 持久层技术选择:dbutils 3.改造基于注解的ioc案例,使用纯注解的方式实现 spri ...
- spring框架的概述以及spring中基于XML的IOC配置——概念
1.spring的概述 spring是什么 spring的两大核心 spring的发展历程和优势 spring体系结构 2.程序的耦合及解耦 曾经案例中问题 ...
- 一步一步手绘Spring IOC运行时序图二(基于XML的IOC容器初始化)
相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...
- spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...
- Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
- spring基于XML的AOP-编写必要的代码
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 【spring】spring基于xml的声明式事务控制
结构 domain package com.itheima.domain;import java.io.Serializable;public class Account implements Ser ...
- spring基于XML的声明式事务控制-配置步骤
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
最新文章
- 命令别名的设置alias,unalias
- scrum敏捷开发工具leangoo如何添加成员
- 中央暗示:07年别急买房
- c语言程序设计电子图书 汉诺塔,用C写的汉诺塔(hanoi)程序
- 小米12 Ultra延期发布:或与小米MIX Fold 2折叠屏旗舰同台亮相
- 新拟物素材|UI设计领域必掌握的要领!
- 曾经的8848,远离真实的代价
- 单片机零基础入门(8-5)模块化编程
- upf模板,来自synopsys rm golden.upf
- 2合1笔记本 android,华为二合一笔记本支持Android可能是鸡肋
- JetChat-简仿微信聊天应用
- 光盘镜像和系统启动盘制作
- html内嵌式选择器,CSS样式 CSS选择器(Cascading Style Sheet)
- ip后面带端口号如何做域名解析
- SVACH264AVS标准的去块滤波比较
- python 多态app_**python多态
- 计算机硬件的五大单元以及CPU的种类
- SQL难学吗,有什么好的学习建议?
- vue是怎么实现数据响应式的?
- 数字集成电路设计系列学习总结